package util
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
func init() {
database := beego.AppConfig.String("database")
host := beego.AppConfig.String("host")
userName := beego.AppConfig.String("username")
password := beego.AppConfig.String("password")
port := beego.AppConfig.String("port")
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", userName, password, host, port, database)
orm.RegisterDriver("mysql", orm.DRMySQL)
err := orm.RegisterDataBase("default", "mysql", dataSource)
orm.SetMaxOpenConns("default",30)
orm.SetMaxIdleConns("default",30)
if err != nil {
fmt.Println("数据库连接出错" + err.Error())
return
}
fmt.Println("数据库连接成功")
}
|