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

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.

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:


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 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. )


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 …