MySQL Study之--「too many connections」錯誤解決方案mysql
錯誤信息以下:
Can not connect to MySQL serversql
Error: Too many connections
Errno.: 1040session
Similar error report has beed dispatched to administrator before.socket
如下是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.
ide
解決方法:網站
一、修改my.cnf配置文件this
[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=1000spa
二、重啓mysql serverrest
[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%';
orm
+-----------------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------------+-----------------+ | character_set_connection | utf8 | | collation_connection | utf8_general_ci | | connect_timeout | 10 | | disconnect_on_expired_password | ON | | extra_max_connections | 1 | | init_connect | | | max_connect_errors | 100 | | max_connections | 1000 | | max_user_connections | 0 | | performance_schema_session_connect_attrs_size | 512 | +-----------------------------------------------+-----------------+ 10 rows in set (0.02 sec)
在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.檢查程序代碼,對於沒有關閉的連接及時進行關閉