1040 🐬 MySQL

MySQL ERROR 1040 (08004): Too many connections

MySQL reached max_connections and refuses new clients.

Seen on: MySQL Node.js PostgreSQL

Meaning

Each client connection uses memory, so MySQL caps them (default 151). Leaking connections, unbounded worker counts, persistent connections from many PHP-FPM children, or slow queries piling up exhaust the limit. Shared hosting often has a per-user cap (max_user_connections, error 1203).

Common causes

  • Connections not closed / connection leaks
  • Too many app workers each holding a pool
  • Slow queries keeping connections busy so new ones pile up
  • Persistent connections × many PHP-FPM processes
  • Per-user limit on shared hosting

⚡ Quick fix

  1. See who is connected: SHOW PROCESSLIST;
  2. Fix slow queries causing pile-ups
  3. Right-size pools: workers × pool size < max_connections
  4. Raise max_connections only if RAM allows

Detailed fix by platform

MySQL

  1. Inspect and adjust:
    sql
    SHOW VARIABLES LIKE 'max_connections';
    SHOW STATUS LIKE 'Max_used_connections';
    SELECT user, host, COUNT(*) FROM information_schema.processlist GROUP BY user, host ORDER BY 3 DESC;
    SET GLOBAL max_connections = 300;   -- also persist in my.cnf

Node.js

  1. Create ONE pool per process (e.g. mysql2.createPool({ connectionLimit: 10 })), not a connection per request.

How to diagnose

  1. Usage — Max_used_connections vs max_connections
  2. Who — Which user/host holds most connections?
  3. State — Many in Sleep (leak) or Query (slow)?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.