2024-01-12 13:15:30 +08:00
|
|
|
|
package dbi
|
2023-10-27 17:41:45 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
"fmt"
|
2024-01-05 22:16:38 +08:00
|
|
|
|
machineapp "mayfly-go/internal/machine/application"
|
2023-10-27 17:41:45 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
|
|
|
|
|
"mayfly-go/pkg/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
// 获取sql.DB函数
|
|
|
|
|
|
type GetSqlDbFunc func(*DbInfo) (*sql.DB, error)
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
type DbInfo struct {
|
2023-12-20 17:29:16 +08:00
|
|
|
|
InstanceId uint64 // 实例id
|
|
|
|
|
|
Id uint64 // dbId
|
|
|
|
|
|
Name string
|
2023-10-27 17:41:45 +08:00
|
|
|
|
|
|
|
|
|
|
Type DbType // 类型,mysql postgres等
|
|
|
|
|
|
Host string
|
|
|
|
|
|
Port int
|
|
|
|
|
|
Network string
|
|
|
|
|
|
Username string
|
|
|
|
|
|
Password string
|
|
|
|
|
|
Params string
|
|
|
|
|
|
Database string
|
|
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
TagPath []string
|
2023-10-27 17:41:45 +08:00
|
|
|
|
SshTunnelMachineId int
|
2024-01-12 13:15:30 +08:00
|
|
|
|
|
|
|
|
|
|
Meta Meta
|
2023-10-27 17:41:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取记录日志的描述
|
|
|
|
|
|
func (d *DbInfo) GetLogDesc() string {
|
|
|
|
|
|
return fmt.Sprintf("DB[id=%d, tag=%s, name=%s, ip=%s:%d, database=%s]", d.Id, d.TagPath, d.Name, d.Host, d.Port, d.Database)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 连接数据库
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (dbInfo *DbInfo) Conn(meta Meta) (*DbConn, error) {
|
|
|
|
|
|
if meta == nil {
|
|
|
|
|
|
return nil, errorx.NewBiz("数据库元信息接口不能为空")
|
2023-10-27 17:41:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
// 赋值Meta,方便后续获取dialect等
|
|
|
|
|
|
dbInfo.Meta = meta
|
|
|
|
|
|
database := dbInfo.Database
|
|
|
|
|
|
|
|
|
|
|
|
conn, err := meta.GetSqlDb(dbInfo)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
if err != nil {
|
2023-11-27 17:40:47 +08:00
|
|
|
|
logx.Errorf("连接db失败: %s:%d/%s, err:%s", dbInfo.Host, dbInfo.Port, database, err.Error())
|
2023-10-27 17:41:45 +08:00
|
|
|
|
return nil, errorx.NewBiz(fmt.Sprintf("数据库连接失败: %s", err.Error()))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = conn.Ping()
|
|
|
|
|
|
if err != nil {
|
2023-11-27 17:40:47 +08:00
|
|
|
|
logx.Errorf("db ping失败: %s:%d/%s, err:%s", dbInfo.Host, dbInfo.Port, database, err.Error())
|
2023-10-27 17:41:45 +08:00
|
|
|
|
return nil, errorx.NewBiz(fmt.Sprintf("数据库连接失败: %s", err.Error()))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dbc := &DbConn{Id: GetDbConnId(dbInfo.Id, database), Info: dbInfo}
|
|
|
|
|
|
|
|
|
|
|
|
// 最大连接周期,超过时间的连接就close
|
|
|
|
|
|
// conn.SetConnMaxLifetime(100 * time.Second)
|
|
|
|
|
|
// 设置最大连接数
|
|
|
|
|
|
conn.SetMaxOpenConns(5)
|
|
|
|
|
|
// 设置闲置连接数
|
|
|
|
|
|
conn.SetMaxIdleConns(1)
|
|
|
|
|
|
dbc.db = conn
|
|
|
|
|
|
logx.Infof("连接db: %s:%d/%s", dbInfo.Host, dbInfo.Port, database)
|
|
|
|
|
|
|
|
|
|
|
|
return dbc, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-05 22:16:38 +08:00
|
|
|
|
// 如果使用了ssh隧道,将其host port改变其本地映射host port
|
|
|
|
|
|
func (di *DbInfo) IfUseSshTunnelChangeIpPort() error {
|
|
|
|
|
|
// 开启ssh隧道
|
|
|
|
|
|
if di.SshTunnelMachineId > 0 {
|
|
|
|
|
|
sshTunnelMachine, err := machineapp.GetMachineApp().GetSshTunnelMachine(di.SshTunnelMachineId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
exposedIp, exposedPort, err := sshTunnelMachine.OpenSshTunnel(fmt.Sprintf("db:%d", di.Id), di.Host, di.Port)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
di.Host = exposedIp
|
|
|
|
|
|
di.Port = exposedPort
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 获取连接id
|
|
|
|
|
|
func GetDbConnId(dbId uint64, db string) string {
|
|
|
|
|
|
if dbId == 0 {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d:%s", dbId, db)
|
|
|
|
|
|
}
|