安装时自动检查服务器上安装的MySQL

This commit is contained in:
刘祥超
2021-11-06 18:35:22 +08:00
parent 3789ac6433
commit a359bff531
4 changed files with 79 additions and 4 deletions

View File

@@ -0,0 +1,61 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package setup
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"net"
"strings"
"time"
)
// DetectDBAction 尝试从本地服务器中发现MySQL
type DetectDBAction struct {
actionutils.ParentAction
}
func (this *DetectDBAction) RunPost(params struct{}) {
var localHost = ""
var localPort = ""
var localUsername = ""
var localPassword = ""
// 本地的3306端口是否可以连接
conn, err := net.DialTimeout("tcp", "127.0.0.1:3306", 3*time.Second)
if err == nil {
_ = conn.Close()
localHost = "127.0.0.1"
localPort = "3306"
var username = "root"
for _, pass := range []string{"", "123456", "654321", "Aa_123456"} {
db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
Driver: "mysql",
Dsn: username + ":" + pass + "@tcp(" + configutils.QuoteIP(localHost) + ":" + localPort + ")/edges11111",
Prefix: "",
})
if err == nil {
err = db.Raw().Ping()
_ = db.Close()
if err == nil || strings.Contains(err.Error(), "Error 1049") {
localUsername = username
localPassword = pass
break
}
}
}
}
this.Data["localDB"] = maps.Map{
"host": localHost,
"port": localPort,
"username": localUsername,
"password": localPassword,
}
this.Success()
}

View File

@@ -13,6 +13,7 @@ func init() {
Post("/validateAdmin", new(ValidateAdminAction)). Post("/validateAdmin", new(ValidateAdminAction)).
Post("/install", new(InstallAction)). Post("/install", new(InstallAction)).
Post("/status", new(StatusAction)). Post("/status", new(StatusAction)).
Post("/detectDB", new(DetectDBAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -149,13 +149,14 @@
<tr> <tr>
<td class="title">MySQL主机地址 *</td> <td class="title">MySQL主机地址 *</td>
<td> <td>
<input type="text" name="host" maxlength="100" placeholder="比如 192.168.1.100" style="width:16em" ref="dbHost"/> <input type="text" name="host" maxlength="100" placeholder="比如 192.168.1.100" style="width:16em" ref="dbHost" v-model="localDB.host"/>
<p class="comment" v-if="localDBHost.length > 0 && localDBHost == localDB.host"><span class="blue">已经自动填入从当前服务器发现的MySQL数据库信息。</span></p>
</td> </td>
</tr> </tr>
<tr> <tr>
<td>数据库连接端口 *</td> <td>数据库连接端口 *</td>
<td> <td>
<input type="text" name="port" maxlength="5" placeholder="比如 3306" style="width:7em"/> <input type="text" name="port" maxlength="5" placeholder="比如 3306" style="width:7em" v-model="localDB.port"/>
</td> </td>
</tr> </tr>
<tr> <tr>
@@ -168,14 +169,14 @@
<tr> <tr>
<td>连接用户名 *</td> <td>连接用户名 *</td>
<td> <td>
<input type="text" name="username" style="width:16em" maxlength="100"/> <input type="text" name="username" style="width:16em" maxlength="100" v-model="localDB.username"/>
<p class="comment">此用户需要可以在数据库中有操作数据和创建数据表的权限。</p> <p class="comment">此用户需要可以在数据库中有操作数据和创建数据表的权限。</p>
</td> </td>
</tr> </tr>
<tr> <tr>
<td>连接密码</td> <td>连接密码</td>
<td> <td>
<input type="password" name="password" style="width:16em" maxlength="100"/> <input type="password" name="password" style="width:16em" maxlength="100" v-model="localDB.password"/>
<p class="comment">连接数据库所需密码,没有密码的话就不需要填写。</p> <p class="comment">连接数据库所需密码,没有密码的话就不需要填写。</p>
</td> </td>
</tr> </tr>

View File

@@ -34,6 +34,7 @@ Tea.context(function () {
this.apiSuccess = function (resp) { this.apiSuccess = function (resp) {
this.step = this.STEP_DB this.step = this.STEP_DB
this.detectDB()
this.apiNodeInfo = resp.data.apiNode this.apiNodeInfo = resp.data.apiNode
if (this.apiNodeMode == "new") { if (this.apiNodeMode == "new") {
@@ -56,7 +57,18 @@ Tea.context(function () {
// 数据库 // 数据库
this.dbInfo = {} this.dbInfo = {}
this.localDB = {"host": "", "port": "", "username": "", "port": ""}
this.localDBHost = ""
this.dbRequesting = false this.dbRequesting = false
this.detectDB = function () {
this.$post(".detectDB")
.success(function (resp) {
this.localDB = resp.data.localDB
this.localDBHost = this.localDB.host
})
}
this.dbSubmit = function () { this.dbSubmit = function () {
this.dbRequesting = true this.dbRequesting = true
} }