Diagnosing and Resolving MySQL Error 1040: Too Many Connections
Learn how to diagnose and fix MySQL Error 1040 (Too many connections) using processlist analysis, timeout tuning, and memory-safe connection limit adjustments.
17 Feb 2026, 06:33 UTC

The Connection Wall
When a MySQL server reaches its configured connection limit, it rejects all new incoming requests with Error 1040: Too many connections. This creates a complete service outage for the application, even if the database CPU and disk I/O are idling. The core problem is rarely a lack of hardware power, but rather a mismatch between how the application requests connections and how the database manages them. The guidance below assumes MySQL 5.7 or 8.x; verify variable names against your version.
Identifying the Root Cause
Before increasing limits, determine whether the failure is caused by a legitimate traffic surge or a resource leak. Use the following table to categorize the behavior:
| Symptom | Likely Cause | Diagnostic Indicator |
|---|---|---|
| Sudden spike; connections drop quickly after | Traffic burst | Max_used_connections equals max_connections |
| Slow climb; many connections in 'Sleep' state | Connection leak or oversized pool | High count of Sleep threads in the processlist |
| Constant high usage; high CPU or disk I/O | Slow queries holding connections | Many threads in Query or Locked state |
Step-by-Step Diagnostic Workflow
Run these checks from a MySQL client with administrative privileges. If the server is completely full, MySQL reserves one extra connection for a user with the CONNECTION_ADMIN (or legacy SUPER) privilege, so connect with an administrative account rather than the application user.
- Check current utilization.
Run the following to see how many connections are active versus the limit:
SHOW GLOBAL STATUS LIKE 'Threads_connected'; SHOW VARIABLES LIKE 'max_connections'; SHOW GLOBAL STATUS LIKE 'Max_used_connections';If
Max_used_connectionsequalsmax_connections, the limit has been hit at least once since the last restart. If it is far below the limit, the current incident may be caused by something else, such as a stalled query blocking new work. - Analyze connection states.
Identify what the connections are actually doing:
SELECT COMMAND, COUNT(*) AS cnt FROM information_schema.processlist GROUP BY COMMAND ORDER BY cnt DESC;A high number of
Sleepconnections indicates the application is opening connections but not closing them, or the pool is oversized for the actual workload. - Inspect long-running queries.
If most threads are in
Querystate, look for stalled processes holding connections open:SHOW FULL PROCESSLIST;Look for queries with a high
Timevalue. These prevent the pool from recycling connections and are a query-tuning problem, not a connection-limit problem.
Remediation Strategies
Immediate Recovery (Emergency)
To restore service immediately, increase the limit dynamically. This takes effect without a restart but is volatile and resets if the server reboots. Run it as a user with the SUPER or SYSTEM_VARIABLES_ADMIN privilege:
SET GLOBAL max_connections = 500;Risk: each connection consumes memory for thread buffers (for example join_buffer_size and sort_buffer_size). Raising the limit too high without sufficient RAM can trigger the Linux OOM (Out-Of-Memory) killer, which may terminate the mysqld process entirely. Increase in small increments and watch free memory.
Long-Term Stability Fixes
- Tune connection timeouts.
If you see too many
Sleepconnections, reduce how long MySQL waits before killing an idle connection. The default is often 28800 seconds (8 hours), which is too high for most web applications:SET GLOBAL wait_timeout = 600; -- 10 minutes SET GLOBAL interactive_timeout = 600;Caution: setting this too low can prematurely terminate legitimate long-running batch processes. Check your batch job durations first.
- Implement client-side pooling.
Avoid opening a new TCP connection for every request. Use a connection pool (for example HikariCP for Java). Ensure the
maximumPoolSizesummed across all application nodes stays below the MySQLmax_connectionsvalue, leaving headroom for administrative and monitoring connections. - Persist the configuration.
To ensure the limit survives a restart, add the following to
my.cnformy.ini:[mysqld] max_connections = 500 wait_timeout = 600Without this step, dynamic changes are lost on the next restart.
Verification and Testing
After applying fixes, monitor Threads_connected during a peak load period to confirm connections are being recycled rather than accumulating. To proactively test the limit in a staging environment, use mysqlslap to simulate concurrent clients. Run it from a shell as a system user with database credentials:
mysqlslap --user=root --password --concurrency=100 --iterations=10 --query="SELECT 1"If the tool reports connection errors, your max_connections is still too low for your target concurrency, or the pool is not releasing connections. Do not run concurrency tests against a production server.
Escalation Criteria
Escalate beyond connection tuning when: connections remain saturated after pooling and timeout fixes (indicating slow queries need index or query optimization); memory pressure prevents raising max_connections further (indicating vertical scaling or a read-replica architecture is needed); or connection growth correlates with a specific deploy (indicating an application-side leak that needs a code fix).
Rollback Procedure
If increasing max_connections causes instability or memory exhaustion, revert dynamically:
SET GLOBAL max_connections = [previous_value];Then remove or correct the corresponding line in my.cnf so the unsafe value does not reapply on restart.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.