Skip to content

Commit

Permalink
feat(mysql): mysql逻辑恢复关闭slow_log #7965
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook committed Nov 14, 2024
1 parent c74cc8b commit 91e40d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,10 @@ func (l *LogicalLoader) CreateConfigFile() error {
// PreLoad 在解压之前做的事情
// 检查实例连通性
func (l *LogicalLoader) PreLoad() error {
if err := l.buildFilter(); err != nil {
return err
}

dbWorker, err := l.TgtInstance.Conn()
err := l.buildFilter()
if err != nil {
return errors.Wrap(err, "目标实例连接失败")
}
defer dbWorker.Stop()
if _, err = dbWorker.Exec("set global init_connect=''"); err != nil { // 禁用 init_connect,这里为了兼容跑一次置空
return err
}
/*
if len(l.Databases) == 1 && l.Databases[0] == "*" { // 如果全库导入,删掉 infodba_schema 库(确保备份会导出 infodba_schema)
if _, err = dbWorker.ExecMore([]string{"set session sql_log_bin=off",
fmt.Sprintf("DROP DATABASE IF EXISTS %s", native.INFODBA_SCHEMA)}); err != nil {
return errors.WithMessage(err, "fail to run drop database if exists infodba_schema")
}
}
*/
return nil
}

Expand All @@ -109,6 +93,22 @@ func (l *LogicalLoader) Load() error {
return err
}

// set variables
dbWorker, err := l.TgtInstance.Conn()
if err != nil {
return errors.Wrap(err, "目标实例连接失败")
}
defer dbWorker.Stop()
if _, err = dbWorker.Exec("set global init_connect=''"); err != nil { // 禁用 init_connect,这里为了兼容跑一次置空
logger.Warn("failed to set global init_connect='' for %d. ignore %s", l.TgtInstance.Port, err.Error())
}
// 关闭慢查询
originalValue, err := dbWorker.SetSingleGlobalVarAndReturnOrigin("slow_query_log", "off")
if err != nil {
logger.Warn("failed to set global slow_query_log=off for %d. ignore %s", l.TgtInstance.Port, err.Error())
} else {
defer dbWorker.SetSingleGlobalVar("slow_query_log", originalValue)
}
if err := l.loadBackup(); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func (m *MLoad) Start() error {
if cmutil.FileExists(filepath.Join(m.targetDir, native.INFODBA_SCHEMA)) {
m.mloadUtil.IgnoreDatabases = append(m.mloadUtil.IgnoreDatabases, native.INFODBA_SCHEMA)
}
// 关闭慢查询
originalValue, err := m.dbWorker.SetSingleGlobalVarAndReturnOrigin("slow_query_log", "off")
if err != nil {
logger.Warn("failed to set global slow_query_log=off for %d. ignore %s", m.TgtInstance.Port, err.Error())
} else {
defer m.dbWorker.SetSingleGlobalVar("slow_query_log", originalValue)
}
if err := m.mloadUtil.Run(); err != nil {
return errors.Wrap(err, "mloadData failed")
}
Expand Down
17 changes: 12 additions & 5 deletions dbm-services/mysql/db-tools/dbactuator/pkg/native/dbworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"database/sql"
"errors"
"fmt"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -501,17 +502,23 @@ func (h *DbWorker) SetSingleGlobalVarAndReturnOrigin(varName, varValue string) (
if err != nil {
return "", err
}
sqlstr := fmt.Sprintf("SET GLOBAL %s='%s'", varName, varValue)
if err = h.ExecuteAdminSql(sqlstr); err != nil {
if err = h.SetSingleGlobalVar(varName, varValue); err != nil {
return "", err
}
return originValue, nil
}

// SetSingleGlobalVar set global
// SetSingleGlobalVar set global, 会自动识别数字/bool/string
func (h *DbWorker) SetSingleGlobalVar(varName, varValue string) error {
sqlstr := fmt.Sprintf("SET GLOBAL %s='%s'", varName, varValue)
if err := h.ExecuteAdminSql(sqlstr); err != nil {
var setSqlStr string
if valLower := strings.ToLower(varValue); slices.Contains([]string{"on", "off"}, valLower) {
setSqlStr = fmt.Sprintf("SET GLOBAL %s=%s", varName, valLower)
} else if varInt, err := cast.ToInt64E(varValue); err == nil {
setSqlStr = fmt.Sprintf("SET GLOBAL %s=%d", varName, varInt)
} else {
setSqlStr = fmt.Sprintf("SET GLOBAL %s='%s'", varName, varValue)
}
if err := h.ExecuteAdminSql(setSqlStr); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 91e40d5

Please sign in to comment.