MySQL & MariaDB error 1040: Too many connections
Too many connections (5.6.51) All max_connections slots are in use, so new clients are refused. Usually leaked or idle pooled connections or a traffic spike, rarely a limit that is really too low.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_CON_COUNT_ERROR | ER_CON_COUNT_ERROR |
| SQLSTATE | 08004 | 08004 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
The server already has max_connections client connections (151 by default) and refuses another one. One extra slot is kept for an account with SUPER (MySQL 8.0+: CONNECTION_ADMIN), so an administrator can still log in and see what’s going on, as long as nobody else used that slot already.
Why it happens
- Applications open connections and don’t close them, or pools are sized so that
pool size × application instancesis more than the server allows. - Slow queries or lock waits make connections stay busy longer, so a normal traffic peak needs many more of them at once.
- Many connections in
Sleepstate for a long time:wait_timeout(8 hours by default) keeps abandoned connections open. - A per-account limit gives a different error: 1203 for
max_user_connections, 1226 forMAX_CONNECTIONS_PER_HOUR.
How to fix it
- Log in as an admin and see who holds the connections:
SHOW FULL PROCESSLIST;works everywhere. On MySQL 8.0.22+ the preferred form isSELECT user, host, command, time, state FROM performance_schema.processlist ORDER BY time DESC;, becauseSHOW PROCESSLISTandinformation_schema.PROCESSLISTraise deprecation warning 1287 there (tested on 8.0.46, 8.4 and 9.7). Group byuser, hostto find the application. KILLlong-idle connections as a short-term relief, then fix the pool settings or the leak.- Lower
wait_timeoutfor application accounts so abandoned connections are dropped sooner. - MariaDB won’t go below
max_connections = 10; MySQL allows 1. - Raise
max_connectionsonly if the server has the memory for it. Each connection uses per-session buffers, so a large value can push the server into swap or the out-of-memory killer under load. SHOW GLOBAL STATUS LIKE 'Max_used_connections';shows the peak since the last restart.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;As an admin, lower the limit so it is easy to reach (don't do this on a shared server).
SET GLOBAL max_connections = 10;Open one connection as a normal (non-admin) account and leave it open, then try a second one:
mysql -u appuser -p -h 127.0.0.1 -e 'SELECT SLEEP(30)' & mysql -u appuser -p -h 127.0.0.1 -e 'SELECT 1'Put the limit back (151 is the default).
SET GLOBAL max_connections = 151;
You get: ERROR 1040 (08004): Too many connections(11.8.9)
Verified fixes
See who holds the connections (an admin account gets one extra slot for this). MySQL 8.0+ warns 1287: SHOW PROCESSLIST is deprecated in favour of performance_schema.processlist
SHOW FULL PROCESSLIST;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| More clients connect than max_connections allows (here it is lowered to 10, MariaDB's minimum) repro | ✓ 1040 | ✓ 1040 | ✓ 1040 | ✓ 1040 | ✓ 1040 | ✓ 1040 | ✓ 1040 | ✓ 1040 | ✓ 1040 |
| See who holds the connections (an admin account gets one extra slot for this). MySQL 8.0+ warns 1287: SHOW PROCESSLIST is deprecated in favour of performance_schema.processlist fix | ✓ | ✓ | ⚠ 1287 | ⚠ 1287 | ⚠ 1287 | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (1)
- See who holds the connections (an admin account gets one extra slot for this). MySQL 8.0+ warns 1287: SHOW PROCESSLIST is deprecated in favour of performance_schema.processlist (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: 'INFORMATION_SCHEMA.PROCESSLIST' is deprecated and will be removed in a future release. Please use performance_schema.processlist instead