/
mysql.go
57 lines (48 loc) · 1.11 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package drive
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"time"
)
type Mysql struct {
Db *gorm.DB
}
func InitMysql() {
cfg := G_conf.Cfg
mysql_url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true",
cfg.Section("mysql").Key("username").String(),
cfg.Section("mysql").Key("password").String(),
cfg.Section("mysql").Key("host").String(),
cfg.Section("mysql").Key("port").String(),
cfg.Section("mysql").Key("database").String(),
cfg.Section("mysql").Key("charset").String(),
)
mysql_url += "&loc=Local"
db, err := gorm.Open("mysql", mysql_url)
if err != nil {
Err(logrus.Fields{}, errors.Wrap(err, "数据库链接失败!"))
}
max_count, err := cfg.Section("mysql").Key("max_count").Int()
if err != nil {
max_count = 1
}
db.DB().SetMaxOpenConns(max_count)
db.DB().SetMaxIdleConns(10)
if err := db.DB().Ping(); err != nil {
panic(err)
}
G_mysql = db
//go dbHear()
}
func dbHear() {
for {
if G_mysql.DB().Ping() != nil {
InitMysql()
} else {
time.Sleep(2 * time.Minute)
}
}
}