PHP: MySQL报错: ERROR 1040: Too many connections

 

MySQL: ERROR 1040: Too many connections

This basically tells that MySQL handles the maximum number of connections simultaneously and by default it handles 100 connections simultaneously.

These following reasons cause MySQL to run out connections.

  1. Slow Queries
  2. Data Storage Techniques
  3. Bad MySQL configuration

I was able to overcome this issues by doing the followings.

Open MySQL command line tool and type, 查看默认连接数:

show variables like "max_connections";

This will return you something like this. 结果如下:

+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 100   |
+-----------------+-------+

You can change the setting to e.g. 200 by issuing the following command without having to restart the MySQL server. 自定义连接数:

set global max_connections = 200;

Now when you restart MySQL the next time it will use this setting instead of the default. 重启apache2:

sudo service apache2 restart

Keep in mind that increase of the number of connections will increase the amount of RAM required for MySQL to run.

注意:增加连接数,意味着需要更高的服务器设备要求!

You have to change max_connections to increase total permitted connections.

And set max_user_connections back to default 0 => no limit unless you need to limit this per user connection

 

更多参看官网:https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

 

本文: PHP: MySQL报错: ERROR 1040: Too many connections

Leave a Reply