简体   繁体   English

为什么我的节点应用程序可以运行片刻然后崩溃(heroku)?

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

i have deployed a nodejs app on heroku with ClearDb mysql, the app works for a few moments then crashes.我已经使用 ClearDb mysql 在 heroku 上部署了一个 nodejs 应用程序,该应用程序运行了一会儿然后崩溃。 i checked logs and got this errors我检查了日志并得到了这个错误

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

This could be the problem可能是问题

You can use the following code to handle the 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js 应用程序在本地运行但在部署到 heroku 时崩溃 - node.js app works locally but crashes when deployed on heroku 我的 node.js 应用程序在本地工作,但在 Heroku 上不起作用,给出了“应用程序错误”。 日志尾部是这样的。 你能帮我解决这个问题吗? - My node.js App works locally but does not work on Heroku gives "application error". The log tail is like this. Can you help me fix this? 为什么我的具有节点和mysql后端的react应用程序在本地运行,但不能在Heroku上运行? - Why is my react app, which has a node and mysql backend, working locally but not on Heroku? 为什么 heroku 服务器会在几分钟后关闭? - Why the heroku server shut downs after few minutes? 为什么我的获取顺序会改变程序的工作方式? - Why does the order of my gets change how the program works? 为什么我的查询仅适用于 number<10? - Why does my query works only for number<10? 为什么 Heroku 使用 Postgresql? - Why does Heroku use Postgresql? 当我通过网络致电exec并持续片刻时,它失败了 - When I call exec over network and it last for a few moments, it fails 在Heroku上远程连接到MySQL数据库时,Rails应用程序崩溃 - Rails app crashes when connecting remotely to MySQL Database on Heroku 为什么我的多行 MySQL 查询在 node.js 中不起作用 - Why does my multiline MySQL query not work in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM