20

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?

1

9 Answers 9

14

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.

Sign up to request clarification or add additional context in comments.

4 Comments

this is the correct answer.. many thanks, I had the same issue and fixed it by following this article
Releasing connection every now and then doesn't sound like performance-wise solution.
@jayarjo I thought "release" in this context does not mean closing the connection. The connection remains active and is available in the pool to be used again. In fact, using a connection pool can improve performance by avoiding closing and reopening connections when you are running multiple requests asynchronously / in parallel.
This worked for me! tysm. I changed my .createConnection to .createPool ? Can someone explain why it worked thought?
1

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

Comments

0

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

5 Comments

Is this problem solved? I am getting exactly same issue. But I cannot find a solution in the answer here.
I'm not certain. I kind of gave up on mySQL (with node) and moved to mongoDB. I am still curious if the issue has be solved or not.
Why is this the accepted answer? It doesn't answer and just gives out more links.
@Scott This is a terrible answer. It does not explain any solution, just more links.
My issue was with the .env file. I was referring to the wrong password on the server.
0

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

Comments

0
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

Comments

0

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>

Comments

0

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 '';

Comments

0

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';

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.