Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqlserver): 修复Sqlserver下架前预检测的bug #3791 #3807

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"dbm-services/common/go-pubpkg/logger"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/components"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/core/cst"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/util/sqlserver"
)

Expand All @@ -31,17 +32,6 @@ type CheckInstProcessParam struct {
Port int `json:"port" validate:"required,gt=0"` // 需要操作的实例端口
}

// 定义连接状态的结构
type ProcessInfo struct {
Spid int `db:"spid"`
DbName string `db:"dbname"`
Cmd string `db:"cmd"`
Status string `db:"status"`
ProgramName string `db:"program_name"`
Hostname string `db:"hostname"`
LoginTime string `db:"login_time"`
}

// Init 初始化
func (c *CheckInstProcessComp) Init() error {
var dbWork *sqlserver.DbWorker
Expand All @@ -65,10 +55,8 @@ func (c *CheckInstProcessComp) Init() error {

// CheckInstProcess 检查db连接情况
func (c *CheckInstProcessComp) CheckInstProcess() error {
var procinfos []ProcessInfo
checkCmd := "select spid, DB_NAME(dbid) as dbname ,cmd, status, program_name,hostname, login_time" +
" from master.sys.sysprocesses where dbid >4 and dbid != DB_ID('Monitor') order by login_time desc;"
if err := c.DB.Queryx(&procinfos, checkCmd); err != nil {
var procinfos []sqlserver.ProcessInfo
if err := c.DB.Queryx(&procinfos, cst.CHECK_INST_SQL); err != nil {
return fmt.Errorf("check-abnormal-db failed %v", err)
}
if len(procinfos) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"dbm-services/common/go-pubpkg/logger"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/components"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/core/cst"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/util/osutil"
"dbm-services/sqlserver/db-tools/dbactuator/pkg/util/sqlserver"
)
Expand Down Expand Up @@ -62,12 +63,11 @@ func (u *UnInstallSQLServerComp) Init() error {
// 检查实例连接
func (u *UnInstallSQLServerComp) PreCheck() error {
var isPass bool = true
checkCmd := "SELECT count(0) FROM SYS.SYSPROCESSES WHERE LOGINAME NOT LIKE '%\\%' " +
"AND LOGINAME NOT LIKE '%#%' AND LOGINAME NOT LIKE 'distributor%' AND LOGINAME not in('sa','monitor')"

for _, port := range u.Params.Ports {
var dbWork *sqlserver.DbWorker
var err error
var cnt int
var procinfos []sqlserver.ProcessInfo
if dbWork, err = sqlserver.NewDbWorker(
u.GeneralParam.RuntimeAccountParam.SAUser,
u.GeneralParam.RuntimeAccountParam.SAPwd,
Expand All @@ -83,13 +83,16 @@ func (u *UnInstallSQLServerComp) PreCheck() error {
// 到最后回收db连接
defer dbWork.Stop()

if err := dbWork.Queryxs(&cnt, checkCmd); err != nil {
if err := dbWork.Queryx(&procinfos, cst.CHECK_INST_SQL); err != nil {
logger.Error("check processlist failed %v", err)
isPass = false
}
if cnt != 0 && !u.Params.Force {
if len(procinfos) != 0 && !u.Params.Force {
// 存在用户连接且安全下架情况,退出异常
logger.Error("There is a business connections [%d] on this port [%d]", cnt, port)
for _, info := range procinfos {
logger.Error("process:[%+v]", info)
}
logger.Error("There is a business connections [%d] on this port [%d]", len(procinfos), port)
isPass = false

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ const (
"on a.name=b.name where principal_id>4 and a.name not in('monitor') and a.is_disabled = 0"
)

// 判断实例是否有业务进程
const (
CHECK_INST_SQL = "select spid, DB_NAME(dbid) as dbname ,cmd, status, program_name,hostname, login_time" +
" from master.sys.sysprocesses where dbid >4 and dbid != DB_ID('Monitor') order by login_time desc;"
)

// 定义SQL版本兼容性级别范围

type SQLServerVersionYear int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (w *WINSOSUser) RemoveGroupMember(groupName string) error {
func (w *WINSOSUser) CreateUser(isTranAdmin bool) error {
// 创建账号,账号默认在内置的Users组
if _, err := StandardPowerShellCommand(
fmt.Sprintf("net user %s %s /add /comment:'%s' /passwordchg:no", w.User, w.Pass, w.Comment),
fmt.Sprintf("net user %s %s /add /comment:'%s' /passwordchg:no /expires:NEVER", w.User, w.Pass, w.Comment),
); err != nil {
return err
}
Expand Down
Loading
Loading