nodejs mysql Error Connection lost The server closed the connection

0 votes

When I use node mysql, an error appears between 12:00 and 2:00, stating that the server has terminated the TCP connection. The entire message is as follows:

Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)

That is the answer. However, when I use this method, the problem reappears. I'm not sure what to do now. Is anyone else having this issue?

Following is how I wrote the solution:

var handleKFDisconnect = function() {
    kfdb.on('error', function(err) {
        if (!err.fatal) {
            return;
        }
        if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
            console.log("PROTOCOL_CONNECTION_LOST");
            throw err;
        }
        log.error("The database is error:" + err.stack);

        kfdb = mysql.createConnection(kf_config);

        console.log("kfid");

        console.log(kfdb);
        handleKFDisconnect();
    });
   };
   handleKFDisconnect();

Jun 17, 2022 in Node-js by Vaani
• 7,070 points
11,333 views

1 answer to this question.

0 votes

Try to use this code to handle server disconnect:

var db_config = {
  host: 'localhost',
    user: 'root',
    password: '',
    database: 'example'
};

var connection;

function handleDisconnect() {
  connection = mysql.createConnection(db_config); // Recreate the connection, since
                                                  // the old one cannot be reused.

  connection.connect(function(err) {              // The server is either down
    if(err) {                                     // or restarting (takes a while sometimes).
      console.log('error when connecting to db:', err);
      setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
    }                                     // 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.
  connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
      handleDisconnect();                         // lost due to either server restart, or a
    } else {                                      // connnection idle timeout (the wait_timeout
      throw err;                                  // server variable configures this)
    }
  });
}

handleDisconnect();

In your code i am missing the parts after connection = mysql.createConnection(db_config);

To know more about Node JS, It's recommended to join Node JS Course today.

answered Jun 17, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,880 points
9,026 views
0 votes
1 answer

Error: listen EADDRINUSE while using nodejs?

Hello @kartik, EADDRINUSE means that the port number which listen() tries ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
6,509 views
0 votes
1 answer

Error:npm WARN unmet dependency in nodejs

Hii @kartik, Following are the possible solution : Manually ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,880 points
6,246 views
0 votes
1 answer

Error:Nodejs cannot find installed module on Windows

Hello @kartik, Add an environment variable called NODE_PATH and set ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,880 points
4,850 views
0 votes
0 answers

Knex NodeJS and inserting into the database

Here is my first attempt at setting ...READ MORE

Jun 27, 2022 in Node-js by Vaani
• 7,070 points
466 views
0 votes
0 answers

Remove double quotes from a Table Name using SEQUELIZE Nodejs

used Nodejs' SEQUELIZE to construct an Account ...READ MORE

Jun 27, 2022 in Node-js by Vaani
• 7,070 points
1,522 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,900 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,844 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,584 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP