Mysql Too many connections 和 数据库堵塞问题排查处理

一、问题描述

        今天突然接到个问题,Mysql内存占用90%多,后来发现有问题就查找到Max_used_connections到达顶峰

二、处理方法

1.查询连接情况,【定位堵塞的线程执行的SQL的time字段,time列:此这个状态持续的时间,单位是秒。】

如果有 SUPER 权限,则可以看到全部的线程,否则,只能看到自己发起的线程(这是指,当前对应的 MySQL 帐户运行的线程)

参数文献参考:mysql show processlist命令 详解  和 https://blog.youkuaiyun.com/dhfzhishi/article/details/81263084

 

root@localhost > show processlist;

…...
1001 rows in set (0.00 sec)
root@localhost > show variables like '%proces%';
Empty set (0.00 sec)

2.检查参数

root@localhost > show global status like 'Max_used_connections';

+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 1001 |
+----------------------+-------+
1 row in set (0.00 sec)

3.通过命令生成杀进程脚本

root@localhost > select concat('KILL ',id,';') from information_schema.processlist where user=’sam' into outfile '/tmp/a.txt
脚本内容如下:

+------------------------+

| concat('KILL ',id,';') |
+------------------------+
| KILL 31964612; |
| KILL 31964609; |
| KILL 31964611; |
…...
| KILL 31966619; |
| KILL 31966620; |
+------------------------+
991 rows in set (0.02 sec)
root@localhost >

4.执行上面生成的KILL脚本

root@localhost > source /tmp/a.txt

Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
……
 
5.检查连接状况,恢复正常

root@localhost > show processlist;

6.修改Max_used_connections参数(注:记得要修改my.cnf文件,下次重启动后仍然有效)

mysql> set GLOBAL max_connections=2000;

Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2000 |
+-----------------+-------+
1 row in set (0.00 sec)

此问题背后肯定存在着某些问题,不要只是一味地调大参数。

遇见”MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务器分散读压力;

另一种原因就是MySQL配置文件中max_connections值过小。

 

 

 

#######################################################

首先,我们来查看mysql的最大连接数:

复制代码

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name  | Value |
+-----------------+-------+
| max_connections | 151  |
+-----------------+-------+
1 row in set (0.00 sec)

复制代码

其次,查看服务器响应的最大连接数:

复制代码

mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name    | Value |
+----------------------+-------+
| Max_used_connections | 2   |
+----------------------+-------+
1 row in set (0.00 sec)

复制代码

可以看到服务器响应的最大连接数为2,远远低于mysql服务器允许的最大连接数值。

对于mysql服务器最大连接数值的设置范围比较理想的是:服务器响应的最大连接数值占服务器上限连接数值的比例值在10%以上,如果在10%以下,说明mysql服务器最大连接上限值设置过高。

Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1%

我们可以看到占比远低于10%(因为这是本地测试服务器,结果值没有太大的参考意义,大家可以根据实际情况设置连接数的上限值)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值