1Panel/backend/app/service/database_mysql.go

487 lines
15 KiB
Go
Raw Normal View History

2022-10-20 18:45:47 +08:00
package service
import (
"compress/gzip"
2022-10-21 18:50:38 +08:00
"encoding/json"
"fmt"
"os"
2022-10-25 18:34:33 +08:00
"os/exec"
"strconv"
2022-10-25 18:34:33 +08:00
"strings"
"time"
2022-10-21 18:50:38 +08:00
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
2022-10-25 18:34:33 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
2022-10-21 18:50:38 +08:00
_ "github.com/go-sql-driver/mysql"
2022-10-20 18:45:47 +08:00
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
type MysqlService struct{}
type IMysqlService interface {
2022-10-25 18:34:33 +08:00
SearchWithPage(search dto.SearchDBWithPage) (int64, interface{}, error)
ListDBByVersion(version string) ([]string, error)
SearchBackupsWithPage(search dto.SearchBackupsWithPage) (int64, interface{}, error)
2022-10-20 18:45:47 +08:00
Create(mysqlDto dto.MysqlDBCreate) error
ChangeInfo(info dto.ChangeDBInfo) error
UpdateVariables(variables dto.MysqlVariablesUpdate) error
Backup(db dto.BackupDB) error
Recover(db dto.RecoverDB) error
2022-10-25 18:34:33 +08:00
Delete(version string, ids []uint) error
2022-10-21 18:50:38 +08:00
LoadStatus(version string) (*dto.MysqlStatus, error)
LoadVariables(version string) (*dto.MysqlVariables, error)
2022-10-25 18:34:33 +08:00
LoadRunningVersion() ([]string, error)
LoadBaseInfo(version string) (*dto.DBBaseInfo, error)
2022-10-20 18:45:47 +08:00
}
func NewIMysqlService() IMysqlService {
return &MysqlService{}
}
2022-10-25 18:34:33 +08:00
func (u *MysqlService) SearchWithPage(search dto.SearchDBWithPage) (int64, interface{}, error) {
total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize, mysqlRepo.WithByVersion(search.Version))
2022-10-20 18:45:47 +08:00
var dtoMysqls []dto.MysqlDBInfo
for _, mysql := range mysqls {
var item dto.MysqlDBInfo
if err := copier.Copy(&item, &mysql); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
dtoMysqls = append(dtoMysqls, item)
}
return total, dtoMysqls, err
}
func (u *MysqlService) ListDBByVersion(version string) ([]string, error) {
mysqls, err := mysqlRepo.List(mysqlRepo.WithByVersion(version))
var dbNames []string
for _, mysql := range mysqls {
dbNames = append(dbNames, mysql.Name)
}
return dbNames, err
}
func (u *MysqlService) SearchBackupsWithPage(search dto.SearchBackupsWithPage) (int64, interface{}, error) {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(search.Version)
if err != nil {
return 0, nil, err
}
searchDto := dto.BackupSearch{
Type: "database-mysql",
PageInfo: search.PageInfo,
Name: app.Name,
DetailName: search.DBName,
}
return NewIBackupService().SearchRecordWithPage(searchDto)
}
2022-10-25 18:34:33 +08:00
func (u *MysqlService) LoadRunningVersion() ([]string, error) {
return mysqlRepo.LoadRunningVersion()
}
2022-10-20 18:45:47 +08:00
func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
if mysqlDto.Username == "root" {
return errors.New("Cannot set root as user name")
}
2022-10-20 18:45:47 +08:00
mysql, _ := mysqlRepo.Get(commonRepo.WithByName(mysqlDto.Name))
if mysql.ID != 0 {
return constant.ErrRecordExist
}
if err := copier.Copy(&mysql, &mysqlDto); err != nil {
return errors.WithMessage(constant.ErrStructTransform, err.Error())
}
2022-10-25 18:34:33 +08:00
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(mysqlDto.Version)
if err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("create database if not exists %s character set=%s", mysqlDto.Name, mysqlDto.Format)); err != nil {
return err
}
tmpPermission := mysqlDto.Permission
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("create user if not exists '%s'@'%s' identified by '%s';", mysqlDto.Name, tmpPermission, mysqlDto.Password)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
grantStr := fmt.Sprintf("grant all privileges on %s.* to '%s'@'%s'", mysqlDto.Name, mysqlDto.Username, tmpPermission)
if mysqlDto.Version == "5.7.39" {
2022-10-25 18:34:33 +08:00
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, mysqlDto.Password)
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, grantStr); err != nil {
return err
}
2022-10-20 18:45:47 +08:00
if err := mysqlRepo.Create(&mysql); err != nil {
return err
}
return nil
}
func (u *MysqlService) Backup(db dto.BackupDB) error {
backupLocal, err := backupRepo.Get(commonRepo.WithByType("LOCAL"))
if err != nil {
return err
}
localDir, err := loadLocalDir(backupLocal)
if err != nil {
return err
}
backupDir := fmt.Sprintf("database/%s/%s", db.Version, db.DBName)
fileName := fmt.Sprintf("%s_%s.sql.gz", db.DBName, time.Now().Format("20060102150405"))
if err := backupMysql("LOCAL", localDir, backupDir, db.Version, db.DBName, fileName); err != nil {
return err
}
return nil
}
func (u *MysqlService) Recover(db dto.RecoverDB) error {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(db.Version)
if err != nil {
return err
}
gzipFile, err := os.Open(db.BackupName)
if err != nil {
fmt.Println(err)
}
defer gzipFile.Close()
gzipReader, err := gzip.NewReader(gzipFile)
if err != nil {
fmt.Println(err)
}
defer gzipReader.Close()
cmd := exec.Command("docker", "exec", "-i", app.ContainerName, "mysql", "-uroot", "-p"+app.Password, db.DBName)
cmd.Stdin = gzipReader
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return errors.New(stdStr)
}
return nil
}
2022-10-25 18:34:33 +08:00
func (u *MysqlService) Delete(version string, ids []uint) error {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(version)
if err != nil {
return err
}
2022-10-25 18:34:33 +08:00
dbs, err := mysqlRepo.List(commonRepo.WithIdsIn(ids))
if err != nil {
return err
}
for _, db := range dbs {
if len(db.Name) != 0 {
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists '%s'@'%s'", db.Name, db.Permission)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop database if exists %s", db.Name)); err != nil {
return err
}
2022-10-20 18:45:47 +08:00
}
_ = mysqlRepo.Delete(commonRepo.WithByID(db.ID))
2022-10-20 18:45:47 +08:00
}
return nil
2022-10-20 18:45:47 +08:00
}
2022-10-21 18:50:38 +08:00
func (u *MysqlService) ChangeInfo(info dto.ChangeDBInfo) error {
2022-10-25 18:34:33 +08:00
var (
mysql model.DatabaseMysql
err error
)
if info.ID != 0 {
mysql, err = mysqlRepo.Get(commonRepo.WithByID(info.ID))
if err != nil {
return err
}
}
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(info.Version)
if err != nil {
return err
2022-10-21 18:50:38 +08:00
}
if info.Operation == "password" {
2022-10-25 18:34:33 +08:00
if info.ID != 0 {
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set password for %s@%s = password('%s')", mysql.Username, mysql.Permission, info.Value)); err != nil {
return err
}
_ = mysqlRepo.Update(mysql.ID, map[string]interface{}{"password": info.Value})
return nil
}
hosts, err := excuteSqlForRows(app.ContainerName, app.Password, "select host from mysql.user where user='root';")
if err != nil {
return err
}
2022-10-25 18:34:33 +08:00
for _, host := range hosts {
if host == "%" || host == "localhost" {
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set password for root@'%s' = password('%s')", host, info.Value)); err != nil {
return err
}
}
}
_ = mysqlRepo.UpdateMysqlConf(app.ID, map[string]interface{}{
"param": strings.ReplaceAll(app.Param, app.Password, info.Value),
"env": strings.ReplaceAll(app.Env, app.Password, info.Value),
})
return nil
}
2022-10-21 18:50:38 +08:00
2022-10-25 18:34:33 +08:00
if info.ID == 0 {
mysql.Name = "*"
mysql.Username = "root"
mysql.Permission = "%"
mysql.Password = app.Password
}
if info.Value != mysql.Permission {
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists '%s'@'%s'", mysql.Username, mysql.Permission)); err != nil {
return err
}
if info.ID == 0 {
return nil
}
}
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("create user if not exists '%s'@'%s' identified by '%s';", mysql.Username, info.Value, mysql.Password)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
grantStr := fmt.Sprintf("grant all privileges on %s.* to '%s'@'%s'", mysql.Name, mysql.Username, info.Value)
if mysql.Version == "5.7.39" {
2022-10-25 18:34:33 +08:00
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, mysql.Password)
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, grantStr); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, "flush privileges"); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if info.ID == 0 {
return nil
}
_ = mysqlRepo.Update(mysql.ID, map[string]interface{}{"permission": info.Value})
return nil
}
func (u *MysqlService) UpdateVariables(variables dto.MysqlVariablesUpdate) error {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(variables.Version)
if err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL key_buffer_size=%d", variables.KeyBufferSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL query_cache_size=%d", variables.QueryCacheSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL tmp_table_size=%d", variables.TmpTableSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL innodb_buffer_pool_size=%d", variables.InnodbBufferPoolSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
// if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL innodb_log_buffer_size=%d", variables.InnodbLogBufferSize)); err != nil {
// return err
// }
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL sort_buffer_size=%d", variables.SortBufferSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL read_buffer_size=%d", variables.ReadBufferSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL read_rnd_buffer_size=%d", variables.ReadRndBufferSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL join_buffer_size=%d", variables.JoinBufferSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
// if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL thread_stack=%d", variables.ThreadStack)); err != nil {
// return err
// }
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL binlog_cache_size=%d", variables.BinlogCachSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL thread_cache_size=%d", variables.ThreadCacheSize)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL table_open_cache=%d", variables.TableOpenCache)); err != nil {
return err
}
2022-10-25 18:34:33 +08:00
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("set GLOBAL max_connections=%d", variables.MaxConnections)); err != nil {
return err
}
return nil
}
2022-10-25 18:34:33 +08:00
func (u *MysqlService) LoadBaseInfo(version string) (*dto.DBBaseInfo, error) {
var data dto.DBBaseInfo
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(version)
2022-10-21 18:50:38 +08:00
if err != nil {
return nil, err
}
2022-10-25 18:34:33 +08:00
data.Name = app.Name
data.Port = int64(app.Port)
data.Password = app.Password
2022-10-21 18:50:38 +08:00
2022-10-25 18:34:33 +08:00
hosts, err := excuteSqlForRows(app.ContainerName, app.Password, "select host from mysql.user where user='root';")
if err != nil {
return nil, err
}
for _, host := range hosts {
if host == "%" {
data.RemoteConn = true
break
}
}
return &data, nil
}
func (u *MysqlService) LoadVariables(version string) (*dto.MysqlVariables, error) {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(version)
2022-10-25 18:34:33 +08:00
if err != nil {
return nil, err
}
variableMap, err := excuteSqlForMaps(app.ContainerName, app.Password, "show global variables;")
if err != nil {
return nil, err
}
var info dto.MysqlVariables
2022-10-21 18:50:38 +08:00
arr, err := json.Marshal(variableMap)
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
return &info, nil
}
func (u *MysqlService) LoadStatus(version string) (*dto.MysqlStatus, error) {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(version)
2022-10-21 18:50:38 +08:00
if err != nil {
return nil, err
}
2022-10-25 18:34:33 +08:00
statusMap, err := excuteSqlForMaps(app.ContainerName, app.Password, "show global status;")
2022-10-21 18:50:38 +08:00
if err != nil {
return nil, err
}
2022-10-25 18:34:33 +08:00
2022-10-21 18:50:38 +08:00
var info dto.MysqlStatus
2022-10-25 18:34:33 +08:00
arr, err := json.Marshal(statusMap)
2022-10-21 18:50:38 +08:00
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
2022-10-25 18:34:33 +08:00
if value, ok := statusMap["Run"]; ok {
uptime, _ := strconv.Atoi(value)
info.Run = time.Unix(time.Now().Unix()-int64(uptime), 0).Format("2006-01-02 15:04:05")
} else {
2022-10-25 18:34:33 +08:00
if value, ok := statusMap["Uptime"]; ok {
uptime, _ := strconv.Atoi(value)
info.Run = time.Unix(time.Now().Unix()-int64(uptime), 0).Format("2006-01-02 15:04:05")
}
}
2022-10-25 18:34:33 +08:00
info.File = "OFF"
info.Position = "OFF"
rows, err := excuteSqlForRows(app.ContainerName, app.Password, "show master status;")
if err != nil {
return nil, err
}
if len(rows) > 2 {
itemValue := strings.Split(rows[1], "\t")
if len(itemValue) > 2 {
info.File = itemValue[0]
info.Position = itemValue[1]
}
}
return &info, nil
}
func excuteSqlForMaps(containerName, password, command string) (map[string]string, error) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
2022-10-25 18:34:33 +08:00
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return nil, errors.New(stdStr)
}
2022-10-25 18:34:33 +08:00
rows := strings.Split(stdStr, "\n")
rowMap := make(map[string]string)
for _, v := range rows {
itemRow := strings.Split(v, "\t")
if len(itemRow) == 2 {
rowMap[itemRow[0]] = itemRow[1]
}
}
2022-10-25 18:34:33 +08:00
return rowMap, nil
}
func excuteSqlForRows(containerName, password, command string) ([]string, error) {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
2022-10-25 18:34:33 +08:00
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return nil, errors.New(stdStr)
}
2022-10-25 18:34:33 +08:00
return strings.Split(stdStr, "\n"), nil
}
func excuteSql(containerName, password, command string) error {
cmd := exec.Command("docker", "exec", containerName, "mysql", "-uroot", "-p"+password, "-e", command)
2022-10-25 18:34:33 +08:00
stdout, err := cmd.CombinedOutput()
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
if err != nil || strings.HasPrefix(string(stdStr), "ERROR ") {
return errors.New(stdStr)
2022-10-25 18:34:33 +08:00
}
return nil
2022-10-21 18:50:38 +08:00
}
func backupMysql(backupType, baseDir, backupDir, version, dbName, fileName string) error {
2022-10-31 17:26:15 +08:00
app, err := mysqlRepo.LoadBaseInfoByKey(version)
if err != nil {
return err
}
fullDir := baseDir + "/" + backupDir
if _, err := os.Stat(fullDir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(fullDir, os.ModePerm); err != nil {
if err != nil {
return fmt.Errorf("mkdir %s failed, err: %v", fullDir, err)
}
}
}
outfile, _ := os.OpenFile(fullDir+"/"+fileName, os.O_RDWR|os.O_CREATE, 0755)
cmd := exec.Command("docker", "exec", app.ContainerName, "mysqldump", "-uroot", "-p"+app.Password, dbName)
gzipCmd := exec.Command("gzip", "-cf")
gzipCmd.Stdin, _ = cmd.StdoutPipe()
gzipCmd.Stdout = outfile
_ = gzipCmd.Start()
_ = cmd.Run()
_ = gzipCmd.Wait()
record := &model.BackupRecord{
Type: "database-mysql",
Name: app.Name,
DetailName: dbName,
Source: backupType,
2022-10-31 17:26:15 +08:00
BackupType: backupType,
FileDir: backupDir,
FileName: fileName,
}
if baseDir != constant.TmpDir || backupType == "LOCAL" {
record.Source = "LOCAL"
record.FileDir = fullDir
}
if err := backupRepo.CreateRecord(record); err != nil {
global.LOG.Errorf("save backup record failed, err: %v", err)
}
return nil
}