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

  1. Persistent Connections Not Being Closed: Applications that open connections and don’t explicitly close them, or improperly handle connection pooling, will exhaust max_connections over time.

    • Diagnosis: Monitor active connections. Run SHOW PROCESSLIST; or SHOW FULL PROCESSLIST; on your MySQL server. Look for a large number of connections in a Sleep state that have been open for a long time.
    • Fix: Review your application’s connection management. Ensure every connect() is paired with a close() or that your connection pool correctly reclaims and reuses idle connections. For long-running Sleep connections, you might need to implement a client-side timeout or a server-side wait_timeout and interactive_timeout adjustment.
      • Example my.cnf adjustment (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) and interactive_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.
  2. max_connections Set Too Low: The server’s max_connections setting is simply insufficient for the normal operational load of your applications.

    • Diagnosis: Check the current max_connections value: SHOW VARIABLES LIKE 'max_connections';. If this number is consistently reached with SHOW PROCESSLIST; showing many active (not just sleeping) connections, it’s likely too low.
    • Fix: Increase max_connections in your MySQL configuration file (my.cnf or my.ini).
      • Example my.cnf adjustment:
        [mysqld]
        max_connections = 500
        
      • Why it works: This directly raises the ceiling on how many clients the MySQL server will accept simultaneously.
  3. 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 your max_connections or 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_connections limit.
  4. 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 with SHOW 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_connections limit.
  5. 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 (Time column). 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.cnf optimization (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.
  6. 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_connections slots.

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.

Want structured learning?

Take the full Express course →