赞
踩
在使用MySQL的过程中,应用程序有时会碰到错误:
Error 1040 Too many connections
这个错误表示,已经达到MySQL的最大连接数。
什么是MySQL的最大连接数?
max_connections
是指MySQL 同时处理的连接的最大数量。
默认的最大连接数是151,
如果希望更大的连接数,可以将max_connections
调整为更大值。
但更大的连接数,也意味着,更大的内存占用。
实际上的最大连接数是max_connections+1
.
有一个连接是预留的,只有SUPER权限的用户才可以使用。
SUPER权限的用户可以使用这个连接,登录MySQL诊断问题。
更确切一些,是指MySQL同时处理的连接数量。这些连接,包括正在建立的连接,已建立的连接, 以及正在断开的连接。
已通过简单demo验证。基本思路是,在出现"Too many connections"错误的时候,看下有多少连接已建立。数据测试显示,报错时,并未达到max_connections。
以下是测试代码, 首先使用普通用户连接,尝试不断建立连接,当出现"Too many connections"错误的时候,使用超级权限用户查询当前的连接数,打印已经建立的连接数。
package main import ( "database/sql" "log" "sync" _ "github.com/go-sql-driver/mysql" ) var superDB *sql.DB var superDataBase = "root:Aa123456@tcp(127.0.0.1:3306)/?loc=Local&parseTime=true&timeout=1s&readTimeout=6s" var DB *sql.DB var dataBase = "orchestrator:orch_backend_password@tcp(127.0.0.1:3306)/?loc=Local&parseTime=true&timeout=1s&readTimeout=6s" func Init(dataBase string) *sql.DB { db, err := sql.Open("mysql", dataBase) if err != nil { log.Fatalln("open db fail:", err) } // max idle db.SetMaxIdleConns(1000) err = db.Ping() if err != nil { log.Fatalln("ping db fail:", err) } return db } func main() { superDB = Init(superDataBase) DB = Init(dataBase) for i:=0; i < 1000; i++ { go one_worker() } select { } } func one_worker() { for { var connectionId int err := DB.QueryRow("select CONNECTION_ID()").Scan(&connectionId) if err != nil { log.Println("query connection id failed:", err) if err.Error() == "Error 1040: Too many connections"{ var varName string var threadsConnected int err := superDB.QueryRow("show status like 'Threads_connected'").Scan(&varName, &threadsConnected) if err != nil { log.Println("show threads connected failed:", err) } log.Println("threads connected:", threadsConnected) panic("too many....") } } } }
日志输出:
... ... [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52921->127.0.0.1:3306: read: connection reset by peer [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52893->127.0.0.1:3306: read: connection reset by peer [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52954->127.0.0.1:3306: read: connection reset by peer 2020/04/05 21:48:02 query connection id failed: Error 1040: Too many connections [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52885->127.0.0.1:3306: read: connection reset by peer [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52876->127.0.0.1:3306: read: connection reset by peer 2020/04/05 21:48:02 query connection id failed: Error 1040: Too many connections [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52927->127.0.0.1:3306: read: connection reset by peer 2020/04/05 21:48:02 query connection id failed: Error 1040: Too many connections [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52905->127.0.0.1:3306: read: connection reset by peer 2020/04/05 21:48:02 query connection id failed: Error 1040: Too many connections [mysql] 2020/04/05 21:48:02 packets.go:36: read tcp 127.0.0.1:52940->127.0.0.1:3306: read: connection reset by peer 2020/04/05 21:48:02 threads connected: 145 panic: too many.... goroutine 1111 [running]: main.one_worker() /Users/lanyang/workspace/mysql_example/mysql_max_connections.go:69 +0x369 created by main.main /Users/lanyang/workspace/mysql_example/mysql_max_connections.go:47 +0xa4 exit status 2
从上面的输出可以看到,出现错误"Too many connections"时, 当前已经建立的连接为145, 是小于152(151+1)的。
最大连接数的查看
mysql> show variables like "max_connections";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.08 sec)
最大连接数的修改
使用超级用户登录,进行修改:
SET GLOBAL max_connections = 300;
这个修改只是临时的,下次MySQL重启后,又恢复默认值。
配置文件中修改,永久生效:
vi /etc/my.cnf
[mysqld]
max_connections = 300
修改配置后,重启MySQL Server后生效。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。