20. August 2010 · 9 comments · Categories: Linux
"Unable to connect to database: Too many connections" - common issue ...

Cause:
This error means that the limit of simultaneous MySQL connection to mysql server is
reached and new connections to the server cannot be established at this time.

Resolution:
There are two ways to solve this issue. The first one is increase the connection
limit and the second, find what is the reason of “too many connection” error and
try to lower MySQL server usage.

MySQL server state can be checked using ‘mysqladmin’ utility. For example to find out
the number of current connections to the server use:

#mysqladmin -uadmin -p extended-status | grep Max_used_connections
| Max_used_connections | 11 |

Current connections limit settings can be found with:

#mysqladmin -uadmin -p variables | grep 'max.*connections'
| max_connections | 100 |
| max_user_connections | 0

In the example above, maximum number of connections to the server (max_connections)
is set to 100. And maximum number of connections per user (max_user_connections)
to zero, that means unlimited.
There are default MySQL values. They can be redefined in /etc/my.cnf, for example:

[mysqld]
set-variable=max_connections=150
set-variable=max_user_connections=20

Restart MySQL after my.cnf is modified.

Note, if you set connections limit to very high value (more than 300) it may
affect the server performance. It is better to find out the reason of the high
MySQL server usage.
You may check what users/requests slow mysql and take all curent connections,
for example with the command:

# mysqladmin -uadmin -p processlist
In order to do this we have to create a file named .ftpaccess with the
following content:

 <Limit ALL> DenyALL
 Allow 127.0.0.1
 Allow IP(s)  ( if you have dynamic IP you have to set IP class like this:

Allow 1.2.3.
Allow 1.2.4.
Allow 1.2.5.
Allow 1.2.6.
Allow 1.2.7. )

 </Limit>

This file must be uploaded in httpdocs, httpsdocs, cgi and web users ...
That's it!

Here is a good website to check if your IP is on the blacklist …

css.php