Krister Johansson
Krister Johansson

Reputation: 711

Node JS Mysql PROTOCOL ENQUEUE AFTER FATAL ERROR

What does the error mean?

{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }

This code works in my test file:

function handleDisconnect() {
    objConn = mysql.createConnection(db_config);    // Recreate the connection, since
                                                    // the old one cannot be reused.
    objConn.connect(function(err) {                 // The server is either down
        if(err) {                                   // or restarting (takes a while sometimes).
            console.log('error when connecting to db:', err.code);
            setTimeout(handleDisconnect, 2000);     // We introduce a delay before attempting to reconnect,
        }else{
            console.log('Connected to db!');
        }                                           // to avoid a hot loop, and to allow our node script to
    });                                             // process asynchronous requests in the meantime.
                                                    // If you're also serving http, display a 503 error.
    objConn.on('error', function(err) {
        if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
            handleDisconnect();                       // lost due to either server restart, or a
        }else{
            throw err;
        }
    });
}

handleDisconnect();
megaLoop();

function megaLoop(){
    objConn.query('SELECT u.`email` FROM `users` as u', function(err, rows) {
        console.log(err);
        console.log(rows);
    });
    setTimeout(megaLoop, 100); 
}

But when I use the function in my Express App I get the error:

{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }

Why does it work in my test and not my app?

Upvotes: 20

Views: 55116

Answers (9)

Tarun Kantiwal
Tarun Kantiwal

Reputation: 301

I was facing exact same issue when connecting my nodejs app with dockerize mysql container

var mysql = require("mysql")

var connection = mysql.createConnection({
    host: "192.168.1.4",
    user: "root",
    password: "123456",
    port: "3307",
    database: "dbname"
})

connection.connect((err)=>{
  if (err) throw err;
  console.log("Connected to db")
})

module.exports = connection

This was the code which was written in config file and i had commented this piece of code

connection.connect((err)=>{
      if (err) throw err;
      console.log("Connected to db")
    })
    
 module.exports = connection

So i was getting this error hope it might help someone

Upvotes: 0

Ankit Kumar
Ankit Kumar

Reputation: 1

I also faced this problem when I am trying to connect mysql as this is an authentication problem you can solve this problem just executing the below query in mysql workbench or any mysql command line.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Upvotes: 0

tejasladani
tejasladani

Reputation: 1

I am facing same problem, when I was working on my project, its problem with database connection file. So first of all check credential for database connectivity is given right. And then check password for accessing database from MySQL. Past following code to your workplace in mySQL which set database password null or empty.

'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';

Upvotes: 0

Nu'man Arif
Nu'man Arif

Reputation: 19

Open MySql Workbench or MySql Command line and type this:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Now, your password for root user is 'password', use that in your DB Connection, it's work

Upvotes: 1

Bilal Mohmand
Bilal Mohmand

Reputation: 51

Open your MySQL command line client and run this command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_current_password';

Insert your password in <your_current_password>

Upvotes: 0

HimanshuGoyal
HimanshuGoyal

Reputation: 139

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

1.use the above query in your mysql workbench replace 'password' with your actual password 2.then restart your server

Upvotes: 0

Vuyani Daweti
Vuyani Daweti

Reputation: 27

I had the same issue , only to find out my DB name on Nodejs was incorrectly named

Upvotes: 0

Sawal Maskey
Sawal Maskey

Reputation: 648

I had similar issue connecting with MySQL. I solved this issue using connection pool. The concept is to get a connection from a connection pool only when required and release it after use. That way the connection will not be in unstable state. You can get implementation details here.

Upvotes: 14

Scott
Scott

Reputation: 782

I believe the issue is an express-sql-session. I'm experiencing the same issue right now. I think someone is on to something at this post: NodeJS running on MAC, and have an error occurred when deploying on centOS

Check this out too: https://github.com/felixge/node-mysql/issues/1166

Upvotes: 0

Related Questions