解決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

您的支持是對博主最大的鼓勵,感謝您的認真閱讀。本文版權歸作者所有,歡迎轉載,但請保留該聲明。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM