I get the error: “Could not connect to SQL database. Too many connections”.

Symptoms
Sometimes I get the “Too many connections” error in Plesk CP or when open a site in my browser:

Unable to connect to database: Too many connections

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`cat /etc/psa/.psa.shadow` extended-status | grep Max_used_connections
| Max_used_connections | 11 |

Current connections limit settings can be found with:

# mysqladmin -uadmin -p`cat /etc/psa/.psa.shadow ` 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`cat /etc/psa/.psa.shadow` processlist

Published
Categorized as Plesk

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.