dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/mysql-1040-too-many-connections
MySQL
MySQL 5.7–8.4 LTSConnection & Resource
8 min read
ERROR 1040 (HY000): Too many connections

MySQL Error 1040 "Too many connections" — Connection Leak or Traffic Storm?

Diagnosing 1040: connection leaks vs connection storms, reserved admin slot, open_files_limit clamps, and the real cost of raising max_connections.

Reviewed by:Principal Database Reliability Architect
Tested Versions: MySQL 8.4 LTS & 8.0.40 (Debian Bookworm / Docker 29.7) | Tarihsel: MySQL 5.7

Operational Verification & Reliability Metadata

Last Technical Review: 30 Ekim 2026
Tested VersionsMySQL 8.4 LTS & 8.0.40 (Docker 29.7) | Tarihsel: MySQL 5.7.44
Required PrivilegesCONNECTION_ADMIN or SUPER (to access the reserved +1 administrative slot)
Risk Assessment
HighRecklessly bumping max_connections can exhaust memory via thread_stack and per-thread buffers causing OOM kill
Expected OutputThreads_connected normalized below max_connections and distinct leak vs storm triage
Rollback Procedure:Prune idle Sleep sessions via KILL CONNECTION and route traffic through an external pooler like ProxySQL
MySQL 8.4 Docs: Too Many Connections
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptom

plaintext
ERROR 1040 (HY000): Too many connections

While conceptually equivalent to PostgreSQL's H-01 (too many clients already), MySQL features two distinct architectural characteristics that fundamentally change triage and recovery:

  1. 1The +1 Reserved Administrative Slot: When MySQL reaches its max_connections limit, it retains one additional connection slot exclusively for users with CONNECTION_ADMIN (MySQL 8.0+) or SUPER (MySQL 5.7) privileges. If you are entirely locked out of the database, you are connecting with an application account; administrative users can still connect to diagnose the root cause.
  2. 2The `open_files_limit` Clamp: The max_connections parameter is strictly constrained by the operating system file descriptor limit. If MySQL finds open_files_limit insufficient at startup, it silently clamps `max_connections` down:
plaintext
[Warning] Could not increase number of max_open_files to more than 1024, please change --open_files_limit [Warning] Changed limits: max_openfiles: 1024 max_connections: 151 table_open_cache: 400
WARNING
If this warning appears in your error log, setting max_connections = 1000 in my.cnf is completely disregarded; MySQL operates capped at 151 connections. Never assume configured values without querying runtime status.

▸Diagnostic Queries to Confirm Root Cause

Connect via the administrative account and inspect active thresholds:

sql
SHOW VARIABLES LIKE 'max_connections'; SHOW VARIABLES LIKE 'max_user_connections'; SHOW VARIABLES LIKE 'open_files_limit'; SHOW GLOBAL STATUS LIKE 'Threads_connected'; SHOW GLOBAL STATUS LIKE 'Threads_running'; SHOW GLOBAL STATUS LIKE 'Max_used_connections'; SHOW GLOBAL STATUS LIKE 'Aborted_connects';

Next, aggregate the processlist to identify which client application is saturating connection slots:

sql
SELECT user, host, IFNULL(db,'-') AS db, command, state, COUNT(*) AS count, MAX(time) AS max_time_seconds FROM information_schema.processlist GROUP BY user, host, db, command, state ORDER BY count DESC;

▸Triaging the Three Core Scenarios

Error 1040 stems from one of three fundamentally different failure modes:

1. Connection Leak

  • •Symptom: High proportion of command = 'Sleep', durations reaching hundreds of seconds, concentrated on a single application host.
  • •Root Cause: Application code fails to close connections, or an ORM pool fails to reclaim idle connections.
  • •Critical Takeaway: Increasing max_connections here does not resolve the issue; it merely inflates RAM usage and delays the crash.

2. Connection Storm

  • •Symptom: Hundreds of short-lived sessions created within the same second across diverse client IPs.
  • •Root Cause: Container rollouts, concurrent cron triggers, aggressive load balancer health checks, or unthrottled reconnect loops following an upstream hiccup.

3. Query Bottleneck & Lock Contention

  • •Symptom: Threads_running is elevated, with very few sessions in Sleep.
  • •Root Cause: Connections are not idle; they are waiting on row/table locks or executing unindexed table scans. Each incoming request spins up another connection, quickly reaching the threshold.

▸Step-by-Step Mitigation & Resolution

1. Prune Idle Sleep Connections

Identify long-dormant sessions:

sql
SELECT id, user, host, time, LEFT(info, 100) AS current_query FROM information_schema.processlist WHERE command = 'Sleep' AND time > 300 ORDER BY time DESC;

Terminate the target connection:

sql
KILL CONNECTION 123456;
NOTE
KILL CONNECTION vs KILL QUERY: KILL CONNECTION severs both the executing statement and the underlying TCP connection. KILL QUERY terminates only the active statement while leaving the session open. Calling KILL QUERY on an idle (Sleep) connection has zero effect.

2. Synchronize Idle Timeouts with Application Pool Lifespans

Ensure MySQL's wait_timeout and interactive_timeout are greater than the application connection pool's maxLifetime. If the server closes an idle connection before the pool reclaims it, the client attempts to use a dead socket, causing Communications link failure.

3. Isolate Blast Radii with Per-User Limits

Prevent non-critical services from starving the entire engine:

sql
CREATE USER 'reporting_app'@'10.0.2.%' IDENTIFIED BY 'StrongPassword' WITH MAX_USER_CONNECTIONS 25;

4. Account for Memory Overhead Before Bumping Limits

Every MySQL connection incurs base memory allocations (thread_stack) plus variable query buffers (sort_buffer_size, read_buffer_size, join_buffer_size). Arbitrarily scaling max_connections from 200 to 2,000 can exhaust system RAM and trigger the Linux OOM-Killer. The production-grade solution is deploying an external pooler such as ProxySQL to multiplex thousands of client connections onto a lean pool of database threads.


▸Related Error Guides & Hub Links

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (50)