proxymysql/app/db/db.go

63 lines
1.1 KiB
Go
Raw Normal View History

2024-02-04 18:17:06 +08:00
package db
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)
var (
db *gorm.DB
)
func InitAdminDb() {
2024-03-01 16:21:28 +08:00
dsn := `admin1:XUztIJWW!sr6bJqWK85CJ7kN@tcp(172.28.16.6:3306)/new_admin?charset=utf8&parseTime=true&loc=Asia%2FShanghai` // 保存sql记录的数据库连接
2024-02-04 18:17:06 +08:00
var err error
db, err = gorm.Open(mysql.Open(dsn))
if err != nil {
panic(err)
}
sqlDb, _ := db.DB()
sqlDb.SetConnMaxIdleTime(time.Hour)
sqlDb.SetMaxIdleConns(4)
err = sqlDb.Ping()
if err != nil {
panic(err)
}
//initTable()
//db = db.Debug()
}
func GetDB() *gorm.DB {
return db
}
var (
logTable = `CREATE TABLE if not exists sql_query_log (
id integer PRIMARY KEY AUTOINCREMENT,
admin_id integer not null,
admin_name text not null,
admin_real_name text not null,
query_game_id integer,
header_game_id integer,
ip text,
request_path text,
request_info text,
unix_milli integer,
query text,
create_time datetime default current_timestamp not null
)
`
)
func initTable() {
if err := db.Exec(logTable).Error; err != nil {
panic(err)
}
}