Fix MySQL/MariaDB Error 1040: Too Many Connections

Diagnose and resolve MySQL 'Too Many Connections' errors by adjusting max_connections, hunting idle connections, and optimizing pool sizes.


The MySQL error Too many connections occurs when the database server receives more concurrent connection requests than the maximum limit defined in its configuration. When this happens, MySQL refuses all subsequent connection requests, causing your web application to show a database connection error.


Symptom & Error Signature

Your application logs, WordPress site, or terminal client displays:

ERROR 1040 (HY000): Too many connections

Root Cause Analysis

MySQL has a system variable called max_connections (defaulting to 151 in MySQL 8.0). This limit is exceeded due to:

  1. Traffic Spike: A sudden burst of users accessing the website.
  2. Slow Queries: Queries that take several seconds to run block the thread, causing new incoming requests to pile up and stack connections.
  3. Persistent Connections / Leaks: Applications opening connections and not closing them properly (e.g., poorly written PHP scripts, or bad database pooling configurations).
  4. Too many Sleep connections: Client connections staying open in a Sleep state for too long before timeout.

Step-by-Step Resolution

1. Temporarily Increase the Connection Limit

If the database is currently throwing this error, you won’t be able to connect normally. However, MySQL reserves one administrative connection for accounts with the CONNECTION_ADMIN (or SUPER) privilege.

Log in as root:

mysql -u root -p

Increase the limit dynamically without restarting the server (which would drop active connections):

SET GLOBAL max_connections = 500;

This immediately resolves the issue for new connections, but will reset if the server restarts.

2. Make the Change Persistent

To make the connection increase permanent, edit the MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

[!NOTE] On MariaDB, the file is usually located at /etc/mysql/mariadb.conf.d/50-server.cnf.

Find or add the max_connections setting under the [mysqld] section:

[mysqld]
max_connections = 500

Save and exit, then reload/restart MySQL when safe:

sudo systemctl restart mysql

3. Analyze Running Connections

Find out what is eating up connections. Run:

SHOW PROCESSLIST;

Look at the Command and Time columns. If you see dozens of queries in the Sleep command state with a high Time (in seconds), it means they are idle.

If you have a query that is hanging in Query state for a long time, note its Id and terminate it:

KILL 12345; -- Replace with the actual process ID

4. Tune Idle Connection Timeouts

By default, MySQL keeps connections open in a Sleep state for 28800 seconds (8 hours) before closing them. If your application doesn’t close connections, this will quickly hit the limit.

Reduce wait_timeout and interactive_timeout to something sensible (e.g., 60 seconds) in your /etc/mysql/mysql.conf.d/mysqld.cnf:

[mysqld]
wait_timeout = 60
interactive_timeout = 60

This forces MySQL to reclaim idle connections after 60 seconds of inactivity.