golang mysql 设置最大连接数

本文介绍golang 中连接MySQL时,如何设置最大连接数。

文中例子连接MySQL用的package是github.com/go-sql-driver/mysql.

设置最大连接数的接口是

func (db *DB) SetMaxOpenConns(n int) 

设置连接MySQL可以打开的最大连接数。

如果n <= 0,表示打开的连接数没有限制。

默认为0,也就是不限制连接数。

另一个与连接数相关的参数是MaxIdleConns,表示最大空闲连接数。

如果MaxIdleConns 大于0,且大于MaxOpenConns,最后MaxIdleConns将最后等于MaxOpenConns

1.测试例子

首先设置最大打开的连接数为1,接着开启20个goroutine,每个goroutine执行sql语句,并打印执行sql使用的连接的connection id。并执行耗时的sql语句占用连接,观察结果。

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

从输出结果可以看到,20个goroutine轮流使用一个连接(connection id 为55)执行sql语句。

2.查看连接

使用show processlist查看连接

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)

使用netstat 查看连接

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

可以看到,只建立了1个连接。

你可能感兴趣的:(golang mysql 设置最大连接数)