Too many connections解决方法

在工作中,大家或许常常遇到Too many connections这个错误,这时作为DBA想进数据库管理都进不去,是非常尴尬的一件事情。当然有同学说可以修改配置文件,但是修改配置文件是需要重启mysqld的,这在业务繁忙的数据库服务器上是不允许的。所以紧急情况下可以采用如下的方法,比如下面的测试。

[root@mysql-server-01 msb_5_6_19]# ./use 
ERROR 1040 (HY000): Too many connections
[root@mysql-server-01 msb_5_6_19]# 

我上面是采用MySQL沙箱环境,关于沙箱环境的简单安装及使用请参看我前面的文章。MySQL Sandbox安装使用
可以看见我上面已经报了错误,提示也非常明显,就是我们配置的连接数太小,现在已经用完了,这时我们刚想进数据库做些操作,那么采用如下方法:

[root@mysql-server-01 ~]# gdb -p $(cat /root/sandboxes/msb_5_6_19/data/mysql_sandbox5619.pid) -ex "set max_connections=500" -batch  
[New LWP 27541]
[New LWP 27540]
[New LWP 27539]
[Thread debugging using libthread_db enabled]
0x00000031152df343 in poll () from /lib64/libc.so.6 [root@mysql-server-01 ~]#

下面再次登录数据库看看,并查看最大连接数是否已经修改

[root@mysql-server-01 msb_5_6_19]# ./use 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 22
Server version: 5.6.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql [localhost] {msandbox} ((none)) > show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 500   |
+-----------------+-------+
1 row in set (0.00 sec)

mysql [localhost] {msandbox} ((none)) > 

在Percona5.5的thread_pool里面提供了2个参数extra_port和extra_max_connections预留额外的连接,预防连接满了以后我们无法进入数据库进行相应的管理。

root@localhost : (none) 23:18:00> show variables like '%extra%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| extra_max_connections | 1     |
| extra_port            | 10086 |
+-----------------------+-------+
2 rows in set (0.00 sec)

root@localhost : (none) 23:18:04> 
[root@mysql-server-01 user_3307]# netstat -nltp | grep 10086
tcp        0      0 0.0.0.0:10086               0.0.0.0:*                   LISTEN      29655/mysqld        
[root@mysql-server-01 user_3307]# 

我这里使用了10086端口,以及最大连接数为1。有这么贴心的功能。必须给一个赞

总结:

通常控制最大连接数有两个参数控制max_connections(该实例允许最大的连接数 ),max_user_connections(该实例允许每个用户的最大连接数),通常情况下前期我们就需要规划好多少连接数合适。一般情况下建议不要超过300。因为MySQL在连接数上升的情况下性能下降非常厉害,如果需要大量连接,这时可以引入thread_pool。所以我们需要保持一个原则:系统创建的用户(给应用使用用户)数* max_user_connections  < max_connections。这样就不会发生文章开始说的问题。

 

参考资料

http://www.mysqlperformanceblog.com/2010/03/23/too-many-connections-no-problem/

http://www.percona.com/doc/percona-server/5.5/performance/threadpool.html

原文地址:https://www.cnblogs.com/gomysql/p/3834797.html