package db import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" "background/config" ) var gDb Database var blogDb Database func Init() { mysqlconf := config.GetMysqlConfig() blogConf := config.GetMysqlBlogConfig() gDb = Database{Type: string(""), DB: initMysql(mysqlconf)} blogDb = Database{Type: string(""), DB: initMysql(blogConf)} } func initMysql(mysqlconf *config.MysqlConfig) *sql.DB { cnn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password, mysqlconf.Addr, mysqlconf.Db) _db, err := sql.Open("mysql", cnn) if err != nil { fmt.Println("connect sql server ", err.Error()) } e := _db.Ping() if nil != e { fmt.Println(e.Error()) } return _db } func GetMysqlClient() *Database { return &gDb } func GetBlogMysql() *Database{ return &blogDb }