This article describes golang in connection MySQL, how to set the maximum number of connections.
An example is given package MySQL connection with the github.com/go-sql-driver/mysql
.
Set the interface maximum number of connections is
func (db *DB) SetMaxOpenConns(n int)
Sets the maximum number of connections MySQL can be opened.
If n <= 0, represents the number of open connections is not limited.
The default is 0, that is, does not limit the number of connections.
Another relevant parameter is the connection number MaxIdleConns
indicates the maximum number of idle connections.
If MaxIdleConns
greater than 0, and greater than MaxOpenConns
, and finally MaxIdleConns
the last equal MaxOpenConns
.
1. Test Examples
First, the maximum number of connections is provided for opening 1, and then turned 20 goroutine, each goroutine sql statement execution, and connected to the print execution sql connection id used. And time-consuming sql statement occupancy connection, observe the results.
package main
import (
"database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
)
var DB *sql.DB
var dataBase = "root:Aa123456@tcp(127.0.0.1:3306)/?loc=Local&parseTime=true"
func Init() {
var err error
DB, err = sql.Open("mysql", dataBase)
if err != nil {
log.Fatalln("open db fail:", err)
}
DB.SetMaxOpenConns(1)
err = DB.Ping()
if err != nil {
log.Fatalln("ping db fail:", err)
}
}
func main() {
Init()
for {
for i:=0; i < 20; i++ {
go one_worker(i)
}
select {
}
}
}
func one_worker(i int) {
var connection_id int
err := DB.QueryRow("select CONNECTION_ID()").Scan(&connection_id)
if err != nil {
log.Println("query connection id failed:", err)
return
}
log.Println("worker:", i, ", connection id:", connection_id)
var result int
err = DB.QueryRow("select sleep(10)").Scan(&result)
if err != nil {
log.Println("query sleep connection id faild:", err)
return
}
}
output
2019/10/02 18:14:25 worker: 2 , connection id: 55
2019/10/02 18:14:25 worker: 17 , connection id: 55
2019/10/02 18:14:25 worker: 11 , connection id: 55
2019/10/02 18:14:35 worker: 3 , connection id: 55
2019/10/02 18:14:45 worker: 0 , connection id: 55
2019/10/02 18:14:45 worker: 4 , connection id: 55
2019/10/02 18:14:45 worker: 5 , connection id: 55
2019/10/02 18:15:05 worker: 7 , connection id: 55
2019/10/02 18:15:25 worker: 15 , connection id: 55
2019/10/02 18:15:25 worker: 6 , connection id: 55
2019/10/02 18:15:35 worker: 13 , connection id: 55
2019/10/02 18:15:45 worker: 19 , connection id: 55
2019/10/02 18:15:45 worker: 10 , connection id: 55
2019/10/02 18:15:45 worker: 12 , connection id: 55
2019/10/02 18:15:55 worker: 14 , connection id: 55
2019/10/02 18:16:15 worker: 8 , connection id: 55
2019/10/02 18:16:35 worker: 18 , connection id: 55
2019/10/02 18:16:35 worker: 1 , connection id: 55
2019/10/02 18:17:05 worker: 16 , connection id: 55
2019/10/02 18:17:35 worker: 9 , connection id: 55
Can be seen from the output, goroutine 20 turns using a connection (connection id as 55) perform sql statement.
2. Check the connection
Use show processlist view connection
mysql> show processlist;
+----+------+-----------------+------+---------+------+------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------+------------------+
| 20 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 55 | root | localhost:59518 | NULL | Query | 5 | User sleep | select sleep(10) |
+----+------+-----------------+------+---------+------+------------+------------------+
2 rows in set (0.00 sec)
Use netstat see the connection
netstat -an | grep 3306
tcp4 0 0 127.0.0.1.3306 127.0.0.1.59518 ESTABLISHED
tcp4 0 0 127.0.0.1.59518 127.0.0.1.3306 ESTABLISHED
tcp46 0 0 *.3306 *.* LISTEN
It can be seen only established a connection.