阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL连接数过多处理方法实例

188次阅读
没有评论

共计 1398 个字符,预计需要花费 4 分钟才能阅读完成。

一、问题描述

        今天突然接到个问题,网页报错:503 Service Temporarily Unavailable。经过查询发现是某个用户的连接超级多,已经将数据库连接占满。处理方案,即时杀掉堵塞的进程,之后可以扩大 max_connections 参数。

二、处理方法

1. 查询连接情况

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 的参数学习之 max_connections,一个控制连接数的参数。此问题背后肯定存在着某些问题,不要只是一味地调大参数。后来经过对语句的分析,最终此问题定位为安全部门在做安全测试,导致问题产生。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计1398字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中