How can I see how many MySQL connections are open?

How can I see how many MySQL connections are open?

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

Stefan Bogdanescu

Founder & Senior Architect

How to Audit MySQL Connection Counts in Legacy PHP Applications

Determining exactly how many database connections are active during a specific request in a legacy PHP environment can be tricky, especially when relying on the deprecated mysql_connect function. The behavior you are describing—where multiple calls with identical credentials return the same connection object rather than opening new sockets—is standard connection pooling logic handled by the MySQL client library itself. This ensures that your application doesn't exhaust server resources by creating thousands of simultaneous sockets for a single request.

However, validating whether this pooling is actually happening or if a rogue code path is inadvertently leaking connections requires looking at the server side. You cannot reliably detect this count strictly within Apache logs or standard PHP error logs because these systems track requests and errors, not specific database socket lifecycle events with that level of granularity. To get an accurate read on open connections, you must query the MySQL server directly.

The most robust method to see how many connections are currently established is by querying the information_schema.processlist table or the SHOW PROCESSLIST command. This returns a list of all active threads, including both user connections and system threads (like background dump threads).

You can run this query from your MySQL client:

SELECT COUNT(*) AS open_connections 
FROM information_schema.processlist 
WHERE COMMAND != 'Sleep';

Filtering out Sleep is crucial. When a connection pool holds idle connections waiting for the next query, they remain in the Sleep state. If you include them in your count, you might think you have thousands of active users when you actually just have a large pool of idle sockets. By excluding Sleep, you see only connections that are actively executing or waiting within the request lifecycle.

For a more detailed breakdown, inspecting the User column in processlist helps identify which specific application is consuming resources. If you notice a spike in connections from a specific user that doesn't correlate with current traffic patterns, it often indicates a missing transaction commit or a failure to close the resource properly after use.

Another critical metric available on most MySQL configurations is the variable max_connections. You can check this by running:

SHOW VARIABLES LIKE 'max_connections';

If your count from processlist approaches this limit, you are in danger of hitting the connection wall. When the server reaches its maximum allowed connections, new requests will fail immediately with an error like "Too many connections," regardless of how efficiently your PHP code manages pooling. This is a hard limit enforced by the OS file descriptors and the MySQL thread pool.

In legacy systems using mysql_connect, there was no native session mechanism to track connection state between calls in the way modern PDO or mysqli extensions allow with persistent connections (PDO::ATTR_PERSISTENT or mysqli::$persistent_connect). Without these flags, every script execution on Apache theoretically starts fresh, but the underlying library caches the socket. If your legacy code is not explicitly closing the handle (which the old mysql_* functions often did automatically on scope exit), you risk accumulating leaks that bypass the logic you expect.

To log this data for long-term analysis without modifying every line of legacy PHP code, consider creating a scheduled cron job that runs the SHOW PROCESSLIST query every few minutes and dumps the results to a CSV file or logs them into an application dashboard. This provides a history of connection usage that can help you identify if specific scripts are causing spikes before they hit the max_connections ceiling.

If you are migrating away from this legacy stack, evaluating a modern framework like Laravel is often necessary. Modern frameworks enforce strict resource management and provide clear visibility into connection counts through built-in debugging tools and middleware. For more information on managing database connections in modern PHP applications, visit https://laravelcompany.com.

Our network