Skip to content

Commit

Permalink
feat(mysql): 下发nginx addrs配置 #8766
Browse files Browse the repository at this point in the history
  • Loading branch information
xfwduke committed Dec 24, 2024
1 parent c511191 commit 935218f
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,26 @@ func (i *InstallMySQLComp) InitDefaultPrivAndSchemaWithResetMaster() (err error)
initSQLs = append(initSQLs, staticembed.SpiderInitSQL)
}

if bsql, err = staticembed.ProcedureSQL.ReadFile(staticembed.GrantProcedureSQLFileName); err != nil {
logger.Error("读取存储过程嵌入文件%s失败", staticembed.ProcedureSQL)
return err
}
logger.Info("read embed procedure sql success: %s", bsql)

for _, value := range strings.SplitAfterN(string(bsql), `#`, -1) {
if !regexp.MustCompile(`^\\s*$`).MatchString(value) {
initSQLs = append(initSQLs, value)
}
}
// 剔除最后一个空字符,splits 会多分割出一个空字符
if len(initSQLs) < 2 {
return fmt.Errorf("初始化sql为空%v", initSQLs)
}

if i.Params.GetPkgTypeName() == cst.PkgTypeTdbctl {
initSQLs = append(initSQLs, staticembed.SpiderInitSQL)
}

// 调用 mysql-monitor 里的主从复制延迟检查心跳表, infodba_schema.master_slave_heartbeat
initSQLs = append(initSQLs, masterslaveheartbeat.DropTableSQL, masterslaveheartbeat.CreateTableSQL)
if i.Params.GetPkgTypeName() == cst.PkgTypeMysql { // 避免迁移实例时,新机器还没有这个表,会同步失败
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package crond

import (
"dbm-services/common/go-pubpkg/logger"
rcnf "dbm-services/common/reverse-api/config"
"dbm-services/mysql/db-tools/dbactuator/pkg/components"
"dbm-services/mysql/db-tools/dbactuator/pkg/tools"
"dbm-services/mysql/db-tools/dbactuator/pkg/util/osutil"
"fmt"
"os"
"path/filepath"
)

type MySQLCrondComp struct {
Expand All @@ -17,20 +23,64 @@ func (c *MySQLCrondComp) Init() error {
if err != nil {
return err
}

if c.Params.NginxAddrs == nil || len(c.Params.NginxAddrs) <= 0 {
err := fmt.Errorf("nginx addresses are required")
logger.Error(err.Error())
return err
}

// 初始化 nginx addrs 配置文件
err = os.MkdirAll(rcnf.CommonConfigDir, 0777)
if err != nil {
logger.Error("mkdir failed", "err", err)
return err
}

f, err := os.OpenFile(
filepath.Join(
rcnf.CommonConfigDir,
rcnf.NginxProxyAddrsFileName),
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
0777,
)
if err != nil {
logger.Error("open file failed", "err", err)
return err
}
defer func() {
_ = f.Close()
}()

for _, addr := range c.Params.NginxAddrs {
if _, err := f.Write([]byte(addr + "\n")); err != nil {
logger.Error("write addr failed", "err", err)
return err
}
}

chownCmd := fmt.Sprintf(`chown -R mysql %s`, rcnf.CommonConfigDir)
_, err = osutil.ExecShellCommand(false, chownCmd)
if err != nil {
logger.Error("exec command failed", "err", err)
return err
}

return nil
}

type MySQLCrondParam struct {
components.Medium
Ip string `json:"ip"`
BkCloudId int `json:"bk_cloud_id"`
EventDataId int `json:"event_data_id"`
EventDataToken string `json:"event_data_token"`
MetricsDataId int `json:"metrics_data_id"`
MetricsDataToken string `json:"metrics_data_token"`
BeatPath string `json:"beat_path"`
AgentAddress string `json:"agent_address"`
BkBizId int `json:"bk_biz_id"`
Ip string `json:"ip"`
BkCloudId int `json:"bk_cloud_id"`
EventDataId int `json:"event_data_id"`
EventDataToken string `json:"event_data_token"`
MetricsDataId int `json:"metrics_data_id"`
MetricsDataToken string `json:"metrics_data_token"`
BeatPath string `json:"beat_path"`
AgentAddress string `json:"agent_address"`
BkBizId int `json:"bk_biz_id"`
NginxAddrs []string `json:"nginx_addrs"`
}

type runtimeConfig struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import "embed"

// DefaultSysSchemaSQLFileName TODO
const DefaultSysSchemaSQLFileName = "default_sys_schema.sql"
const GrantProcedureSQLFileName = "procedure.sql"

// DefaultSysSchemaSQL TODO
//
//go:embed default_sys_schema.sql
var DefaultSysSchemaSQL embed.FS

//go:embed procedure.sql
var ProcedureSQL embed.FS

// SpiderInitSQL TODO
const SpiderInitSQL = `CREATE TABLE if not exists infodba_schema.tscc_schema_checksum(
db char(64) NOT NULL,
Expand Down
Loading

0 comments on commit 935218f

Please sign in to comment.