簡體   English   中英

為什么我的節點應用程序可以運行片刻然后崩潰(heroku)?

[英]why does my node app works for a few moments on and crashes( heroku)?

我已經使用 ClearDb mysql 在 heroku 上部署了一個 nodejs 應用程序,該應用程序運行了一會兒然后崩潰。 我檢查了日志並得到了這個錯誤

2019-09-20T19:03:41.536452+00:00 app[web.1]: events.js:174
2019-09-20T19:03:41.536474+00:00 app[web.1]: throw er; // Unhandled 'error' event
2019-09-20T19:03:41.536476+00:00 app[web.1]: ^
2019-09-20T19:03:41.536478+00:00 app[web.1]:
2019-09-20T19:03:41.536481+00:00 app[web.1]: Error: Connection lost: The server closed the connection.
2019-09-20T19:03:41.536483+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:112:13)
2019-09-20T19:03:41.536485+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:97:28)
2019-09-20T19:03:41.536488+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:525:10)
2019-09-20T19:03:41.53649+00:00 app[web.1]: at Socket.emit (events.js:203:15)
2019-09-20T19:03:41.536492+00:00 app[web.1]: at endReadableNT (_stream_readable.js:1145:12)
2019-09-20T19:03:41.536495+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:63:19)
2019-09-20T19:03:41.536497+00:00 app[web.1]: Emitted 'error' event at:
2019-09-20T19:03:41.536503+00:00 app[web.1]: at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:426:8)
2019-09-20T19:03:41.536506+00:00 app[web.1]: at Protocol.emit (events.js:198:13)
2019-09-20T19:03:41.536509+00:00 app[web.1]: at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
2019-09-20T19:03:41.536511+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:116:8)
2019-09-20T19:03:41.536513+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:97:28)
2019-09-20T19:03:41.536515+00:00 app[web.1]: [... lines matching original stack trace ...]
2019-09-20T19:03:41.536517+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:63:19)
2019-09-20T19:03:41.599727+00:00 heroku[web.1]: Process exited with status 1
2019-09-20T19:03:41.653368+00:00 heroku[web.1]: State changed from up to crashed

可能是問題

您可以使用以下代碼來處理服務器斷開連接

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM