Node.js MySQL: Cannot enqueue Query after a fatal error
Error: Cannot enqueue Query after fatal error.Short answer
This error is a symptom, not the problem. An earlier fatal error already killed that connection object, and your code kept running queries on it. A connection cannot be repaired. Find the first error in the log, then create a new connection or, better, use a pool that hands out a fresh one.
Fix it fast
1Find out which cause you have
Search the log for the first error that has fatal: true. The enqueue error only repeats after that one, so the first one is the real problem.
connection.on('error', (err) => { console.error(err.code, 'fatal =', err.fatal, err.message); });2Match what you see, then jump to its fix
- 1
The first error has fatal: true (for example PROTOCOL_CONNECTION_LOST), and every later query fails with Cannot enqueue Query after fatal error.One shared connection is reused after a fatal errorFix - 2
The message ends with 'after invoking quit' or 'after being destroyed' instead of 'after fatal error'.The connection was already ended or destroyedFix
- 1
What does the error look like?
Error: Cannot enqueue Query after fatal error.code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR'Cannot enqueue Query after invoking quit.
Cause 1 of 2
One shared connection is reused after a fatal error
1Check
A fatal error ends the connection object. The package states that re-connecting means establishing a new connection. If your code created one connection at start-up and kept it, every query after that error is refused.
grep -rn "createConnection" src/2Fix
Replace the single connection with a pool. A pool drops a disconnected connection and opens a new one on the next getConnection call.
const mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
pool.getConnection((err, connection) => {
if (err) throw err;
connection.query('SELECT 1 + 1 AS two', (error, rows) => {
connection.release();
if (error) throw error;
console.log(rows[0].two);
});
});3Verify
Stop MySQL for a few seconds in a test setup, start it again, and run a query: the next call should succeed on a new connection.
sudo systemctl restart mysql
node app.jsConfirmed by github.com
Cause 2 of 2
The connection was already ended or destroyed
1Check
The package refuses new queries once connection.end() or connection.destroy() has run, and once pool.end() has been called the pool can no longer hand out connections. Look for an end() call that runs too early, for example in a finally block of a request handler.
grep -rn "\.end()\|\.destroy()" src/2Fix
Call end() only once, at process shutdown. For a pooled connection, call release() when the request is done, not end().
pool.getConnection((err, connection) => {
if (err) throw err;
connection.query('SELECT 1', (error) => {
connection.release(); // back to the pool, still usable
if (error) throw error;
});
});3Verify
Send two requests one after the other and confirm both return data.
curl -s http://127.0.0.1:3000/health && curl -s http://127.0.0.1:3000/healthConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Use a pool for any long-running Node.js server.
- Always attach an error handler, and log err.code and err.fatal.
- Release pooled connections in every code path, including errors.
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
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
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 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
MySQL says Too many connections to a Node.js app
Too many connections
MySQL refuses new clients once max_connections slots are in use.
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 fixDatabases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fix