Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Node.js MySQL: Cannot enqueue Query after a fatal error

ErrorError: 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.

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

Fix it fast

  1. 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);
    });
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The message ends with 'after invoking quit' or 'after being destroyed' instead of 'after fatal error'.The connection was already ended or destroyedFix

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.js

Confirmed by github.com

Did this fix it?

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/health

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

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.