MySQL 'Too many connections' when PHP opens database connections
Too many connectionsShort 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.
Fix it fast
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';"2Match what you see, then jump to its fix
- 1
Max_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
The process list shows many Sleep rows with large Time values for the application user.Connections are held open longer than the work needsFix - 3
The connection count stays high even when traffic is low, and PDO::ATTR_PERSISTENT is set.Persistent connections multiply across PHP workersFix
- 1
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
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
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_PERSISTENT3Verify
After removing it and restarting PHP, the count drops after each request.
SHOW PROCESSLIST;Confirmed by php.net
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
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 fixDatabases
Laravel query fails: conversion from one collation into another is impossible
SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8_unicode_ci into utf8mb4_0900_ai_ci impossible for parameter
MySQL is comparing or binding a value whose character set cannot be converted into the one on the other side.
3 causes, each with a fixDatabases
PHP cannot connect to MySQL: the connection attempt failed
Error 2003
Error 2003 means the TCP connection to MySQL never opened.
4 causes, each with a fixDatabases
The client cannot reach the MySQL or MariaDB server through its Unix socket file
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2) (MariaDB client wording)
Error 2002 means the client tried the Unix socket file named in the message and found nothing it could talk to.
4 causes, each with a fixDatabases
MySQL or MariaDB: Column count of mysql.proc is wrong, table probably corrupted
Error 1558: Column count of mysql.proc is wrong
The mysql.proc system table has a different number of columns than the running server expects.
3 causes, each with a fixDatabases
Saving an empty date from PHP into a MySQL DATETIME column fails
The column holds 0000-00-00 00:00:00 instead of NULL
An empty string is not NULL.
4 causes, each with a fix