Database
mySQL errors
Troubleshooting
Connection limits
Error 1040

mySQL Error 1040 Too Many Connection

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

MySQL error 1040 means the server has run out of available client connection slots. The immediate symptom is connection failure, but the real fix is usually a mix of diagnosis, connection cleanup, query tuning, and sometimes a higher max_connections value.

What Error 1040 Actually Means

MySQL's reference manual states that Too many connections happens when all permitted client connections are already in use. The limit is controlled by the max_connections system variable.

In MySQL 8, the server also reserves an extra administrative connection for accounts with the CONNECTION_ADMIN privilege. That is important because it gives administrators a way to get in and diagnose the problem even when normal clients are locked out.

Check The Current Situation

Once connected as an administrator, start with visibility.

sql
SHOW STATUS LIKE 'Threads_connected';
SHOW VARIABLES LIKE 'max_connections';
SHOW FULL PROCESSLIST;

These commands tell you how many connections are active, what the configured limit is, and what those sessions are doing. Long-idle sessions, sleeping clients, and slow queries are the usual clues.

Short-Term Fix: Raise max_connections

If the workload is legitimate and the server has enough memory, increase the limit.

sql
SET GLOBAL max_connections = 300;

To persist the change, update your MySQL configuration file.

ini
[mysqld]
max_connections = 300

Then restart MySQL if needed for the persistent config path you use. Raising the limit can help, but it should not be the only response because each connection consumes server resources.

Fix Connection Leaks In The Application

Many 1040 incidents are caused by application code that opens connections and does not return them to a pool quickly enough. The durable fix is often in the client layer.

Examples of healthier patterns include:

  • using connection pools such as HikariCP or SQLAlchemy pools
  • closing connections promptly
  • avoiding one-connection-per-request designs without pooling
  • limiting background worker fan-out

If the application leaks connections, a larger max_connections just delays the next outage.

Reduce Time Spent Holding Connections

Slow queries and blocked transactions keep sessions alive longer than necessary. That reduces effective capacity even if traffic volume is unchanged.

Start by checking for:

  • missing indexes
  • large table scans
  • long-running reports sharing the main database
  • transactions left open while application code waits on external systems

The faster each request finishes, the fewer concurrent sessions are needed.

Use Pooling And Timeouts Carefully

Connection pools should have sensible upper bounds. If every application instance opens an oversized pool, the total potential connections across the fleet can exceed what MySQL can support.

The problem is often architectural, not just per-process. Ten services with 100-connection pools can overwhelm a database long before traffic justifies it.

Emergency Cleanup

If a burst of stuck sessions is causing the outage, you may need to terminate specific connections.

sql
KILL 12345;

Use this carefully and only after identifying the right sessions from SHOW FULL PROCESSLIST. Killing active write transactions can interrupt application work.

Common Pitfalls

A common mistake is increasing max_connections without checking memory capacity. More sessions can mean more memory pressure and a less stable server.

Another mistake is ignoring sleeping or leaked connections from the application tier. Database configuration alone will not solve a connection-management bug.

It is also easy to forget about pool sizing across multiple app instances. Each process may look reasonable in isolation while the combined pool size is excessive.

Summary

  • Error 1040 means all available MySQL client connections are in use.
  • Inspect Threads_connected, max_connections, and SHOW FULL PROCESSLIST first.
  • Increase max_connections only when the server can support it.
  • Fix connection leaks, pool sizing, and slow queries for the durable solution.
  • Use the reserved administrative access path to diagnose overloaded systems safely.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.