MySql Proccesslist filled with "Sleep" Entries leading to "Too many Connections"?

MySql Proccesslist filled with "Sleep" Entries leading to "Too many Connections"?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

MySQL Processlist Filled with "Sleep" Entries leading to "Too many Connections"?

When you run SHOW PROCESSLIST and see hundreds of entries stuck in the Sleep state, it usually signals a resource leak rather than high concurrency. In your specific scenario, 400 idle connections from five web servers are accumulating until MySQL hits its limit. The fact that these connections vanish only after restarting Apache is the critical clue: the application layer is failing to close the socket before the process exits or the script terminates.

The underlying mechanism here relies on how PHP handles database resources and what "persistent connections" actually mean. You mentioned that none of your projects use persistent connections, which implies you are relying on the default behavior where mysqli_connect() or PDO creates a new session for every request. Ideally, once a PHP script finishes executing, the Web Server (Apache) destroys the associated process, taking any open database handles with it.

However, if Apache is configured to handle concurrent requests efficiently—perhaps using multiple worker processes or threads—and your scripts are not explicitly cleaning up resources, you risk leaving sockets open. This often happens when an exception occurs before the close() function is called, or when a global singleton manages connections but fails to reset them between requests. Even if your code doesn't explicitly call $conn->close(), modern PHP versions should handle cleanup on process termination. If they aren't, it suggests a logic error in how your application lifecycle interacts with the database driver.

The configuration snippet you provided shows max_connections = 450. With 400 sleeping connections and only 20–100 active ones, you are dangerously close to exhaustion. When a new request arrives, MySQL checks the process list. If it sees too many slots occupied—even if they are sleeping—it may reject the connection immediately or force existing connections to close, leading to the "Too many connections" error. The update in your source material notes that concurrent requests never exceed 50. This discrepancy confirms that the bottleneck is not request throughput but rather the accumulation of orphaned sessions.

To prevent this, you must ensure that every database interaction block ends with an explicit resource cleanup. While PHP's garbage collector will eventually free memory, it does not guarantee that the underlying OS file descriptor for the MySQL socket is closed immediately upon script end, especially under high load or if the process hangs momentarily. The most robust solution is to wrap your database logic in a try...finally block (or use a context manager pattern) where the connection is explicitly closed in the finally clause:

$conn = new PDO('mysql:host=localhost;dbname=database', $user, $pass);
try {
    // Execute query
    $stmt = $conn->query('SELECT * FROM users');
} catch (PDOException $e) {
    // Handle error
} finally {
    if ($conn && !$conn->inTransaction()) {
        $conn->close();
    }
}

Using PEAR::DB or Zend Framework adapters often requires checking the adapter's state or explicitly calling disconnect(). If you are using MySQLi, ensure you aren't accidentally reusing a static connection object across requests without resetting it.

Another potential pitfall involves Apache's worker model. If MaxRequestWorkers is set high but your scripts run slowly (due to long queries missing indexes, as hinted by your slow query log configuration), the processes holding open sockets persist longer than expected. While raising max_connections in my.cnf is an option, it treats the symptom rather than the cause and increases memory overhead for the MySQL server without solving the leak.

If you are using Laravel or modern PHP frameworks, always utilize their built-in connection pooling features if they exist, or ensure your service container correctly scopes connections per request. For legacy codebases using raw extensions like the deprecated mysql module, consider migrating to PDO or MySQLi immediately, as the old extension lacks proper resource management in many edge cases.

Ultimately, the sleeping entries are a direct result of unclosed sockets. Fixing the code to guarantee closure on every path—success or failure—is the only sustainable fix. Monitoring SHOW PROCESSLIST regularly will help you catch leaks early before they trigger connection errors during traffic spikes.

Our network