Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL says Too many connections to a Node.js app

ErrorError: ER_CON_COUNT_ERROR: Too many connections

Short answer

MySQL refuses new clients once max_connections slots are in use. In a Node.js app the slots are usually held by code that opens a connection per request and never releases it, or by many processes each with its own pool. Count the connections per client first, then fix the leak or lower the pool size.

  • Applies to Node.js 22, mysql 2.18 (mysqljs), mysql2 3.x, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Connect as an administrator (the server keeps one extra slot for accounts with CONNECTION_ADMIN or SUPER) and see who holds the connections.

    SHOW STATUS WHERE `variable_name` = 'Threads_connected';
    SELECT @@max_connections;
    SHOW PROCESSLIST;
  2. 2Match what you see, then jump to its fix

    1. 1Threads_connected climbs steadily with traffic and many rows in the process list are in Sleep.Connections are opened per request and never releasedFix
    2. 2The process list shows the same app user from several hosts, and the total is roughly processes multiplied by connectionLimit.Several app processes each hold a full poolFix
    3. 3Connections are released correctly and the pool is small, yet Threads_connected sits at the limit at peak.max_connections is simply too low for the real workloadFix

What does the error look like?

  • Error: ER_CON_COUNT_ERROR: Too many connections
  • Too many connections

Cause 1 of 3

Connections are opened per request and never released

1Check

Look for createConnection() inside request handlers, or pool.getConnection() without a matching release().

grep -rn "createConnection\|getConnection" src/

2Fix

Create one pool at start-up, share it, and release every connection you take from it.

// db.js: created once
const mysql = require('mysql');
module.exports = mysql.createPool({ connectionLimit: 10, host: 'db.internal', user: 'app', password: process.env.DB_PASSWORD, database: 'app' });

// handler
pool.getConnection((err, connection) => {
  if (err) return next(err);
  connection.query('SELECT 1', (error, rows) => {
    connection.release();            // always release
    if (error) return next(error);
    res.json(rows);
  });
});

3Verify

Run a load test and watch Threads_connected stay under the pool limit.

SHOW STATUS WHERE `variable_name` = 'Threads_connected';

Confirmed by github.com

Did this fix it?

Cause 2 of 3

Several app processes each hold a full pool

1Check

Add up connectionLimit across every instance: PM2 cluster workers, containers, serverless functions. The server counts all of them against one max_connections.

pm2 status

2Fix

Lower connectionLimit so that instances multiplied by the limit stays well below max_connections, or lower the number of instances.

const pool = mysql.createPool({ connectionLimit: 5, /* ... */ });

3Verify

Restart the app and compare Threads_connected with max_connections under normal traffic.

SHOW STATUS WHERE `variable_name` = 'Threads_connected';
SELECT @@max_connections;

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

max_connections is simply too low for the real workload

1Check

Compare the limit with the number of connections in use at peak.

SHOW STATUS WHERE `variable_name` = 'Threads_connected';

2Fix

Raise max_connections. On Amazon RDS for MySQL the limit defaults to a value based on the instance memory, and you change it in the DB parameter group.

# my.cnf, [mysqld] section, then restart MySQL
max_connections = 500

3Verify

Read the value back and watch the error stop at peak.

SELECT @@max_connections;

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • One pool per process, created at start-up.
  • Release every pooled connection in the error path too.
  • Write down the pool size of every service that shares the database.
  • Alert on Threads_connected above 80 percent of max_connections.

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

SequelizeUniqueConstraintError: Validation error on insert

Unhandled rejection SequelizeUniqueConstraintError: Validation error

Sequelize sends the insert, MySQL rejects it because a unique column or unique index already holds that value, and Sequelize reports it as SequelizeUniqueConstraintError.

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.