19

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?

random_user_name
  • 25,694
  • 7
  • 76
  • 115
Krister Johansson
  • 691
  • 2
  • 10
  • 34

8 Answers8

12

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.

Sawal Maskey
  • 628
  • 2
  • 7
  • 18
  • this is the correct answer.. many thanks, I had the same issue and fixed it by following this article – Jethik Oct 02 '18 at 11:49
  • Releasing connection every now and then doesn't sound like performance-wise solution. – jayarjo Nov 27 '18 at 16:28
  • 1
    @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. – JohnRC Sep 02 '20 at 11:44
2

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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
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

Community
  • 1
  • 1
Scott
  • 772
  • 1
  • 11
  • 20
0

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

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>

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
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

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 '';
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
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';
idchi
  • 761
  • 1
  • 5
  • 15