1

I got an error once that the TCP connection is shut down by the server. The error looks the same as this question has

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)

So to solve this problem I used the answer posted in this question. I did this

var mysql = require('mysql');
var db_config = {
    host     : 'localhost',
    user     : 'xxxx',
    password : 'xxxx',
    database : 'xxxx'
}
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 {
     console.log('error is err:', err);                                      // connnection idle timeout (the wait_timeout
     // throw err;                                  // server variable configures this)
    }
  });
}

handleDisconnect();
module.exports = connection;

My program never crashes until today when I face some issue like user was not getting the notifications. since it has to do with the database. So when I checked the log files. this was the error

sql error isError: ER_QUERY_INTERRUPTED: Query execution was interrupted
db error { Error: Connection lost: The server closed the connection.
    at Protocol.end (/var/www/html/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/var/www/html/mysite/node/node_modules/mysql/lib/Connection.js:97:28)
    at Socket.<anonymous> (/var/www/html/mysite/node/node_modules/mysql/lib/Connection.js:502:10)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
error when connecting to db: { Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)

and then it keeps printing this above error in my log files and along with this as well

Cannot enqueue Query after fatal error.

Please help me to solve this problem. I have to do a restart the server to make it work

user1hjgjhgjhggjhg
  • 1,237
  • 4
  • 15
  • 34

0 Answers0