MySQL error 1040
ERROR 1040: Too many connections
Every connection slot the server allows (max_connections, 151 by default) is taken, so it turns new ones away. Usually an app holds more connections than it needs: leaked, idle or stuck behind slow queries. Find them first; raise the limit only if the load is real.
ERROR 1040 (08004): Too many connections
Tested on MySQL 8.4.11 and MariaDB 11.4.13 (temporary servers with a low max_connections) · Updated 9 October 2026
What it means
The server allows at most max_connections client connections at once: 151 by default in both
MySQL 8.4 and MariaDB 11.4. When they’re all in use, it turns new connections away with error 1040.
Nothing is wrong with your credentials or network; there’s no room left.
Both servers keep one extra slot for administrators (accounts with CONNECTION_ADMIN, or the old
SUPER), so someone can still get in to look. Once that slot is taken too, even administrators are
refused. MySQL 8.4 can also open a separate administrative port that isn’t limited at all, if it was
configured in advance.
You may see the SQLSTATE as 08004 or HY000: in our tests, an ordinary user turned away while the
admin slot was still free got 08004, and a connection refused once even the admin slot was taken
got HY000.
Common causes
- Connection pools larger than the server allows. Ten app instances with a pool of 20 each can open 200 connections, more than the default 151. Autoscaling and serverless functions multiply this.
- Leaked connections: code paths that open a connection and never close it or return it to the pool.
- Idle connections that never time out. The server closes idle connections only after
wait_timeout, eight hours (28800 s) by default. - Slow queries or lock waits. If each request holds a connection while it waits, a slow query or a lock wait makes connections pile up.
- A small limit for the workload: hosted databases often set
max_connectionsby instance size.
How to fix it
Get in and see who holds the connections
Connect with an administrator account to use the reserved slot, then group the connections:
SELECT user, SUBSTRING_INDEX(host, ':', 1) AS client, command,
COUNT(*) AS connections, MAX(time) AS longest_s
FROM information_schema.processlist
GROUP BY user, client, command
ORDER BY connections DESC;
Many rows with command = Sleep and large longest_s are idle connections an app is holding.
Many in Query point to slow queries. The counters show how close you’ve been to the limit:
SHOW GLOBAL STATUS WHERE Variable_name IN
('Threads_connected', 'Max_used_connections', 'Max_used_connections_time',
'Connection_errors_max_connections');
Free some slots
End idle connections from a misbehaving client by id (KILL <id>), or restart that app, which
closes its pool. This buys time; it doesn’t fix the cause.
Fix the pools and the leaks
Add up the pool maximum across every instance of every app, plus your own tools, and keep it below
max_connections. Make sure every code path closes its connection or returns it to the pool, and
give pools an idle timeout and a maximum lifetime shorter than the server’s wait_timeout. For many
short-lived clients, such as serverless functions, put a proxy or pooler in front of the database.
Shorten wait_timeout
So abandoned connections close sooner:
SET GLOBAL wait_timeout = 600;
It applies to new connections. Check your pools’ settings first, so they don’t hand out connections the server has already closed.
Raise max_connections, if the load is real
SET GLOBAL max_connections = 500; -- until restart
SET PERSIST max_connections = 500; -- MySQL 8: also after restarts
On MariaDB, set max_connections in the server’s config file to keep it across restarts. Each
connection uses memory, so raise it in steps and watch the server. On hosted databases, change it
in the provider’s parameter settings.
Keep a way in for next time (MySQL)
Set admin_address (and optionally admin_port, 33062 by default) in the server config. Accounts
with SERVICE_CONNECTION_ADMIN can then always connect on that port, however many ordinary
connections there are. MariaDB has extra_port and extra_max_connections for the same purpose.
Reproduce it
On a temporary MySQL 8.4.11 container started with --max-connections=3, an ordinary account
(seo_mysql_web) opened three connections running SELECT SLEEP(12). Its fourth:
ERROR 1040 (08004): Too many connections
root still got in through the reserved slot, and saw four connections:
Variable_name Value
Threads_connected 4
Variable_name Value
Connection_errors_max_connections 1
With root holding that slot, another root connection was refused:
ERROR 1040 (HY000): Too many connections
A temporary MariaDB 11.4.13 container started with --max-connections=3 came up with 10 (MariaDB
didn’t go lower), and the eleventh ordinary connection got the same ERROR 1040 (08004). With the
reserved slot also taken, MariaDB’s own client reported it like this:
ERROR 2002 (HY000): Received error packet before completion of TLS handshake. The authenticity of the following error cannot be verified: 1040 - Too many connections
In Inlet
Inlet’s Activity monitor for MySQL and MariaDB shows the server’s sessions, so once you’re connected with an administrator account you can see which users and clients hold the connections. When a connection is refused, the connection window shows the server’s message with a hint.