錯誤資訊如下:
Can not connect to MySQL server
Error: Too many connections
Errno.: 1040
Similar error report has beed dispatched to administrator before.
以下是mysql.com網站的相關說明:
If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients.
The number of connections allowed is controlled by the max_connections system variable. Its default value is100. If you need to support more connections, you should restart mysqld with a larger value for this variable.
mysqld actually allows max_connections+1 clients to connect. The extra connection is reserved for use by accounts that have the SUPER privilege. By granting the SUPER privilege to administrators and not to normal users (who should not need it), an administrator can connect to the server and useSHOW PROCESSLIST to diagnose problems even if the maximum number of unprivileged clients are connected. See Section 13.5.4.19, “SHOW PROCESSLIST Syntax”.
The maximum number of connections MySQL can support depends on the quality of the thread library on a given platform. Linux or Solaris should be able to support 500-1000 simultaneous connections, depending on how much RAM you have and what your clients are doing. Static Linux binaries provided by MySQL AB can support up to 4000connections.
解決方法:
1、修改my.cnf配置檔案
[root@rh64 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend
sql_mode=STRICT_TRANS_TABLES ,NO_ENGINE_SUBSTITUTION
slow_query_log=true
slow_query_log_file = "/var/lib/mysql/rh64-slow.log"
long_query_time=1
log-queries-not-using-indexes=true
max_connections=1000
2、重新開機mysql server
[root@rh64 ~]# service mysql restart
Shutting down MySQL (Percona Server).. [ OK ]
Starting MySQL (Percona Server). [ OK ]
[root@rh64 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25-73.1-log Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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 variables like '%connect%';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<code>+-----------------------------------------------+-----------------+</code>
<code>| Variable_name | Value |</code>
<code>| character_set_connection | utf8 |</code>
<code>| collation_connection | utf8_general_ci |</code>
<code>| connect_timeout | </code><code>10</code> <code>|</code>
<code>| disconnect_on_expired_password | ON |</code>
<code>| extra_max_connections | </code><code>1</code> <code>|</code>
<code>| init_connect | |</code>
<code>| max_connect_errors | </code><code>100</code> <code>|</code>
<code>| </code>
<code>max_connections | </code><code>1000</code>
<code> </code><code>|</code>
<code>| max_user_connections | </code><code>0</code> <code>|</code>
<code>| performance_schema_session_connect_attrs_size | </code><code>512</code> <code>|</code>
<code>10</code> <code>rows </code><code>in</code> <code>set (</code><code>0.02</code> <code>sec)</code>
在my.cnf修改參數後,需要重新啟動mysql server,如果不能重新開機,也可以查詢連接配接的程序,然後嘗試用kill id關閉一些程序。
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined |
| 1 | root | localhost | NULL | Query | 0 | init | show processlist | 0 | 0 |
1 row in set (0.00 sec)
mysql> kill id //查詢出的程序 id
總結,解決問題的最終方法:
1.修改配置檔案/etc/my.cnf,調整連接配接參數
2.檢查程式代碼,對于沒有關閉的連結及時進行關閉
本文轉自 客居天涯 51CTO部落格,原文連結:http://blog.51cto.com/tiany/1725955,如需轉載請自行聯系原作者