Skip to content

Commit

Permalink
fix: 修复sqlserver处理库的sql报错隐患 TencentBlueKing#7183
Browse files Browse the repository at this point in the history
  • Loading branch information
yksitu authored and iSecloud committed Oct 8, 2024
1 parent 532c892 commit 4e3770b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (c *CleanDBSComp) DropdbwithMirroring(dbName string) error {
}
// 表示有建立镜像关系,所以drop之前需要解除
if cnt != 0 {
execDBSQLs = append(execDBSQLs, fmt.Sprintf("ALTER DATABASE %s SET PARTNER OFF;", dbName))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("ALTER DATABASE [%s] SET PARTNER OFF;", dbName))
}
// 查询数据库是否有关联的快照库
getSnapshots := fmt.Sprintf(
Expand All @@ -264,11 +264,11 @@ func (c *CleanDBSComp) DropdbwithMirroring(dbName string) error {
// 如果有存在快照,则先删除快照库
if len(dbSnapshots) != 0 {
for _, snapshot := range dbSnapshots {
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE %s;", snapshot))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE [%s];", snapshot))
}
}
// 拼接执行删除源库
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE %s", dbName))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE [%s]", dbName))

// 执行drop 批命令
if _, err := c.DB.ExecMore(execDBSQLs); err != nil {
Expand Down Expand Up @@ -339,11 +339,11 @@ func (c *CleanDBSComp) DropdbwithAlwayson(dbName string) error {
// 如果有存在快照,则先删除快照库
if len(dbSnapshots) != 0 {
for _, snapshot := range dbSnapshots {
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE %s;", snapshot))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE [%s];", snapshot))
}
}
// 拼接执行删除源库
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE %s", dbName))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE [%s]", dbName))

// 执行drop 批命令
if _, err := c.DB.ExecMore(execDBSQLs); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func (r *InitSqlserverInstanceComp) CreateSysUser() error {
logger.Error("init drs login failed %v", err)
return err
}
// 初始化dbha账号
if err := r.DB.CreateLoginUser(
r.GeneralParam.RuntimeAccountParam.DBHAUser,
r.GeneralParam.RuntimeAccountParam.DBHAPwd,
"sysadmin",
); err != nil {
logger.Error("init dbha login failed %v", err)
return err
}

// 初始化mssql_exporter账号
if err := r.DB.CreateLoginUser(
r.GeneralParam.RuntimeAccountParam.MssqlExporterUser,
Expand All @@ -120,12 +130,12 @@ func (r *InitSqlserverInstanceComp) CreateSysUser() error {
return err
}
// mssql_exporter账号, 授权
cmd := fmt.Sprintf(
exporterCmd := fmt.Sprintf(
cst.GRANT_MSSQL_EXPORTER_SQL,
r.GeneralParam.RuntimeAccountParam.MssqlExporterUser,
)
if _, err := r.DB.Exec(cmd); err != nil {
logger.Error("init mssql_exporter failed %v", err)
if _, err := r.DB.Exec(exporterCmd); err != nil {
logger.Error("init mssql_exporter-grant failed %v", err)
return err
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,11 @@ func (i *InstallSqlServerComp) InitUsers() (err error) {
if err := dbWork.CreateLoginUser(
i.GeneralParam.RuntimeAccountParam.DBHAUser,
i.GeneralParam.RuntimeAccountParam.DBHAPwd,
"public",
"sysadmin",
); err != nil {
logger.Error("init dbha login failed %v", err)
return err
}
// dbha账号, 授权
dbhaCmd := fmt.Sprintf(
cst.GRANT_DBHA_SQL,
i.GeneralParam.RuntimeAccountParam.DBHAUser,
)
if _, err := dbWork.Exec(dbhaCmd); err != nil {
logger.Error("init dbha-grant failed %v", err)
return err
}

// 初始化mssql_exporter账号
if err := dbWork.CreateLoginUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ func DropOldDatabaseOnslave(dbname string, DRS []slaves) error {
// 如果有存在快照,则先删除快照库
if len(dbSnapshots) != 0 {
for _, snapshot := range dbSnapshots {
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE %s;", snapshot))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE [%s];", snapshot))
}
}
// 拼接执行删除源库
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE %s", dbname))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("DROP DATABASE [%s]", dbname))

// 执行drop 从库
if _, err := slave.Connet.ExecMore(execDBSQLs); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,6 @@ var (
`
)

// dbha账号权限sql
var (
GRANT_DBHA_SQL = `
use [master]
GRANT CONTROL SERVER TO [%s] AS [sa]
`
)

// mssql_exporter账号权限
var (
GRANT_MSSQL_EXPORTER_SQL = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ GO

IF DB_ID('Monitor') IS NOT NULL
BEGIN
ALTER DATABASE Monitor SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE Monitor
ALTER DATABASE [Monitor] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [Monitor]
END

--****************************************** DATABASE ****************************************
Expand Down

0 comments on commit 4e3770b

Please sign in to comment.