What the message looks like
ERROR 1040 (08004): Too many connections
SQLSTATE[HY000] [1040] Too many connectionsCommon causes
- An application opens connections and does not close or reuse them, for example without a pool or with too many workers.
- Slow queries or locks keep connections busy, so new requests pile up behind them.
- A traffic peak or a bot flood makes a web app open many connections at once.
- Idle connections stay open too long because
wait_timeoutis high. max_connectionsis simply too low for the real load.
Check
- Current limit
mysql -e "SHOW VARIABLES LIKE 'max_connections'" - How high did it get since the start?
mysql -e "SHOW GLOBAL STATUS LIKE 'Max_used_connections'" - What are the connections doing?
mysql -e 'SHOW FULL PROCESSLIST' - Which user and host holds most of them?
mysql -e 'SELECT user, host, COUNT(*) AS n FROM information_schema.processlist GROUP BY user, host ORDER BY n DESC'
Fix
- Log in as an admin account. The server keeps one extra connection for users with the needed privilege, so the root account can usually still get in.
- Find the cause in the process list: many
Sleepentries mean leaking or idle clients, many long-running queries mean slow queries or locks. - End stuck sessions if needed with
KILL id;. This aborts what they are doing. - Fix the application: use a connection pool, close connections, limit the number of workers.
- Only then raise the limit, for example
SET GLOBAL max_connections = 300;and keep it in the[mysqld]section of the config. Every connection uses memory, so do not set it higher than the RAM allows. Consider a lowerwait_timeouttoo.
Typical scenario
An online shop has pm.max_children = 200 in PHP-FPM, but MySQL still has max_connections = 151. During a promotion the workers open more simultaneous connections than the server allows, and the site shows "SQLSTATE[HY000] [1040] Too many connections". With SHOW GLOBAL STATUS LIKE 'Max_used_connections' compare how close the peak since startup came to the limit, and check the number of workers.
FAQ
Is it safe to just raise max_connections?
It hides the cause and can lead to memory pressure and the OOM killer. Raise it moderately and watch memory, after you have fixed leaks and slow queries.
I cannot log in at all, not even as root.
Connect through the local socket as root, which uses the extra slot. If that also fails, restart the service and fix the application before it reconnects.
How does max_connections relate to memory?
Each connection takes additional memory for thread buffers such as sort and join buffers. The demand therefore grows with the number of simultaneously active connections. If you raise max_connections a lot, you can push the server into memory shortage at a load peak. At least do a rough calculation.
What does Max_used_connections tell me?
It is the peak of simultaneous connections since server start. If it is just below or equal to max_connections, you have already hit the limit. Threads_connected shows the current number instead. Together they show whether the problem is constant or only occurs in peaks.
How do PHP-FPM workers and database connections fit together?
Each active worker usually holds one connection. With pm.max_children = 200 PHP can open up to 200 connections, permanently with persistent connections. The database limit must therefore at least fit the sum of all pools plus reserve for administrators and cron jobs.