PROTOCOL_CONNECTION_LOST

637 views
Skip to first unread message

Corster 8

unread,
Jan 28, 2022, 11:28:27 AM1/28/22
to node-mysql
ive been using the package just fine for months, but the entire time ive used it, ive hit an issue. every few hours, about half a day, node-mysql crashes my code. while i am ok with restarting it every once in a while, i would really like to learn how to do this the proper way, to prevent this. the error is:
"
events.js:352
      throw er; // Unhandled 'error' event
      ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (C:\Users\corster\Desktop\project\node_modules\mysql\lib\protocol\Protocol.js:112:13)
    at Socket.<anonymous> (C:\Users\corster\Desktop\project\node_modules\mysql\lib\Connection.js:94:28)
    at Socket.<anonymous> (C:\Users\corster\Desktop\project\node_modules\mysql\lib\Connection.js:526:10)
    at Socket.emit (events.js:387:35)
    at endReadableNT (internal/streams/readable.js:1317:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (C:\Users\corster\Desktop\project\node_modules\mysql\lib\Connection.js:423:8)
    at Protocol.emit (events.js:375:28)
    at Protocol._delegateError (C:\Users\corster\Desktop\project\node_modules\mysql\lib\protocol\Protocol.js:398:10)
    at Protocol.end (C:\Users\corster\Desktop\project\node_modules\mysql\lib\protocol\Protocol.js:116:8)
    at Socket.<anonymous> (C:\Users\corster\Desktop\project\node_modules\mysql\lib\Connection.js:94:28)
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}
"

Corster 8

unread,
Jan 28, 2022, 11:29:40 AM1/28/22
to node-mysql
ive made sure to use connection.end(); at the end of all my code, but it still does this

Consusalud Del Caribe

unread,
May 9, 2023, 4:31:46 PM5/9/23
to node-mysql
Tengo el mismo error

MANAS SANTRA

unread,
May 10, 2023, 12:09:49 AM5/10/23
to node-...@googlegroups.com
Try once and see what's the actual issue :

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();

Thanks & Regards,

MANAS SANTRA
Mobile : 8436489050
Web Portfolio : manassantra.online


--
You received this message because you are subscribed to the Google Groups "node-mysql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/node-mysql/31f0d5b2-f3fc-4e8b-a833-dd23d058dda6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages