1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-20 00:39:17 +08:00
1Panel/backend/init/db/mysql.go
2022-10-18 11:30:02 +08:00

29 lines
563 B
Go

package db
import (
"github.com/1Panel-dev/1Panel/backend/global"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func MysqlGorm() *gorm.DB {
m := global.CONF.Mysql
if m.Dbname == "" {
return nil
}
mysqlConfig := mysql.Config{
DSN: m.Dsn(),
DefaultStringSize: 191,
SkipInitializeWithVersion: false,
}
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{}); err != nil {
panic(err)
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
return db
}
}