MySQL says Too many connections to a Node.js app
Error: ER_CON_COUNT_ERROR: Too many connectionsShort 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.
Fix it fast
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;2Match what you see, then jump to its fix
- 1
Threads_connected climbs steadily with traffic and many rows in the process list are in Sleep.Connections are opened per request and never releasedFix - 2
The 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
Connections 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
- 1
What does the error look like?
Error: ER_CON_COUNT_ERROR: Too many connectionsToo 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
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 status2Fix
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
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 = 5003Verify
Read the value back and watch the error stop at peak.
SELECT @@max_connections;Confirmed by docs.aws.amazon.com
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:
- 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.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Databases
MySQL 'Too many connections' when PHP opens database connections
Too many connections
Every connection slot up to max_connections is in use.
3 causes, each with a fixDatabases
NestJS connects to MySQL at ::1 and is refused
Error: connect ECONNREFUSED ::1:3306
::1 is the IPv6 loopback address.
2 causes, each with a fixDatabases
Node.js MySQL: Cannot enqueue Query after a fatal error
code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR'
This error is a symptom, not the problem.
2 causes, each with a fixDatabases
Node.js cannot reach MySQL: connect ETIMEDOUT
code: 'ETIMEDOUT'
ETIMEDOUT on connect means packets to the database port got no answer at all.
4 causes, each with a fixDatabases
Node.js says the MySQL server closed the connection
code: 'PROTOCOL_CONNECTION_LOST'
The error means the MySQL server, or something between it and your app, closed a connection your code still held.
3 causes, each with a fixDatabases
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