0

I leave my computer on 24 hours, but I still lose connection with the MySQL server. My WiFi runs smoothly and is never disconnected because I'm still able to use the internet along with my other electronics. The error message says

The server closed the connection.

Is this inferring a problem on my computer/connection or with MySQL? Or is it my codes? Please advise.

Error messages:

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\protocol\Protocol.js:112:13)
    at Socket.<anonymous> (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:97:28)
    at Socket.<anonymous> (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:525:10)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Connection._handleProtocolError (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:426:8)
    at Protocol.emit (events.js:198:13)
    at Protocol._delegateError (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\protocol\Protocol.js:398:10)
    at Protocol.end (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\protocol\Protocol.js:116:8)
    at Socket.<anonymous> (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:97:28)
    [... lines matching original stack trace ...]
    at process._tickCallback (internal/process/next_tick.js:63:19)

My codes:

let con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "password",
  database: 'db',
  dateStrings: 'date',
});

const handleDisconnect = () => {
  con.connect(err => {
    if (err) throw err;
    console.log("Connected!");
  });

  con.on('err', err => {
    console.log('db error', err);
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
      handleDisconnect();
    } else {
      throw err;
    }
  });
}

handleDisconnect();
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
Lost
  • 43
  • 1
  • 8
  • I hope this [solution](https://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection) helps you. – CCC Oct 05 '19 at 07:17
  • This sounds like a timeout. Does your node.js-client do something regularly (e.g. does it rest during night cause there are no (or less) new messages/stock quotes/..., and the disconnect happens then?) Also, i'm not entirely sure: does your code reconnect properly and you are asking why it has to reconnect, or does your code stop working after this disconnect and you need a fix for this? – Solarflare Oct 05 '19 at 07:59
  • @Solarflare It just randomly disconnects overnight and I can reconnect it again the following morning I wake up. The computer is on overnight but it still disconnects. The codes don't seem to handle the error when it disconnects to the MySQL server. – Lost Oct 05 '19 at 08:33
  • @CCC Thank you, it seems like I'm missing a single line of code. hopefully it fixes my problem. – Lost Oct 05 '19 at 08:35

0 Answers0