您的位置:首页 > 数据库 > MySQL

Mysql 解决“too many connections”的过程学习记录

2016-12-22 15:42 435 查看
The palest ink is better than best memory——好记性不如烂笔头。2013补记

“博学之,审问之,慎思之,明辨之,笃行之。有弗学,学之弗能,弗措也;有弗问,问之弗知,弗措也;有弗思,思之弗得,弗措也;有弗辨,辨之弗明,弗措也;有弗行,行之弗笃,弗措也。人一能之,己百之;人十能之,己千之。果能此道矣,虽愚必明,虽柔必强。”《中庸》

一、问题:

使用mysql过程中,时而会出现:

Can not connect to MySQL server

Error: Too many connections
Errno.: 1040

Similar error report has beed dispatched to administrator before.


二、排查问题:

1.看看官方手册说明:http://dev.mysql.com/doc/refman/5.7/en/too-many-connections.html

If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients.

The number of connections permitted is controlled by the max_connections system variable. The default value is 151 to improve performance when MySQL is used with the Apache Web server. (Previously, the default was 100.) If you need to support more connections, you should set a larger value for this variable.

2.以及max_connections参数说明:

http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_connections



3.通常一般会直接修改max_connections参数,但是没找到真正问题所在,往往治标不治本

4.这里最好仔细学习一下“How MySQL Opens and Closes Tables

5.了解服务器相关配置&运行状态:

SHOW VARIABLES LIKE '%max_connections%';#查看当前服务器配置的最多连接数A


show variables like '%wait_timeout%';#等待连接的超时配置


SHOW GLOBAL STATUS LIKE '%max_used_connections%';#查看当前已使用最大连接数B(已报错,so 肯定 B > A)


SHOW FULL PROCESSLIST;#通常发现某一条慢SQL大量阻塞,找出来优化它


KILL <id>;#临时紧急杀掉


三、结论:

1.连接数超过了 MySQL 设置的值,与 max_connectionswait_timeout 都有关系。wait_timeout 的值越大,连接的空闲等待就越长,这样就会造成当前连接数越大

2.排除业务访问量真的增长情况,通常多半都是应用程序(如,数据库连接池配置不当;数据库连接使用后未及时释放关闭)或慢SQL导致
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: