转到版本 1.9.2
go-sql-driver/mysql git commit hash cd4cb90
mysql服务器版本:5.6.15-log MySQL Community Server
操作系统版本:CentOS release 6.7 (Final)
db 打开配置
max_idle_conns = 5
max_open_conns = 30
max_life_time=600
超时=600
mysql配置
+-----------------------------+----------+
| Variable_name | Value |
+-----------------------------+----------+
| connect_timeout | 60 |
| delayed_insert_timeout | 300 |
| interactive_timeout | 600 |
| lock_wait_timeout | 31536000 |
| log_output | FILE |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| wait_timeout | 600 |
+-----------------------------+----------+
lsof 输出
srv_promo 12672 root 10u sock 0,6 0t0 63382668 无法识别协议
srv_promo 12672 root 11u sock 0,6 0t0 63366850 无法识别协议
srv_promo 12672 root 12u sock 0,6 0t0 63366688 无法识别协议
srv_promo 12672 root 13u sock 0,6 0t0 63366690 无法识别协议
下面是代码:
包 dbtest
import (
"database/sql"
"fmt"
"github.com/golang/glog"
"gopkg.in/gorp.v2"
"log"
"os"
"sync"
"time"
)
type DatabaseConfig struct {
DBName string `toml:"dbname"`
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
Sslmode string `toml:"sslmode"`
ShowLog bool
DataSaveDir string
DataFileSaveLoopSize int
MaxIdleConns int `toml:"max_idle_conns"`
MaxOpenConns int `toml:"max_open_conns"`
MaxLifeTime int `toml:"max_life_time"`
Timeout int `toml:"timeout"`
RTimeout int `toml:"rtimeout"`
WTimeout int `toml:"wtimeout"`
}
func (c DatabaseConfig) MySQLSource() string {
params := make(map[string]string, 0)
params["charset"] = "utf8mb4"
cfg := mysql.Config{}
cfg.User = c.User
cfg.Passwd = c.Password
cfg.DBName = c.DBName
cfg.ParseTime = true
cfg.Collation = "utf8mb4_unicode_ci"
cfg.Params = params
cfg.Loc, _ = time.LoadLocation("Asia/Chongqing")
cfg.Timeout = time.Duration(c.Timeout) * time.Second
cfg.MultiStatements = true
cfg.ReadTimeout = time.Duration(c.RTimeout) * time.Second
cfg.WriteTimeout = time.Duration(c.WTimeout) * time.Second
return cfg.FormatDSN()
}
var (
dbmap *gorp.DbMap
Dbm *gorp.DbMap
config DatabaseConfig
opened bool
openMutex sync.RWMutex
DB *sql.DB
)
//Open open the database for passport with config
func Open(cfg DatabaseConfig) {
if !opened {
config = cfg
db, err := sql.Open("mysql", config.MySQLSource())
glog.Infof("open err %v ", err)
if err != nil {
panic(fmt.Errorf("sql.Open failed: %v", err))
}
if config.MaxLifeTime > 0 {
db.SetConnMaxLifetime(time.Duration(config.MaxLifeTime) * time.Second)
}
db.SetMaxIdleConns(config.MaxIdleConns)
db.SetMaxOpenConns(config.MaxOpenConns)
db.Ping()
DB = db
// construct a gorp DbMap
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "utf8mb4"}}
Dbm = dbmap
err = dbmap.CreateTablesIfNotExists()
if err != nil {
panic("create table failed " + err.Error())
}
openMutex.Lock()
opened = true
openMutex.Unlock()
if config.ShowLog {
dbmap.TraceOn("[gorp]", log.New(os.Stdout, schemaName+" ", log.Lmicroseconds))
}
}
}
//Close close the database for passport
func Close() {
if dbmap != nil && opened {
glog.Infof("close database %s for %s", config.DBName, schemaName)
dbmap.Db.Close()
openMutex.Lock()
opened = false
openMutex.Unlock()
}
}