mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
优化本地mysql服务自动启动逻辑
This commit is contained in:
@@ -1,10 +1,18 @@
|
|||||||
package dbutils
|
package dbutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
executils "github.com/TeaOSLab/EdgeAPI/internal/utils/exec"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewQuery 构造Query
|
// NewQuery 构造Query
|
||||||
@@ -127,3 +135,79 @@ func MySQLVersionFrom8() (bool, error) {
|
|||||||
}
|
}
|
||||||
return false, nil
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,3 +35,11 @@ func TestMySQLVersion(t *testing.T) {
|
|||||||
func TestMySQLVersionFrom8(t *testing.T) {
|
func TestMySQLVersionFrom8(t *testing.T) {
|
||||||
t.Log(dbutils.MySQLVersionFrom8())
|
t.Log(dbutils.MySQLVersionFrom8())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindMySQLPath(t *testing.T) {
|
||||||
|
t.Log(dbutils.FindMySQLPath())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStartLocalMySQL(t *testing.T) {
|
||||||
|
dbutils.StartLocalMySQL()
|
||||||
|
}
|
||||||
@@ -295,6 +295,9 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
|
|||||||
func (this *APINode) checkDB() error {
|
func (this *APINode) checkDB() error {
|
||||||
logs.Println("[API_NODE]checking database connection ...")
|
logs.Println("[API_NODE]checking database connection ...")
|
||||||
|
|
||||||
|
// lookup mysqld_safe process
|
||||||
|
go dbutils.FindMySQLPathAndRemember()
|
||||||
|
|
||||||
db, err := dbs.Default()
|
db, err := dbs.Default()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -311,13 +314,7 @@ func (this *APINode) checkDB() error {
|
|||||||
if strings.Contains(err.Error(), "connection refused") {
|
if strings.Contains(err.Error(), "connection refused") {
|
||||||
config, _ := db.Config()
|
config, _ := db.Config()
|
||||||
if config != nil && (strings.Contains(config.Dsn, "tcp(127.0.0.1:") || strings.Contains(config.Dsn, "tcp(localhost:")) && os.Getgid() == 0 /** ROOT 用户 **/ {
|
if config != nil && (strings.Contains(config.Dsn, "tcp(127.0.0.1:") || strings.Contains(config.Dsn, "tcp(localhost:")) && os.Getgid() == 0 /** ROOT 用户 **/ {
|
||||||
var mysqldSafeFile = "/usr/local/mysql/bin/mysqld_safe"
|
dbutils.StartLocalMySQL()
|
||||||
_, 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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -835,7 +832,7 @@ func (this *APINode) unaryInterceptor(ctx context.Context, req any, info *grpc.U
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
statusErr, ok := status.FromError(err)
|
statusErr, ok := status.FromError(err)
|
||||||
if ok {
|
if ok {
|
||||||
err = status.Error(statusErr.Code(), "'" + info.FullMethod + "()' says: " + err.Error())
|
err = status.Error(statusErr.Code(), "'"+info.FullMethod+"()' says: "+err.Error())
|
||||||
} else {
|
} else {
|
||||||
err = errors.New("'" + info.FullMethod + "()' says: " + err.Error())
|
err = errors.New("'" + info.FullMethod + "()' says: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
59
internal/utils/exec/look_linux.go
Normal file
59
internal/utils/exec/look_linux.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package executils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LookPath customize our LookPath() function, to work in broken $PATH environment variable
|
||||||
|
func LookPath(file string) (string, error) {
|
||||||
|
result, err := exec.LookPath(file)
|
||||||
|
if err == nil && len(result) > 0 {
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// add common dirs contains executable files these may be excluded in $PATH environment variable
|
||||||
|
var binPaths = []string{
|
||||||
|
"/usr/sbin",
|
||||||
|
"/usr/bin",
|
||||||
|
"/usr/local/sbin",
|
||||||
|
"/usr/local/bin",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, binPath := range binPaths {
|
||||||
|
var fullPath = binPath + string(os.PathSeparator) + file
|
||||||
|
|
||||||
|
stat, err := os.Stat(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if stat.IsDir() {
|
||||||
|
return "", syscall.EISDIR
|
||||||
|
}
|
||||||
|
|
||||||
|
var mode = stat.Mode()
|
||||||
|
if mode.IsDir() {
|
||||||
|
return "", syscall.EISDIR
|
||||||
|
}
|
||||||
|
err = syscall.Faccessat(unix.AT_FDCWD, fullPath, unix.X_OK, unix.AT_EACCESS)
|
||||||
|
if err == nil || (err != syscall.ENOSYS && err != syscall.EPERM) {
|
||||||
|
return fullPath, err
|
||||||
|
}
|
||||||
|
if mode&0111 != 0 {
|
||||||
|
return fullPath, nil
|
||||||
|
}
|
||||||
|
return "", fs.ErrPermission
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", &exec.Error{
|
||||||
|
Name: file,
|
||||||
|
Err: exec.ErrNotFound,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
10
internal/utils/exec/look_others.go
Normal file
10
internal/utils/exec/look_others.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package executils
|
||||||
|
|
||||||
|
import "os/exec"
|
||||||
|
|
||||||
|
func LookPath(file string) (string, error) {
|
||||||
|
return exec.LookPath(file)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user