diff --git a/backend/utils/mysql/client/info.go b/backend/utils/mysql/client/info.go index 42467fa0b..7e9d22582 100644 --- a/backend/utils/mysql/client/info.go +++ b/backend/utils/mysql/client/info.go @@ -1,5 +1,11 @@ package client +import ( + "strings" + + "github.com/1Panel-dev/1Panel/backend/utils/common" +) + type DBInfo struct { From string `json:"from"` Address string `json:"address"` @@ -85,3 +91,16 @@ var formatMap = map[string]string{ "gbk": "gbk_chinese_ci", "big5": "big5_chinese_ci", } + +func loadNameByDB(name, version string) string { + if strings.HasPrefix(version, "5.6") { + if len(name) <= 16 { + return name + } + return strings.TrimSuffix(name[:10], "_") + "_" + common.RandStr(5) + } + if len(name) <= 32 { + return name + } + return strings.TrimSuffix(name[:25], "_") + "_" + common.RandStr(5) +} diff --git a/backend/utils/mysql/client/local.go b/backend/utils/mysql/client/local.go index ce2e3f9c3..7dd376835 100644 --- a/backend/utils/mysql/client/local.go +++ b/backend/utils/mysql/client/local.go @@ -38,7 +38,7 @@ func (r *Local) Create(info CreateInfo) error { return err } - if err := r.CreateUser(info); err != nil { + if err := r.CreateUser(info, true); err != nil { _ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout) return err } @@ -46,7 +46,7 @@ func (r *Local) Create(info CreateInfo) error { return nil } -func (r *Local) CreateUser(info CreateInfo) error { +func (r *Local) CreateUser(info CreateInfo, withDeleteDB bool) error { var userlist []string if strings.Contains(info.Permission, ",") { ips := strings.Split(info.Permission, ",") @@ -201,7 +201,7 @@ func (r *Local) ChangeAccess(info AccessChangeInfo) error { Password: info.Password, Permission: info.Permission, Timeout: info.Timeout, - }); err != nil { + }, false); err != nil { return err } if err := r.ExecSQL("flush privileges", 300); err != nil { @@ -305,15 +305,16 @@ func (r *Local) SyncDB(version string) ([]SyncDBInfo, error) { } } if len(dataItem.Username) == 0 { + username := loadNameByDB(parts[0], version) if err := r.CreateUser(CreateInfo{ Name: parts[0], Format: parts[1], Version: version, - Username: parts[0], + Username: username, Password: common.RandStr(16), Permission: "%", Timeout: 300, - }); err != nil { + }, false); err != nil { global.LOG.Errorf("sync from remote server failed, err: create user failed %v", err) } dataItem.Username = parts[0] diff --git a/backend/utils/mysql/client/remote.go b/backend/utils/mysql/client/remote.go index 7ab7354e0..c962de558 100644 --- a/backend/utils/mysql/client/remote.go +++ b/backend/utils/mysql/client/remote.go @@ -39,7 +39,7 @@ func (r *Remote) Create(info CreateInfo) error { return err } - if err := r.CreateUser(info); err != nil { + if err := r.CreateUser(info, true); err != nil { _ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout) return err } @@ -47,7 +47,7 @@ func (r *Remote) Create(info CreateInfo) error { return nil } -func (r *Remote) CreateUser(info CreateInfo) error { +func (r *Remote) CreateUser(info CreateInfo, withDeleteDB bool) error { var userlist []string if strings.Contains(info.Permission, ",") { ips := strings.Split(info.Permission, ",") @@ -62,15 +62,17 @@ func (r *Remote) CreateUser(info CreateInfo) error { for _, user := range userlist { if err := r.ExecSQL(fmt.Sprintf("create user %s identified by '%s';", user, info.Password), info.Timeout); err != nil { - _ = r.Delete(DeleteInfo{ - Name: info.Name, - Version: info.Version, - Username: info.Username, - Permission: info.Permission, - ForceDelete: true, - Timeout: 300}) - if strings.Contains(err.Error(), "ERROR 1396") { - return buserr.New(constant.ErrUserIsExist) + if withDeleteDB { + _ = r.Delete(DeleteInfo{ + Name: info.Name, + Version: info.Version, + Username: info.Username, + Permission: info.Permission, + ForceDelete: true, + Timeout: 300}) + if strings.Contains(err.Error(), "ERROR 1396") { + return buserr.New(constant.ErrUserIsExist) + } } return err } @@ -82,13 +84,15 @@ func (r *Remote) CreateUser(info CreateInfo) error { grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, info.Password) } if err := r.ExecSQL(grantStr, info.Timeout); err != nil { - _ = r.Delete(DeleteInfo{ - Name: info.Name, - Version: info.Version, - Username: info.Username, - Permission: info.Permission, - ForceDelete: true, - Timeout: 300}) + if withDeleteDB { + _ = r.Delete(DeleteInfo{ + Name: info.Name, + Version: info.Version, + Username: info.Username, + Permission: info.Permission, + ForceDelete: true, + Timeout: 300}) + } return err } } @@ -202,7 +206,7 @@ func (r *Remote) ChangeAccess(info AccessChangeInfo) error { Password: info.Password, Permission: info.Permission, Timeout: info.Timeout, - }); err != nil { + }, false); err != nil { return err } if err := r.ExecSQL("flush privileges", 300); err != nil { @@ -312,16 +316,17 @@ func (r *Remote) SyncDB(version string) ([]SyncDBInfo, error) { i++ } if len(dataItem.Username) == 0 { + userName := loadNameByDB(dbName, version) if err := r.CreateUser(CreateInfo{ Name: dbName, Format: charsetName, Version: version, - Username: dbName, + Username: userName, Password: common.RandStr(16), Permission: "%", Timeout: 300, - }); err != nil { - global.LOG.Errorf("sync from remote server failed, err: create user failed %v", err) + }, false); err != nil { + return datas, fmt.Errorf("sync db from remote server failed, err: create user failed %v", err) } dataItem.Username = dbName dataItem.Permission = "%"