github.com/jtzjtz/kit@v1.0.2/conn/mysql_pool/mysql_pool.go (about) 1 package mysql_pool 2 3 import ( 4 "fmt" 5 _ "github.com/jinzhu/gorm/dialects/mysql" 6 "gorm.io/driver/mysql" 7 "gorm.io/gorm" 8 ) 9 10 func NewMySqlPool(o *Options) (*gorm.DB, error) { 11 if err := o.validate(); err != nil { 12 return nil, err 13 } 14 dbConnStr := getDbConnStr(o.User, o.Pass, o.DataBase, o.Host, o.Port) 15 DB, err := gorm.Open(mysql.Open(dbConnStr)) 16 //DB, err := gorm.Open("mysql", dbConnStr) 17 if err != nil { 18 fmt.Printf("mysql connection err=%v\n", err) 19 } 20 mysqlDB, err := DB.DB() 21 mysqlDB.SetMaxIdleConns(o.MaxCap) 22 mysqlDB.SetMaxIdleConns(o.InitCap) 23 24 return DB, nil 25 } 26 27 func getDbConnStr(user, pass, db, host, port string) string { 28 return user + ":" + pass + "@(" + host + ":" + port + ")/" + db + "?charset=utf8&parseTime=True&loc=Local" 29 }