HomeBlogMySQL "Too Many Connections" (Error 1040): Causes and Fixes
Performance & Tuning3 November 20266 min read

MySQL "Too Many Connections" (Error 1040): Causes and Fixes

Raising max_connections hides the problem. Usually something is holding connections open: slow queries, a bot flood or too many PHP workers. Here is how to find which.

🗄️

Short answer: MySQL and MariaDB accept 151 connections by default. Error 1040 means every slot is taken, usually because slow queries hold connections open while new visitors keep arriving, or because the PHP workers across all sites can open more connections than the database allows. Find what is holding them before raising the limit; each extra connection costs memory.

What WordPress shows

WordPress shows "Error establishing a database connection". The real error, Too many connections, is in the PHP or MySQL error log.

Look at the numbers

sql
SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW GLOBAL STATUS LIKE 'Max_used_connections';
-- what are the connections doing right now?
SHOW FULL PROCESSLIST;

MySQL always keeps one extra slot for an account with the CONNECTION_ADMIN or SUPER privilege, so an administrator can still log in to investigate.

Causes and fixes

What the process list showsCauseFix
Many queries running for secondsSlow queries (missing index, huge wp_options autoload)Turn on the slow query log; add the index or fix the plugin
Many Sleep connectionsPersistent connections or a long wait_timeoutLower wait_timeout; avoid persistent connections in PHP
Same query from many sessionsA bot or crawler flood on uncached pagesCache pages; rate-limit or block the bot
Connections spread across many sitesTotal PHP workers exceed max_connectionsSize PHP workers per site so their sum stays below the limit

If you do raise the limit

ini
# /etc/mysql/mysql.conf.d/mysqld.cnf (path varies by distribution)
[mysqld]
max_connections = 300
# per-user cap, so one site cannot take every slot
max_user_connections = 60

Check memory first. Each connection uses its own buffers, so doubling max_connections on a small VPS can move the problem from MySQL to the out-of-memory killer.

On GHP Cloud

Each site runs its own PHP pool with its own limits, so one busy site cannot take every PHP worker on the server. A per-site page cache (1 minute to 24 hours) means most visits never reach PHP or MySQL, and the resource graphs show which site was busy when the errors started.

Frequently asked questions

What is the default max_connections in MySQL?

151 in both MySQL and MariaDB.

Will restarting MySQL fix too many connections?

For a few minutes. If the cause is slow queries or a bot, the slots fill up again.

How many connections does WordPress need?

About one per PHP request being processed at that moment, so it tracks the number of busy PHP workers, not the number of visitors.

G

GHP Team

GHP Cloud

Manage WordPress sites the smart way

GHP Cloud gives you a managed server in India with a firewall, malware scanning and daily backups, billed in INR with a GST invoice — from ₹1,450/month.