Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

mysql - node server in expressjs stops working after a while on its own

after running npm start, some time passes and then i get this error in the terminal. I am new to express js and node paired with mysql, so I can't really understand the errors below, hopefully that's enough context for this. The project is a simple table display of the database's table content, and it works fine, I just have to run npm start every 10-40 seconds.

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

Error: Connection lost: The server closed the connection.
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1226:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (events.js:314:20)
    at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_09_42_711Z-debug.log
thodoristhomaidis@Theos-iMac nodeapp % npm i -g mysql
+ [email protected]
added 11 packages from 15 contributors in 0.589s
thodoristhomaidis@Theos-iMac nodeapp % npm start

> [email protected] start /Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp
> node ./bin/www

Database is connected successfully !
GET /users/form 304 7.634 ms - -
GET /css/form-style.css 404 3.291 ms - 1532
events.js:291
      throw er; // Unhandled 'error' event
      ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1226:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (events.js:314:20)
    at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_11_13_709Z-debug.log
question from:https://stackoverflow.com/questions/65838914/node-server-in-expressjs-stops-working-after-a-while-on-its-own

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

can you check this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...