记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。
Mysql连接数过多,应急处理方法

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


二、处理方法
1.查询连接情况

  1. root@localhost > show processlist;

  2. ...

  3. 1001 rows in set (0.00 sec)

  4. root@localhost > show variables like '%proces%';

  5. Empty set (0.00 sec)


2.检查参数

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

  2. +----------------------+-------+

  3. | Variable_name | Value |

  4. +----------------------+-------+

  5. | Max_used_connections | 1001 |

  6. +----------------------+-------+

  7. 1 row in set (0.00 sec)


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

  1. root@localhost > select concat('KILL ',id,';') from information_schema.processlist where user=’sam' into outfile '/tmp/a.txt


脚本内容如下:

  1. +------------------------+

  2. | concat('KILL ',id,';') |

  3. +------------------------+

  4. | KILL 31964612; |

  5. | KILL 31964609; |

  6. | KILL 31964611; |

  7. ...

  8. | KILL 31966619; |

  9. | KILL 31966620; |

  10. +------------------------+

  11. 991 rows in set (0.02 sec)

  12. root@localhost >


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

  1. root@localhost > source /tmp/a.txt

  2. Query OK, 0 rows affected (0.00 sec)

  3. Query OK, 0 rows affected (0.00 sec)

  4. ……

5.检查连接状况,恢复正常

  1. root@localhost > show processlist;


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

  1. mysql> set GLOBAL max_connections=2000;

  2. Query OK, 0 rows affected (0.00 sec)


  3. mysql> show variables like '%max_connections%';

  4. +-----------------+-------+

  5. | Variable_name | Value |

  6. +-----------------+-------+

  7. | max_connections | 2000 |

  8. +-----------------+-------+

  9. 1 row in set (0.00 sec)



三、总结
    Mysql的参数学习之max_connections,一个控制连接数的参数。此问题背后肯定存在着某些问题,不要只是一味地调大参数。




转载请标明出处【Mysql连接数过多,应急处理方法】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论