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.
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.