Resolving: 'Too many connections' Error in MySQL / MariaDB Databases
The Error 1040: Too many connections error immediately stops queries to your database and occurs when the number of open connection threads from your application exceeds the maximum directive defined in the internal settings of the MySQL or MariaDB server.
Quick Diagnostics
SET GLOBAL max_connections = 500;max_connections = 500 in /etc/mysql/my.cnf and restart serviceThe Error 1040: Too many connections error immediately stops queries to your database and occurs when the number of open connection threads from your application exceeds the maximum directive defined in the internal settings of the MySQL or MariaDB server.
Step-by-Step Solution
-
1
Step 1: Access the engine and diagnose inactive threads
If the web server still allows you to interact via console using the admin account, inspect which query processes are holding up the data traffic:
BASH# Log in to the database engine mysql -u root -p # Execute the command to list processes SHOW PROCESSLIST;(Look for queries with a prolonged "Sleep" state. If there are hundreds of them, the application is not closing connection channels after responding).
-
2
Step 2: Adjust global limits in the configuration file
Open the database server configuration file (
/etc/mysql/my.cnfor/etc/my.cnf.d/server.cnfin MariaDB) and increase the maximum limit of concurrent connections allowed by your hardware:PLAINTEXT[mysqld] max_connections = 250 interactive_timeout = 180 wait_timeout = 60(Note: Reducing
wait_timeoutforces the engine to kill inactive, unused connections after 60 seconds, automatically freeing up slots). -
3
Step 3: Restart the service to free memory
BASHsudo systemctl restart mysql # For MySQL sudo systemctl restart mariadb # For MariaDB
Prevention Advice
Recommended security practices:
- Do not arbitrarily increase the
max_connectionsdirective to astronomical values (such as 1000 or 2000) if your hardware has limited physical RAM. Each connection channel consumes processor resources and cache memory; exceeding the server's capacities will cause the system to freeze due to a lack of pagefile space or trigger critical crashes. Implement connection pooling in your web application to reuse channels instead of opening a new one for every query.