MySQL error 1040 (Too many connections), explained and fixed

ERROR 1040 (HY000): Too many connections means new sessions are being refused because the count of open connections reached max_connections. The instinct to raise the limit is understandable and sometimes right, but the common cause is connections accumulating faster than they close: an application that opens without closing, a pool configured larger than the server allows across all its instances, or long-lived idle sessions nobody reaps. MySQL reserves one extra connection above the limit for a privileged account, which is the administrator's way in to fix it.

Get in and see who holds the connections

  1. Connect as root or a SUPER-privileged account (the reserved slot) and run SHOW PROCESSLIST; or query information_schema.processlist, grouped by user, host, and state. The picture is usually unmistakable: hundreds of connections from one application host in Sleep state, or a flood from a single misbehaving client.
  2. Check the counters: SHOW STATUS LIKE 'Threads_connected'; and Max_used_connections show the current load and the historical peak, and SHOW VARIABLES LIKE 'max_connections'; the configured ceiling.

The fixes, matched to the finding

  1. Leaking application: connections opened per request, job, or retry and not closed on every path, including error paths. Fix the code to close deterministically (try-with-resources, framework-managed pools); no server setting outruns a leak for long.
  2. Pool arithmetic: sum the maximum pool sizes of every application instance and background job; if the fleet's ceiling exceeds the server's limit, lower the pools or raise the server. This mismatch appears right after scaling the application tier.
  3. Idle accumulation: lower wait_timeout so abandoned sessions are reaped sooner, and let pools validate and retire idle connections on their side too.
  4. Raising max_connections is legitimate when the load is real, sized with memory in mind: each connection carries per-session buffers, so thousands of connections can consume gigabytes; on systemd-managed installs the setting also depends on the service's open-files limit, which the error log warns about when it becomes the binding constraint.

Common questions

Why does restarting MySQL “fix” it temporarily? Restarting drops every connection, so the count resets. If the cause is a leak or pool arithmetic, the count climbs back on the same schedule as before, which is itself the confirmation of the cause.

Can one bad client lock out everyone? Yes. Connections are a shared pool; a single client leaking or storming connections can consume them all. The processlist grouped by host identifies it, and killing its sessions (KILL <id>) is a valid immediate relief while the client is fixed.

What is a sensible max_connections? There is no universal number; it should exceed the summed pool maxima of all clients with headroom, and stay within what the host's memory supports. Hundreds is typical for application servers; tens of thousands is a design problem, not a setting.

Related errors

MySQL error 2002 (cannot connect) · MySQL error 1045 (Access denied) · MongoDB: Server selection timed out