2023-07-19 16:25:41 +08:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2023-08-11 22:20:15 +08:00
|
|
|
"context"
|
2023-07-19 16:25:41 +08:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-11-07 14:09:28 +08:00
|
|
|
"strings"
|
2023-08-11 22:20:15 +08:00
|
|
|
"time"
|
2023-07-19 16:25:41 +08:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"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 14:32:23 +08:00
|
|
|
Backup(info client.BackupInfo) error
|
2023-07-25 17:08:13 +08:00
|
|
|
Recover(info client.RecoverInfo) error
|
|
|
|
|
2023-07-27 16:07:27 +08:00
|
|
|
SyncDB(version string) ([]client.SyncDBInfo, error)
|
2023-07-19 16:25:41 +08:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMysqlClient(conn client.DBInfo) (MysqlClient, error) {
|
2023-07-20 17:51:57 +08:00
|
|
|
if conn.From == "local" {
|
2023-07-21 18:28:45 +08:00
|
|
|
connArgs := []string{"exec", conn.Address, "mysql", "-u" + conn.Username, "-p" + conn.Password, "-e"}
|
2023-08-31 17:22:12 +08:00
|
|
|
return client.NewLocal(connArgs, conn.Address, conn.Password, conn.Database), nil
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|
2023-07-21 18:28:45 +08:00
|
|
|
|
2023-11-07 14:09:28 +08:00
|
|
|
if strings.Contains(conn.Address, ":") {
|
|
|
|
conn.Address = fmt.Sprintf("[%s]", conn.Address)
|
|
|
|
}
|
2023-11-29 10:46:07 +08:00
|
|
|
|
|
|
|
tlsItem, err := client.ConnWithSSL(conn.SSL, conn.SkipVerify, conn.ClientKey, conn.ClientCert, conn.RootCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8%s", conn.Username, conn.Password, conn.Address, conn.Port, tlsItem)
|
2023-07-21 18:28:45 +08:00
|
|
|
db, err := sql.Open("mysql", connArgs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-11 22:20:15 +08:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conn.Timeout)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := db.PingContext(ctx); err != nil {
|
2023-07-21 18:28:45 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-11 22:20:15 +08:00
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return nil, buserr.New(constant.ErrExecTimeOut)
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:08:13 +08:00
|
|
|
return client.NewRemote(client.Remote{
|
|
|
|
Client: db,
|
2023-08-31 17:22:12 +08:00
|
|
|
Database: conn.Database,
|
2023-07-25 17:08:13 +08:00
|
|
|
User: conn.Username,
|
|
|
|
Password: conn.Password,
|
|
|
|
Address: conn.Address,
|
|
|
|
Port: conn.Port,
|
2023-11-29 10:46:07 +08:00
|
|
|
|
|
|
|
SSL: conn.SSL,
|
|
|
|
RootCert: conn.RootCert,
|
|
|
|
ClientKey: conn.ClientKey,
|
|
|
|
ClientCert: conn.ClientCert,
|
|
|
|
SkipVerify: conn.SkipVerify,
|
2023-07-25 17:08:13 +08:00
|
|
|
}), nil
|
2023-07-19 16:25:41 +08:00
|
|
|
}
|