2023-07-19 08:25:41 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/mysql/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MysqlClient interface {
|
|
|
|
Create(info client.CreateInfo) error
|
|
|
|
Delete(info client.DeleteInfo) error
|
|
|
|
|
|
|
|
ChangePassword(info client.PasswordChangeInfo) error
|
|
|
|
ChangeAccess(info client.AccessChangeInfo) error
|
|
|
|
|
2023-07-27 06:32:23 +00:00
|
|
|
Backup(info client.BackupInfo) error
|
2023-07-25 09:08:13 +00:00
|
|
|
Recover(info client.RecoverInfo) error
|
|
|
|
|
2023-07-27 08:07:27 +00:00
|
|
|
SyncDB(version string) ([]client.SyncDBInfo, error)
|
2023-07-19 08:25:41 +00:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMysqlClient(conn client.DBInfo) (MysqlClient, error) {
|
2023-07-20 09:51:57 +00:00
|
|
|
if conn.From == "local" {
|
|
|
|
if cmd.CheckIllegal(conn.Address, conn.Username, conn.Password) {
|
2023-07-19 08:25:41 +00:00
|
|
|
return nil, buserr.New(constant.ErrCmdIllegal)
|
|
|
|
}
|
2023-07-21 10:28:45 +00:00
|
|
|
connArgs := []string{"exec", conn.Address, "mysql", "-u" + conn.Username, "-p" + conn.Password, "-e"}
|
2023-07-27 08:07:27 +00:00
|
|
|
return client.NewLocal(connArgs, conn.Address, conn.Password, conn.From), nil
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
2023-07-21 10:28:45 +00:00
|
|
|
|
|
|
|
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", conn.Username, conn.Password, conn.Address, conn.Port)
|
|
|
|
db, err := sql.Open("mysql", connArgs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-25 09:08:13 +00:00
|
|
|
return client.NewRemote(client.Remote{
|
|
|
|
Client: db,
|
2023-07-27 08:07:27 +00:00
|
|
|
From: conn.From,
|
2023-07-25 09:08:13 +00:00
|
|
|
User: conn.Username,
|
|
|
|
Password: conn.Password,
|
|
|
|
Address: conn.Address,
|
|
|
|
Port: conn.Port,
|
|
|
|
}), nil
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|