php, mysql - Too many connections to database error

php, mysql - Too many connections to database error

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

Stefan Bogdanescu

Founder & Senior Architect

Why "Too Many Connections" Hits Small Applications First

The error SQLSTATE[HY000] [1040] Too many connections often appears in stack traces alongside a misleading implication: that your application has reached its intended capacity. In the specific case described, the application was serving only eight concurrent users when the crash occurred, yet the database refused new connections. This discrepancy usually stems from a misunderstanding of how connection pools interact with long-running processes rather than raw user count.

When an application architecture relies on fetching data every two seconds via AJAX, the PHP script does not simply execute a query and exit. Instead, it establishes a connection to the MySQL server, executes SELECT, and then holds onto that physical TCP socket while waiting for the next request cycle or until the script terminates. If these scripts are long-lived or if the application server fails to close connections after a request finishes, those sockets accumulate on the MySQL side.

MySQL maintains two distinct types of connection limits: max_connections and connect_timeout. The error in question occurs when the number of active TCP connections exceeds the max_connections setting in your my.cnf or my.ini file. By default, this limit is often set to 151 on Linux systems, but production environments frequently raise it to 1000 or more. The fact that 8 users triggered the error suggests that each user's session spawned multiple connections, or worse, that connections were never being released back to the pool.

The stack trace provided indicates the use of Zend_Db_Adapter_Pdo_Mysql. While legacy frameworks like Zend Framework are still in use, modern PHP development typically utilizes PDO directly or an ORM like Eloquent within the Laravel ecosystem. The core issue remains identical regardless of the abstraction layer: if a script connects to the database and does not explicitly close the connection at the end of execution, that connection remains open on the server side until it times out.

In a polling architecture where data is fetched every two seconds, the risk is high. If a user leaves the chat page open for an hour, they might have established ten separate connections to fetch data, or worse, if the PHP process hangs after fetching data and before closing the socket, those sockets sit idle but active. MySQL treats these as "in use" connections. When the 15th user joined yesterday, perhaps a background process or a specific script failure prevented cleanup, pushing the total over the limit.

To resolve this, you must first inspect the current state of your database connections using the following command:

mysql -u root -e "SHOW PROCESSLIST;"

Look for threads in the Sleep state that have been idle for more than a few seconds. These are likely leaked connections. You can also check the current limits with:

SHOW VARIABLES LIKE 'max_connections';
SHOW VARIABLES LIKE 'wait_timeout';

If your application server is using PHP-FPM or Apache, ensure that your configuration explicitly closes database handles. In PDO, this means ensuring you are not relying on the connection being implicitly closed by the web server's request lifecycle if you are manually managing resources in a custom loop. If you are using a persistent connection (PDO::ATTR_PERSISTENT => true), be extremely cautious. Persistent connections allow PHP to reuse a socket across requests, which saves overhead but can lead to stale or leaked connections if the application logic doesn't properly handle disconnections.

The most robust fix involves refactoring the polling mechanism. Instead of opening and closing connections on every 2-second interval, establish a single persistent connection per user session (or use a shared connection pool if using an application server like Node.js with a proxy). Alternatively, switch to WebSockets or Server-Sent Events (SSE) where the server pushes updates rather than the client polling. This eliminates the need for repeated SELECT queries and drastically reduces the number of open sockets required per user.

If you are building this on top of Laravel, consider using the built-in event system or a queue worker to handle real-time updates asynchronously, ensuring that database interactions are kept short-lived and properly managed within the framework's lifecycle. For more advanced scaling needs, resources like https://laravelcompany.com offer insights into building scalable applications with modern PHP stacks, though the fundamental principle of connection hygiene remains universal: open a connection only when needed, and close it immediately after use.

Our network