The MySQL server is refusing new client connections because it has reached its configured maximum number of simultaneous connections.
Here’s what’s actually breaking: A client application is attempting to establish a new TCP connection to the mysqld process, but mysqld is rejecting it because the max_connections limit, set in its configuration, has been hit. This is a hard limit; once it’s reached, no new connections are accepted until existing ones are closed.
Common Causes and Fixes
-
Persistent Connections Not Being Closed: Applications that open connections and don’t explicitly close them, or improperly handle connection pooling, will exhaust
max_connectionsover time.- Diagnosis: Monitor active connections. Run
SHOW PROCESSLIST;orSHOW FULL PROCESSLIST;on your MySQL server. Look for a large number of connections in aSleepstate that have been open for a long time. - Fix: Review your application’s connection management. Ensure every
connect()is paired with aclose()or that your connection pool correctly reclaims and reuses idle connections. For long-runningSleepconnections, you might need to implement a client-side timeout or a server-sidewait_timeoutandinteractive_timeoutadjustment.- Example
my.cnfadjustment (if persistent connections are desired but need a ceiling):[mysqld] wait_timeout = 60 interactive_timeout = 60 - Why it works:
wait_timeout(for non-interactive clients) andinteractive_timeout(for interactive clients) define how long the server waits for activity on a connection before closing it. Lowering these values forces the server to prune idle connections, freeing up slots.
- Example
- Diagnosis: Monitor active connections. Run
-
max_connectionsSet Too Low: The server’smax_connectionssetting is simply insufficient for the normal operational load of your applications.- Diagnosis: Check the current
max_connectionsvalue:SHOW VARIABLES LIKE 'max_connections';. If this number is consistently reached withSHOW PROCESSLIST;showing many active (not just sleeping) connections, it’s likely too low. - Fix: Increase
max_connectionsin your MySQL configuration file (my.cnformy.ini).- Example
my.cnfadjustment:[mysqld] max_connections = 500 - Why it works: This directly raises the ceiling on how many clients the MySQL server will accept simultaneously.
- Example
- Diagnosis: Check the current
-
Connection Pooling Misconfiguration: If your application uses a connection pool, it might be configured to keep too many connections open, or it might not be correctly releasing them.
- Diagnosis: Examine your application’s connection pool settings (e.g.,
maximumPoolSize,minimumIdle,connectionTimeout). Check if the pool size is excessively large relative to yourmax_connectionsor if connections are being held longer than necessary. - Fix: Tune your connection pool parameters. A common strategy is to set the pool’s maximum size slightly below or equal to
max_connections, and ensure idle connections are properly managed.- Example HikariCP config (in
application.properties):spring.datasource.hikari.maximum-pool-size=200 spring.datasource.hikari.idle-timeout=30000 - Why it works: Properly configured pooling ensures efficient reuse of connections and avoids unnecessary connection churn, while also respecting the server’s
max_connectionslimit.
- Example HikariCP config (in
- Diagnosis: Examine your application’s connection pool settings (e.g.,
-
Application Threads Exhausting Connections: A bug or design flaw in the application causes it to spawn a very large number of threads, each attempting to acquire a database connection simultaneously.
- Diagnosis: Use system monitoring tools (like
top,htop, or application-specific thread dumps) to see if the number of application threads is abnormally high when the error occurs. Correlate this withSHOW PROCESSLIST;. - Fix: Debug and refactor the application code to limit the number of concurrent threads that interact with the database. This might involve using thread pools or synchronizing access to database operations.
- Why it works: By reducing the number of threads actively trying to get a connection, you decrease the demand on the
max_connectionslimit.
- Why it works: By reducing the number of threads actively trying to get a connection, you decrease the demand on the
- Diagnosis: Use system monitoring tools (like
-
MySQL Server Under-resourced or Slow: A slow or overloaded MySQL server can cause connections to stay open longer than they should, even if they are technically "active" and not in
Sleep. This can be due to long-running queries, insufficient hardware, or heavy I/O.- Diagnosis: Check
SHOW PROCESSLIST;for queries that are running for a very long time (Timecolumn). Monitor server CPU, memory, and I/O utilization. Check the MySQL slow query log. - Fix: Optimize slow queries, add indexes, upgrade server hardware, or tune MySQL’s buffer pool and other memory-related settings.
- Example
my.cnfoptimization (if memory is available):[mysqld] innodb_buffer_pool_size = 4G - Why it works: Faster query execution and better resource utilization mean connections are released more quickly, reducing the sustained load on
max_connections.
- Example
- Diagnosis: Check
-
Frequent Application Restarts: If your application restarts frequently (e.g., due to crashes or deployments), and it doesn’t properly close its connections before exiting, each restart can leave behind lingering connections that were not cleanly terminated.
- Diagnosis: Observe the number of connections in
SHOW PROCESSLIST;over time, especially around application deployment or restart events. Look for connections that don’t disappear even after the application should be down or fully restarted. - Fix: Implement graceful shutdown procedures in your application to ensure all database connections are closed before the application process terminates.
- Why it works: A clean shutdown prevents orphaned connections from accumulating and consuming
max_connectionsslots.
- Why it works: A clean shutdown prevents orphaned connections from accumulating and consuming
- Diagnosis: Observe the number of connections in
After fixing the "Too Many Connections" error, you might encounter a "MySQL server has gone away" error if you’ve aggressively lowered wait_timeout and your application isn’t robust enough to handle unexpectedly closed connections.