Skip to content
Databases2 min read

Fix MySQL error 1040: Too many connections

ERROR 1040 (HY000): Too many connections

Short answer

Error 1040 means the number of open connections to MySQL reached max_connections, so new ones are refused until others close. It's usually caused by slow queries, connections that never close, or uncached traffic, and the fix is using fewer connections, not only raising the limit.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. On your own server
  4. Prevention
  5. On ArabyHost

Every visit to a dynamic page opens a database connection, which stays busy until the page finishes. When connections pile up faster than they close, the server reaches max_connections and refuses new ones. In PHP the message reads:

SQLSTATE[HY000] [1040] Too many connections

WordPress visitors see Error establishing a database connection instead.

This is a server-wide limit. If your message mentions max_user_connections, you’re looking at error 1203, the per-user limit.

Causes

  • Lots of traffic to uncached pages, from real visitors or bots.
  • Slow queries: each request holds its connection longer, so connections stack up.
  • Persistent connections via mysqli_pconnect or PDO::ATTR_PERSISTENT that stay open while idle.
  • Long scripts, cron jobs or background workers that open a connection and never close it.
  • Locked tables (especially MyISAM) that make queries wait while holding their connections.
  • A low max_connections on a self-managed server that was never tuned.

How to fix it

  1. Look at current connections in phpMyAdmin’s SQL tab:
SHOW PROCESSLIST;

On shared hosting you only see your own user’s connections. Lots of Sleep rows mean connections left open; lots of Query rows with a high Time mean slow queries.

  1. Turn off persistent connections in PHP:
$pdo = new PDO($dsn, $user, $pass, [
    PDO::ATTR_PERSISTENT => false,
]);
  1. Cache your pages. On WordPress, LiteSpeed Cache keeps most visits from touching the database at all.

  2. Speed up slow queries with indexes. Check the query first:

EXPLAIN SELECT * FROM orders WHERE customer_id = 42;
CREATE INDEX idx_orders_customer ON orders (customer_id);
  1. Close connections in long scripts once they’re done: $pdo = null; in PHP, or DB::disconnect(); inside a long loop in Laravel.

  2. Block abusive crawlers that request many pages in a short time, with robots.txt or blocking rules.

On your own server

With root access, check the limit and the peak you’ve reached:

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

Then raise it in the MySQL or MariaDB config, as far as memory allows:

[mysqld]
max_connections = 300

Prevention

  • Treat caching as part of the site, not an extra.
  • Watch for slow queries after every new plugin or feature.
  • In Node.js apps, use a single small pool instead of a connection per request.

On ArabyHost

Every ArabyHost account has a limit on simultaneous database connections, and there’s no root access to change server settings. So the fix is always on your site’s side: close connections when you’re done, avoid persistent connections, and cache. The web server is LiteSpeed, with LiteSpeed Cache for WordPress; the setup is in the WordPress speed guide. If the error keeps coming back, the Araby AI assistant reads your error log and shows when it happens and why.

FAQ

What's the difference between error 1040 and 1203?

1040 means the whole server reached its connection limit (max_connections); 1203 means a single database user reached its own limit (max_user_connections). Either way, the fix is fewer open connections.

Should I just raise max_connections?

On your own server you can, as far as memory allows, since each connection uses some. But it doesn't fix slow queries or connections that never close, so deal with those first.

Do persistent connections make a site faster?

They save the time to open a connection, but keep idle connections reserved. On hosting with a per-account connection limit they trigger this error sooner, so it's better to turn them off.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.