gpt4 book ai didi

mysql - 为什么我的 Node 应用程序运行一段时间后崩溃(heroku)?

转载 作者:行者123 更新时间:2023-11-29 15:37:57 26 4
gpt4 key购买 nike

我已经在heroku上使用ClearDb mysql部署了一个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

最佳答案

This可能是问题所在

您可以使用以下代码来处理服务器断开连接

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

关于mysql - 为什么我的 Node 应用程序运行一段时间后崩溃(heroku)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com