mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-05 01:20:25 +08:00
只有在数据库用户是root时才执行某些命令
This commit is contained in:
@@ -386,6 +386,19 @@ func (this *APINode) setupDB() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查是否为root用户
|
||||||
|
config, _ := db.Config()
|
||||||
|
if config == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
dsnConfig, err := mysql.ParseDSN(config.Dsn)
|
||||||
|
if err != nil || dsnConfig == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dsnConfig.User != "root" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 设置Innodb事务提交模式
|
// 设置Innodb事务提交模式
|
||||||
{
|
{
|
||||||
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name='innodb_flush_log_at_trx_commit'")
|
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name='innodb_flush_log_at_trx_commit'")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package setup
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
@@ -137,12 +138,22 @@ func (this *SQLDump) Dump(db *dbs.DB) (result *SQLDumpResult, err error) {
|
|||||||
func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult, showLog bool) (ops []string, err error) {
|
func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult, showLog bool) (ops []string, err error) {
|
||||||
// 设置Innodb事务提交模式
|
// 设置Innodb事务提交模式
|
||||||
{
|
{
|
||||||
|
// 检查是否为root用户
|
||||||
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name='innodb_flush_log_at_trx_commit'")
|
config, _ := db.Config()
|
||||||
if err == nil && result != nil {
|
if config == nil {
|
||||||
var oldValue = result.GetInt("Value")
|
return nil, nil
|
||||||
if oldValue == 1 {
|
}
|
||||||
_, _ = db.Exec("SET GLOBAL innodb_flush_log_at_trx_commit=2")
|
dsnConfig, err := mysql.ParseDSN(config.Dsn)
|
||||||
|
if err != nil || dsnConfig == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if dsnConfig.User == "root" {
|
||||||
|
result, err := db.FindOne("SHOW VARIABLES WHERE variable_name='innodb_flush_log_at_trx_commit'")
|
||||||
|
if err == nil && result != nil {
|
||||||
|
var oldValue = result.GetInt("Value")
|
||||||
|
if oldValue == 1 {
|
||||||
|
_, _ = db.Exec("SET GLOBAL innodb_flush_log_at_trx_commit=2")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func TestSQLDump_Apply(t *testing.T) {
|
|||||||
|
|
||||||
db2, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
|
db2, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
|
||||||
Driver: "mysql",
|
Driver: "mysql",
|
||||||
Dsn: "root:123456@tcp(192.168.2.60:3306)/db_edge_new?charset=utf8mb4&timeout=30s",
|
Dsn: "edge:123456@tcp(192.168.2.60:3306)/db_edge_new?charset=utf8mb4&timeout=30s",
|
||||||
Prefix: "edge",
|
Prefix: "edge",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user