Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL 'Too many connections' when PHP opens database connections

ErrorToo many connections

Short answer

Every connection slot up to max_connections is in use. Log in with an account that has CONNECTION_ADMIN, which gets one extra slot, and run SHOW PROCESSLIST to see who holds them. Then close PDO objects when done, avoid persistent connections piling up, and raise max_connections only if the load is real.

  • Applies to MySQL 8.4, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Use an administrator account (it can connect even when the limit is reached) and see who is holding connections and what the limit and peak are.

    mysql -u root -p -e "SHOW PROCESSLIST; SHOW VARIABLES LIKE 'max_connections'; SHOW GLOBAL STATUS LIKE 'Max_used_connections';"
  2. 2Match what you see, then jump to its fix

    1. 1Max_used_connections equals max_connections and many rows in the process list belong to the web application's user.More concurrent PHP requests than connection slotsFix
    2. 2The process list shows many Sleep rows with large Time values for the application user.Connections are held open longer than the work needsFix
    3. 3The connection count stays high even when traffic is low, and PDO::ATTR_PERSISTENT is set.Persistent connections multiply across PHP workersFix

What does the error look like?

  • Too many connections

Cause 1 of 3

More concurrent PHP requests than connection slots

1Check

Each running PHP request opens its own connection. The server permits max_connections plus one extra for accounts with CONNECTION_ADMIN, so an admin can still get in.

SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Max_used_connections';

2Fix

If the traffic is real, raise the limit. SET PERSIST also writes it to mysqld-auto.cnf so it survives a restart. Do not grant CONNECTION_ADMIN to normal application users.

SET PERSIST max_connections = 300;

3Verify

The variable shows the new value.

SHOW VARIABLES LIKE 'max_connections';

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

Connections are held open longer than the work needs

1Check

A PDO connection stays active for as long as the PDO object is referenced; PHP closes it at the end of the script. In long scripts, workers or queue jobs the connection lives as long as the process does.

SHOW PROCESSLIST;

2Fix

Release the connection as soon as the work is done by assigning null to the PDO object and to every PDOStatement that references it.

$sth = $dbh->query('SELECT * FROM foo');
$rows = $sth->fetchAll();
$sth = null;
$dbh = null;

3Verify

Sleep rows for the application user disappear from the process list after the job.

SHOW PROCESSLIST;

Confirmed by php.net

Did this fix it?

Cause 3 of 3

Persistent connections multiply across PHP workers

1Check

Persistent connections are not closed when the script ends. They are cached and reused for later requests with the same credentials, so each PHP worker process can hold one open.

grep -rn "ATTR_PERSISTENT" src/ config/

2Fix

Remove the option, or keep the number of PHP workers well below max_connections. PDO does no cleanup of leftover state on persistent connections, which is another reason to drop it.

$dbh = new PDO($dsn, $user, $pass);   // no ATTR_PERSISTENT

3Verify

After removing it and restarting PHP, the count drops after each request.

SHOW PROCESSLIST;

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Alert when Max_used_connections gets close to max_connections.
  • Keep PHP worker counts below the connection limit.
  • Grant CONNECTION_ADMIN to administrators only, so one slot is always free for diagnosis.
  • Close PDO objects in long-running scripts as soon as the work is done.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

Laravel bulk insert fails with 'too many placeholders'

SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders

Laravel sends one placeholder per value, so one insert of many rows times many columns, or a whereIn with a huge list, creates a statement MySQL refuses.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.