mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-09 20:10:25 +08:00
优化本地mysql服务自动启动逻辑
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
package dbutils
|
||||
|
||||
import (
|
||||
executils "github.com/TeaOSLab/EdgeAPI/internal/utils/exec"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewQuery 构造Query
|
||||
@@ -127,3 +135,79 @@ func MySQLVersionFrom8() (bool, error) {
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// FindMySQLPath find out mysqld_safe path from system processes
|
||||
func FindMySQLPath() string {
|
||||
psExe, err := executils.LookPath("ps")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var cmd = executils.NewTimeoutCmd(3*time.Second, psExe, "-ef").
|
||||
WithStdout().
|
||||
WithStderr()
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var reg = regexp.MustCompile(`\s(/\S+/mysqld_safe)\s`)
|
||||
var matches = reg.FindStringSubmatch(cmd.Stdout())
|
||||
if len(matches) > 1 {
|
||||
var path = matches[1]
|
||||
_, err = os.Stat(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// FindMySQLPathAndRemember find out mysqld_safe path then remember it for future usage
|
||||
func FindMySQLPathAndRemember() {
|
||||
var path = FindMySQLPath()
|
||||
if len(path) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var cacheFile = Tea.Root + "/data/mysql-path.cache"
|
||||
_ = os.WriteFile(cacheFile, []byte(path), 0666) // ignore error
|
||||
}
|
||||
|
||||
// StartLocalMySQL try to start local mysql server
|
||||
func StartLocalMySQL() {
|
||||
// possible installed paths
|
||||
var mysqldSafeFiles = []string{}
|
||||
|
||||
// read last path from cache file
|
||||
var cacheFile = Tea.Root + "/data/mysql-path.cache"
|
||||
cacheData, err := os.ReadFile(cacheFile)
|
||||
if err == nil && len(cacheData) > 0 {
|
||||
mysqldSafeFiles = append(mysqldSafeFiles, string(cacheData))
|
||||
}
|
||||
|
||||
// from $PATH variable
|
||||
exePath, lookErr := executils.LookPath("mysqld_safe")
|
||||
if lookErr == nil && len(exePath) > 0 && !lists.ContainsString(mysqldSafeFiles, exePath) {
|
||||
mysqldSafeFiles = append(mysqldSafeFiles, exePath)
|
||||
}
|
||||
|
||||
// these installed by edge-boot or foolish-mysql
|
||||
for _, path := range []string{
|
||||
"/usr/local/mysql/bin/mysqld_safe",
|
||||
"/usr/local/mysql8/bin/mysqld_safe",
|
||||
} {
|
||||
if !lists.ContainsString(mysqldSafeFiles, path) {
|
||||
mysqldSafeFiles = append(mysqldSafeFiles, path)
|
||||
}
|
||||
}
|
||||
|
||||
for _, mysqldSafeFile := range mysqldSafeFiles {
|
||||
_, err := os.Stat(mysqldSafeFile)
|
||||
if err == nil {
|
||||
logs.Println("[API_NODE]try to start local mysql server from '" + mysqldSafeFile + "' ...")
|
||||
var mysqlCmd = exec.Command(mysqldSafeFile)
|
||||
_ = mysqlCmd.Start()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user