解决CentOS服务器:mysql 1040 too many connections问题(亲测有用)

方法一:

1、mysql -u root -p 回车输入密码进入mysql

[root@root ~]# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 135
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> 

2、查看现在MYSQL的最大链接数,执行:show variables like “max_connections”;

mysql> show variables like "max_connections";
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.01 sec)

我们这里可以看到:MYSQL的最大链接数才151,

3、修改MYSQL的最大链接数,执行:set GLOBAL max_connections=10000;

mysql> set GLOBAL max_connections=10000; 
mysql>

4、再执行:show variables like “max_connections”;看看MYSQL的最大链接数有没有修改成功:

mysql> show variables like "max_connections";
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 10000 |
+-----------------+-------+
1 row in set (0.01 sec)

我们这里可以看到:MYSQL的最大链接数才10000了,退出就可以了

5、exit退出,这种方法弊端就是每次重启服务器后都需要修改MYSQL的最大链接数

 

方法二:

1、修改mysql配置文件my.cnf:vim /etc/my.cnf

[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 160M
max_allowed_packet = 100M
table_open_cache = 256
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 80M
skip-grant-tables
lower_case_table_names=1
max_connections=10000
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.

找到:max_connections=151,修改为:max_connections=10000 (如没有就在[mysqld]下自行添加)

2、:wq保存退出

3、重新启动mysql:service mysql restart