- Error number
- 1040
- SQLSTATE
08004- Symbol
ER_CON_COUNT_ERROR- Product
- MySQL Server
- Versions
- MySQL 5.7, 8.0 and 8.4 (default max_connections 151)
- Category
- Connections & Network
- Last reviewed
- 24 October 2026
On this page
What it means
Error 1040 means the server already has as many client connections as max_connections allows, so it refused a new one.
Every client connection uses a slot on the server, whether it is running a query or sitting idle. max_connections (151 by default) caps the number of ordinary client connections; when they are all taken, the next connection attempt fails with 1040 and the server increments the Connection_errors_max_connections status counter. The error says nothing about the client that receives it: the cause is the connections that are already open, very often idle ones that an application, a pool or a script opened and never closed.
MySQL keeps one extra connection for administrators: the server actually permits max_connections + 1 client connections, and the extra one is reserved for accounts with the CONNECTION_ADMIN privilege (or the deprecated SUPER). From MySQL 8.0.14 the server can also accept administrative connections on a separate interface (admin_address and admin_port). Either way, an administrator can still get in to run SHOW PROCESSLIST and find out who holds the connections, which is the first step. A per-account limit (max_user_connections) gives a different error, 1203.
Common causes
- Connection leaks: code paths that open a connection and do not close it (an exception before close, persistent connections in many web server processes).
- Connection pools that together allow more connections than the server: for example 20 application instances with a pool of 20 each against max_connections 151.
- Slow queries or lock waits keep connections busy for longer, so a normal traffic peak needs more connections at the same time.
- Many idle connections that the server keeps for
wait_timeoutseconds (eight hours by default) before closing them. - A traffic spike, a retry storm after an outage, or a batch job that opens one connection per task.
Diagnose it
Read-only These statements only read metadata (information_schema, performance_schema and SHOW statements) or your own data. They do not change data, privileges or configuration, so they are safe to run first.
How close is the server to the limit?
Connect with an administrative account (it can use the reserved extra connection) and compare current and peak use with the limit.
SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW GLOBAL STATUS LIKE 'Max_used_connections%';
SHOW GLOBAL STATUS LIKE 'Connection_errors_max_connections'; Threads_connected at or near max_connections means the server is full now. Max_used_connections and its time show the peak since the server started. Connection_errors_max_connections counts the connections refused with 1040.
Who holds the connections, and are they idle?
SELECT user, SUBSTRING_INDEX(host, ':', 1) AS client_host, db, command,
COUNT(*) AS connections, MAX(time) AS longest_seconds
FROM performance_schema.processlist
GROUP BY user, client_host, db, command
ORDER BY connections DESC
LIMIT 20; Connections per account, client host and state. A large group in command Sleep with a high longest_seconds is a leak or an oversized pool; a large group in Query points to slow queries or lock waits holding connections.
Connections per account since the server started
SELECT user, host, current_connections, total_connections
FROM performance_schema.accounts
WHERE user IS NOT NULL
ORDER BY current_connections DESC
LIMIT 20; Which accounts use the most connections now and in total. One application account with far more current connections than its pool size suggests leaked connections.
Idle timeout and per-account limits
SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'max_user_connections';
SELECT user, host, max_user_connections
FROM mysql.user
WHERE max_user_connections > 0; How long idle connections are kept, and whether any account has its own connection cap (max_user_connections 0 means no per-account limit).
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Get in with the reserved connection and find the cause
Read-onlyConnect as an account with CONNECTION_ADMIN (or through the administrative interface if admin_address is set) and run the diagnostics above. Keep that privilege for administrators only, so the reserved slot is still free when you need it.
Close connections and size pools to fit the server
Client or network sideClose every connection in a finally block (or use the language's automatic resource handling), and add up the maximum pool size of every application instance, worker and job: the total must stay below max_connections with room for administrators and tools. Give pools a maximum idle time so unused connections are released.
End abandoned idle connections
Changes database stateIf the process list shows connections that have been idle for a long time and belong to a process that is gone or stuck, ending them frees slots at once.
Caution: KILL rolls back any open transaction on that connection, and the application behind it gets an error the next time it uses the connection. Check the user, host and command first, and agree it with the owner on a production server.
KILL 5389; -- the id of an idle connection from performance_schema.processlist
Release idle connections sooner, per application
Client or network sideAn application that cannot close connections reliably can set a shorter idle timeout on its own connections right after connecting, so the server closes forgotten ones sooner.
SET SESSION wait_timeout = 600; -- this connection only, in seconds
Raise max_connections deliberately, not blindly
Changes configurationWhen the workload genuinely needs more concurrent connections (after leaks and pool sizes are fixed), raise the limit. SET PERSIST changes it at runtime and stores it in mysqld-auto.cnf so it survives a restart; SET GLOBAL alone lasts until the next restart; my.cnf is read at startup.
Caution: Each connection uses memory for its own buffers and work, so a higher limit can exhaust memory under load, and it does nothing for leaked or idle connections, which will fill the new limit too. Check memory headroom first and raise it in steps.
SET PERSIST max_connections = 300;
# or in my.cnf
[mysqld]
max_connections = 300
Examples
Every connection slot is in use
Shown as documented in the MySQL reference manual (Too many connections); not reproduced here, because filling every connection slot would mean lowering max_connections on a shared server.
-- On the server (as an administrator):
SHOW VARIABLES LIKE 'max_connections'; -- 151, the default
SHOW GLOBAL STATUS LIKE 'Threads_connected'; -- at the limit
-- A new client without CONNECTION_ADMIN then connects:
-- mysql -h db.example.com -u app -p
ERROR 1040 (08004): Too many connections
A per-account limit is a different error (1203)
Shown as documented in the MySQL server error message reference (ER_TOO_MANY_USER_CONNECTIONS); not reproduced here, because it needs an account with a connection limit and the lab may not create accounts. The fixes are the same as for 1040, applied to that one account.
-- The account may hold at most 10 connections:
SELECT user, host, max_user_connections FROM mysql.user WHERE user = 'app'; -- app | % | 10
-- An eleventh connection as app:
-- mysql -h db.example.com -u app -p
ERROR 1203 (42000): User app already has more than 'max_user_connections' active connections
How to prevent it
Budget connections: list every application instance, worker, cron job and tool with its maximum pool size, keep the total below max_connections, and revisit it whenever you scale out. Close connections reliably, give pools an idle limit, and keep queries and transactions short so connections are returned quickly.
Monitor Threads_connected against max_connections and alert well before the limit (for example at 80 percent), and alert on any increase in Connection_errors_max_connections. Keep CONNECTION_ADMIN for administrators only, so the reserved connection is free in an emergency.
Common questions
How do I fix MySQL error 1040 Too many connections?
Connect with an administrative account (MySQL keeps one extra connection for CONNECTION_ADMIN), look at performance_schema.processlist to see which account and host holds the connections, and close or end the idle or leaked ones. Then fix the leak or pool sizes, and raise max_connections only if the workload really needs it.
What is the default max_connections in MySQL?
151. The server allows one more connection than that for accounts with the CONNECTION_ADMIN privilege, so administrators can still connect when ordinary clients are refused.
Is it safe to set max_connections very high?
Not as a fix on its own. Each connection uses memory, so a very high limit can make the server run out of memory under load, and leaked or idle connections will fill any limit. Fix leaks and pool sizes first, then raise it in steps with memory headroom in mind.
Learn more on CodeWithSQL
Official documentation
- Too many connections
- Connection interfaces (max_connections + 1, administrative interface)
- Server system variables (max_connections, wait_timeout)
- Persisted system variables (SET PERSIST)
Last technically reviewed 24 October 2026; examples and diagnostics run on MySQL 8.0.46. Message templates checked against the server's error catalogue (perror) for that version.