解决Too many connections问题
有些人觉得,解决too many connections问题,灰非简单,down了mysql,修改my.cnf调大max_connections,好吧,你想法是没错的,这的确可以解决问题,但试问对于线上在跑的MySQL,你能随便down吗?嘻嘻,如果不行,只能用另外的方法了
一旦出现了too many connections错误,DBA或者运维人员已经连接不上MySQL去动态修改max_connections了,下面做实验来演示一下:
为了方便演示,我把max_connections调小到4:
mysql> set global max_connections=4; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'max_connec%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 10 | | max_connections | 4 | +--------------------+-------+ 2 rows in set (0.00 sec) mysql>
然后打开几个seesion连接mysql,当连接超出4个时,报以下错误:
[root ~]$ mysql -uroot -p123456 -S /data/mysql-5.5/mysql.sock ERROR 1040 (HY000): Too many connections
提示也非常明显,就是我们配置的连接数太小,现在已经用完了,这时我们只能通过hack的方法,用过gdb直接修改mysqld内存中max_connections的值,具体做法如下:
可能很人的系统环境上没有安装gdb,安装下即可:
[root ~]$ gdb -p $(cat /data/mysql-5.5/localhost.localdomain.pid) -ex "set max_connections=500" -batch -bash: gdb: command not found [root ~]$ yum install gdb -y
然后执行gdb -p $(cat /data/mysql-5.5/localhost.localdomain.pid) -ex "set max_connections=500" -batch; cat后面跟的是你当前mysql服务的pid文件,根据自己的存放指定即可,后面是修改mysqld内存中max_connections为多少
也可以这样查看自己的pid文件存放在那里:
mysql> show variables like '%pid%'; +---------------+-------------------------------------------+ | Variable_name | Value | +---------------+-------------------------------------------+ | pid_file | /data/mysql-5.5/localhost.localdomain.pid | +---------------+-------------------------------------------+ 1 row in set (0.00 sec)
[root ~]$ gdb -p $(cat /data/mysql-5.5/localhost.localdomain.pid) -ex "set max_connections=500" -batch [New LWP 17980] [New LWP 17948] [New LWP 17910] [New LWP 17878] [New LWP 17803] [Thread debugging using libthread_db enabled] 0x00007f49f1c82333 in poll () from /lib64/libc.so.6
这时已经可以连接mysql了,查看它的max_connections已经不再是之前的4了,而是500了
mysql -uroot -p123456 -S /data/mysql-5.5/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.5.40-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> show global variables like 'max_conn%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 10 | | max_connections | 500 | +--------------------+-------+ 2 rows in set (0.04 sec) mysql>
在Percona5.5的thread_pool里面提供了2个参数extra_port和extra_max_connections预留额外的连接,预防连接满了以后我们无法进入数据库进行相应的管理。
[root msb_5_5_40]$ ./use Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.40-36.1 Percona Server (GPL), Release 36.1, Revision 707 Copyright (c) 2009-2014 Percona LLC and/or its affiliates 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 ((none)) > show variables like '%extra%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | extra_max_connections | 1 | | extra_port | 10010 | +-----------------------+-------+ 2 rows in set (0.00 sec)
当出现超出最大链接的错误时,可以指定用户名 密码 端口登录,端口为extra_port设置的,不是mysql服务自身的端口,相信大家都觉得这个功能很贴心吧,哈哈^.^
总结:
通常有两个参数控制控制最大连接数:
max_connections:该实例允许最大的连接数
max_user_connections:该实例允许每个用户的最大连接数
每个人要根本自己业务量,设置合适的值,不要盲目有设置过大,但也不设置过小,因为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
http://www.cnblogs.com/gomysql/p/3834797.html
作者:陆炫志 出处:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111 您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明。 |