You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.3 KiB
127 lines
3.3 KiB
package dbc
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"recook/configs"
|
|
"time"
|
|
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/jinzhu/gorm"
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
)
|
|
|
|
type MysqlConfig struct {
|
|
Dsn string `toml:"dsn"`
|
|
ReadDSN []string `toml:"readDSN"`
|
|
Active int `toml:"active"`
|
|
Idle int `toml:"idle"`
|
|
}
|
|
|
|
var (
|
|
DB *gorm.DB
|
|
)
|
|
|
|
func SetupMysql() {
|
|
c := &MysqlConfig{}
|
|
|
|
if configs.IsProductionEnv() {
|
|
//c = &MysqlConfig{
|
|
// Dsn: "db_recook_super:sduEb9MTSiqhweXGpxa0@tcp(rm-uf6d9yr967ew7yxhn.mysql.rds.aliyuncs.com:3306)/db_recook?timeout=3s&readTimeout=3s&writeTimeout=3s&parseTime=true&loc=Local&charset=utf8mb4,utf8",
|
|
// Idle: 10,
|
|
// Active: 50,
|
|
//}
|
|
//rm-uf6qb1jym26a044z03o.mysql.rds.aliyuncs.com
|
|
//rm-uf6qb1jym26a044z0.mysql.rds.aliyuncs.com内网
|
|
c = &MysqlConfig{
|
|
Dsn: "db_recook_super:sduEb9MTSiqhweXGpxa0@tcp(rm-uf650e6f4439fxw54.mysql.rds.aliyuncs.com:3306)/db_recook?timeout=3s&readTimeout=3s&writeTimeout=3s&parseTime=true&loc=Local&charset=utf8mb4,utf8",
|
|
Idle: 10,
|
|
Active: 500,
|
|
}
|
|
log.Println("当前为正式数据库")
|
|
} else {
|
|
|
|
c = &MysqlConfig{
|
|
Dsn: fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?timeout=3s&readTimeout=3s&writeTimeout=3s&parseTime=true&loc=Local&charset=utf8mb4,utf8",
|
|
configs.GetEnv("mysql-username", "db_recook_v2"),
|
|
configs.GetEnv("mysql-password", "H6568h4Y7BDsWcYe"),
|
|
configs.GetEnv("mysql-host", "127.0.0.1"),
|
|
configs.GetEnv("mysql-dbname", "db_recook_v2"),
|
|
),
|
|
Idle: 10,
|
|
Active: 500,
|
|
}
|
|
log.Println("当前为测试数据库")
|
|
|
|
//c = &MysqlConfig{
|
|
// Dsn: "root:root@tcp(127.0.0.1:3306)/recook?timeout=3s&readTimeout=3s&writeTimeout=3s&parseTime=true&loc=Local&charset=utf8mb4,utf8",
|
|
// Idle: 10,
|
|
// Active: 50,
|
|
//}
|
|
//log.Println("当前为本地数据库")
|
|
|
|
}
|
|
|
|
db, err := gorm.Open("mysql", c.Dsn)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
db.DB().SetMaxIdleConns(c.Idle)
|
|
db.DB().SetMaxOpenConns(c.Active)
|
|
db.DB().SetConnMaxLifetime(4 * time.Hour)
|
|
|
|
if configs.IsProductionEnv() {
|
|
db.LogMode(true)
|
|
DB = db
|
|
} else {
|
|
db.LogMode(true)
|
|
DB = db.Debug()
|
|
DB = db
|
|
}
|
|
SetupNewMysql()
|
|
log.Println("mysql初始化成功")
|
|
}
|
|
|
|
func SetupNewMysql() {
|
|
if configs.IsProductionEnv() {
|
|
err := mysql2.InitMysql(&mysql2.DbConfig{
|
|
Username: "db_recook_super",
|
|
Password: "sduEb9MTSiqhweXGpxa0",
|
|
Host: "rm-uf650e6f4439fxw54.mysql.rds.aliyuncs.com",
|
|
Port: 3306,
|
|
Extend: "timeout=10s&readTimeout=10s&writeTimeout=10s&parseTime=true&loc=Local&charset=utf8mb4,utf8",
|
|
LogLevel: 4,
|
|
MaxIdleConns: 10,
|
|
MaxOpenConns: 500,
|
|
DbName: "db_recook",
|
|
})
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
db, err := mysql2.Db.DB()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
db.SetConnMaxLifetime(43200)
|
|
} else {
|
|
err := mysql2.InitMysql(&mysql2.DbConfig{
|
|
Username: configs.GetEnv("mysql-username", "db_recook_v2"),
|
|
Password: configs.GetEnv("mysql-password", "H6568h4Y7BDsWcYe"),
|
|
Host: configs.GetEnv("mysql-host", "127.0.0.1"),
|
|
Port: 3306,
|
|
Extend: "timeout=30s&readTimeout=30s&writeTimeout=30s&parseTime=true&loc=Local&charset=utf8mb4,utf8",
|
|
LogLevel: 4,
|
|
MaxIdleConns: 10,
|
|
MaxOpenConns: 500,
|
|
DbName: configs.GetEnv("mysql-dbname", "db_recook_v2"),
|
|
})
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|