About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
ERROR 1040 (08004): Too many connections means every connection that max_connections allows is already in use. The manual says: "If clients encounter Too many connections errors when attempting to connect to the mysqld server, all available connections are in use by other clients." Raising the limit is a valid fix only when the load is real. If the connections are idle, leaked, or queued behind a slow query, a higher limit only moves the failure further out.
What the error means
The MySQL 8.4 error reference lists error 1040, symbol ER_CON_COUNT_ERROR, SQLSTATE 08004, with the message Too many connections. The permitted number is the max_connections system variable. Its default is 151, it is dynamic (it can be changed at runtime), and its range is 1 to 100000. The manual adds that "The maximum effective value is the lesser of the effective value of open_files_limit - 810, and the value actually set for max_connections", so the operating system's file limit can cap it.
One extra connection exists for administrators. The manual says: "mysqld actually permits max_connections + 1 client connections. The extra connection is reserved for use by accounts that have the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege)." With the default, that is 151 + 1 = 152 connections, and the last one is for an administrator. This is why an administrator can still connect to diagnose the problem, and why granting that privilege to ordinary application accounts defeats the reserve.
The server can also accept administrative connections on a separate network interface, set up with a dedicated IP address and port, as a further route in.
Likely causes
| Cause | What you see | How to confirm |
|---|---|---|
| Connection pool or web workers larger than the limit | Many connections from the same application user | Count connections by user in the process list |
Many idle sessions (Sleep) |
Command is Sleep for most rows, with large Time |
Process list: the manual says Command is Sleep if the session is idle |
| A slow or blocked query makes others queue | Many sessions running or waiting for one table or row | Process list with State and Info |
| Connection leak in an application | Count climbs and stays high until the application restarts | Threads_connected over time |
| Limit set below the real load | Connections steadily at the limit with active queries | Max_used_connections reaches max_connections |
One account capped by max_user_connections |
A different error, error 1203: User %s already has more than 'max_user_connections' active connections |
max_user_connections and the account's resource limit |
Prove the cause before changing anything
All of these are read-only. Connect as an account with CONNECTION_ADMIN (or SUPER) so that the reserved connection works. The account also needs the PROCESS privilege, or steps 2 and 3 will not show other users' threads.
1. The limit and the counters. The manual documents SHOW VARIABLES and SHOW STATUS with LIKE:
SHOW GLOBAL 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';
SHOW GLOBAL STATUS LIKE 'Threads_running';
The manual defines Threads_connected as "The number of currently open connections", Max_used_connections as "The maximum number of connections that have been in use simultaneously since the server started", Connection_errors_max_connections as "The number of connections refused because the server max_connections limit was reached", and Threads_running as "The number of threads that are not sleeping". A high Threads_connected with a low Threads_running means mostly idle sessions.
2. Who holds them. The manual says SHOW PROCESSLIST "is very useful if you get the “too many connections” error message and want to find out what is going on". The Performance Schema processlist table gives the same data in a form you can group:
SELECT USER, COMMAND, COUNT(*) FROM performance_schema.processlist GROUP BY USER, COMMAND ORDER BY COUNT(*) DESC;
The table has the columns ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO and EXECUTION_ENGINE. The manual says that with the PROCESS privilege you can see all threads; without it you see only your own.
3. The oldest or longest sessions.
SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE FROM performance_schema.processlist ORDER BY TIME DESC LIMIT 20;
Idle sessions with a very large TIME point to leaks. Sessions in a query state with a large TIME point to a blocked or slow statement.
Fix
Fix the client side first
Reduce the application's pool size, close connections that are no longer needed, and fix the code path that leaks. The sum of every application's pool should stay under max_connections. If the cause is queued statements, fix the slow query or lock first.
End specific sessions (only if needed)
Pick the exact ID from step 3. Disruptive step: KILL ends a session. The manual says KILL CONNECTION "terminates the connection associated with the given processlist_id, after terminating any statement the connection is executing", and KILL QUERY ends the statement but leaves the connection. The client loses its work in progress, and this cannot be undone. Run it for one ID you have just read:
KILL QUERY 4321;
Replace 4321 with the real ID. Prefer KILL QUERY where stopping the statement is enough. Without CONNECTION_ADMIN or SUPER you can kill only your own threads.
Raise max_connections
Only after the checks above show a real need, and after checking open_files_limit and memory. Save first by printing the current values and keeping them:
SHOW GLOBAL VARIABLES LIKE 'max_connections';
SHOW GLOBAL VARIABLES LIKE 'open_files_limit';
SELECT * FROM performance_schema.persisted_variables;
The persisted-variables page says the Performance Schema persisted_variables table is an SQL interface to mysqld-auto.cnf, so the last statement shows any value already persisted. Keep all three outputs. The manual's form for a runtime change is SET GLOBAL max_connections = 1000;. Disruptive step: the change applies at once to the running server. The manual also gives SET PERSIST max_connections = 1000;, which sets the runtime value and also writes the setting to the mysqld-auto.cnf file in the data directory so that it survives a restart.
If you used only SET GLOBAL, back out with SET GLOBAL max_connections = 151;, using the value you saved. If you used SET PERSIST, back out in two parts: run SET GLOBAL max_connections with the saved value to restore the runtime value, and run RESET PERSIST max_connections; to remove the persisted setting from mysqld-auto.cnf (if the persisted_variables output you saved already held a max_connections value, set that value again with SET PERSIST instead of resetting), because the manual says SET PERSIST "writes the variable setting to the mysqld-auto.cnf file" and RESET PERSIST is how to remove persisted variables. The manual says the server processes mysqld-auto.cnf "after all other option files", so a persisted value is applied after anything in my.cnf; check the manual's persisted-variables page before relying on a file edit instead.
Example (illustrative): db01 runs MySQL 8.4 with max_connections at 151. During a sale, shop-web reports ERROR 1040 (08004): Too many connections. The administrator connects with the reserved connection. Threads_connected is 152 and Threads_running is 6. The grouped process list shows 140 sessions of shop in Sleep, with TIME values of several minutes. The application's pool was set to 150 per web server and there are three servers, so the pools can ask for 450. The team reduces each pool to 40, and the count falls to about 120 without any change to the server.
Confirm the fix
Threads_connectedstays belowmax_connectionsthrough a busy period.Connection_errors_max_connectionsstops increasing. (Compare two readings taken some time apart.)- The application connects normally at its busiest time.
- After any limit change,
SHOW GLOBAL VARIABLES LIKE 'max_connections';shows the intended value, and a restart (in a window) keeps it only if you persisted it.
What to watch afterwards
- Alert at a share of
max_connections(for example, 80 per cent), not at the limit. - Watch
Max_used_connectionsafter each release of an application. - Add up all application pool sizes when a new server is added.
- Grant
CONNECTION_ADMINonly to administrators.
Read next
Related articles you may want next.
- TroubleshootingMySQL ERROR 1205: Lock wait timeout exceeded; try restarting transactionMySQL ERROR 1205 Lock wait timeout exceeded; try restarting transaction: find the blocking session with sys.innodb_lock_waits and fix it safely.Read article
- PerformancePHP-FPM Has Reached pm.max_children: Measure Queueing Before Adding WorkersSeparate long-running requests from insufficient concurrency and size workers against measured memory and dependency limits.Read article
- TroubleshootingPostgreSQL "sorry, too many clients already": count connections before raising max_connectionsPostgreSQL 'sorry, too many clients already'? Count connections by state, fix idle-in-transaction leaks, then decide on max_connections.Read article
Risks and limits
- These commands have not been run or lab-tested. They come from the MySQL 8.4 manual.
- MySQL 8.4 only; MariaDB is not covered.
- The manual does not give a figure for the memory each extra connection uses, so none is stated.
- Group Replication internal sessions are said not to count toward the limit; the connection-interfaces page names no other exclusion.
Sources
- MySQL 8.4 Reference Manual: Too many connections — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: Server System Variables (max_connections, max_user_connections, open_files_limit) — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: Server Status Variables — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: SHOW PROCESSLIST Statement — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: The processlist Table — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: KILL Statement — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: SET Syntax for Variable Assignment — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: Persisted System Variables — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: Connection Interfaces — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: SHOW VARIABLES Statement — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Reference Manual: SHOW STATUS Statement — Oracle, MySQL 8.4 manual, no page date shown, accessed 20 November 2026
- MySQL 8.4 Error Reference: Server Error Message Reference — Oracle, MySQL 8.4 error reference, no page date shown, accessed 20 November 2026
This article was originally published on .