MySQL Study之--“too many connections”错误解决方案

MySQL Study之--“too many connections”错误解决方案

错误信息如下
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%';

+-----------------------------------------------+-----------------+
| 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.检查程序代码,对于没有关闭的链接及时进行关闭


你可能感兴趣的:(mysql)