mysql連線數優化 SetMaxIdleConns 最大空閒連線數
最大空閒連線數
Go\src\database\sql\sql.go
SetMaxIdleConns sets the maximum number of connections in the idle
connection pool.
If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.
If n <= 0, no idle connections are retained.
The default max idle connections is currently 2. This may change in
a future release
SetMaxOpenConns sets the maximum number of open connections to the database.
If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
MaxIdleConns, then MaxIdleConns will be reduced to match the new
MaxOpenConns limit.
If n <= 0, then there is no limit on the number of open connections.
The default is 0 (unlimited).
d, e := sql.Open(DBDriverName, DBDataSourceName)
if e != nil {
panic(e)
}
e = d.Ping()
if e != nil {
panic(e)
}
d.SetConnMaxIdleTime(60 * time.Minute)
d.SetMaxIdleConns(16)
d.SetMaxOpenConns(32)
u@u5:~/exe$ ss -atp | grep mysql | grep MyAPP |nl
1 ESTAB 0 0 10.123.1.45:47254 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=23))
2 ESTAB 0 0 10.123.1.45:47512 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=29))
3 ESTAB 0 130 10.123.1.45:47374 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=31))
4 ESTAB 0 0 10.123.1.45:46020 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=42))
5 ESTAB 0 0 10.123.1.45:46162 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=44))
6 ESTAB 0 0 10.123.1.45:45256 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=19))
7 ESTAB 0 0 10.123.1.45:45944 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=38))
8 ESTAB 0 0 10.123.1.45:45400 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=34))
9 ESTAB 0 0 10.123.1.45:49110 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=7))
10 ESTAB 0 0 10.123.1.45:49116 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=11))
11 ESTAB 0 0 10.123.1.45:46018 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=41))
12 ESTAB 0 0 10.123.1.45:45916 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=28))
13 ESTAB 0 0 10.123.1.45:45262 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=27))
14 ESTAB 0 0 10.123.1.45:49134 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=14))
15 ESTAB 0 0 10.123.1.45:47232 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=21))
16 ESTAB 0 0 10.123.1.45:47234 10.123.1.45:mysql users:(("MyAPP",pid=6719,fd=22))
u@u5:~/exe$
State Recv-Q Send-Q Local Address:Port Peer Address:Port