本文是由Script House收集并编辑的,主要介绍它. Script House的编辑认为这非常好. 现在,我将与您分享并提供参考. 让我们来看看脚本库的编辑器
我在Node.js服务器上运行一个Web应用程序,并且我希望它一直,所以我一直在使用它. 但这是我一段时间后得到的:
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/me/private/app/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
error: Forever detected script exited with code: 8
error: Forever restarting script for 3 time
我仍然有两台服务器连续运行约10天. 我在所有服务器上都有一个“ keepalive”循环,每隔5分钟左右执行一次“ select 1” mysql查询,但这似乎没有任何区别.

p>
有什么想法吗?
编辑1
我的其他服务器也出现了类似的错误,我认为这是“连接超时”,因此我放置了以下功能:

function keepalive() {
db.query('select 1',[],function(err,result) {
if(err) return console.log(err);
console.log('Successful keepalive.');
});
}
它修复了我的另外两台服务器. 但是在我的主服务器上,我仍然收到上面的错误.
这是我启动主服务器的方式:
var https = require('https');
https.createServer(options,onRequest).listen(8000,'mydomain.com');

我不确定您想查看什么代码. 基本上,服务器是REST API,并且需要一直保持高效. 每分钟大约需要2-5node 关闭服务器,可能需要10个请求.
最佳答案
该错误与您的HTTPS实例无关,与您的MySQL连接有关.
与的连接意外终止node 关闭服务器,并且未处理. 要解决此问题,可以使用手动重新连接解决方案,也可以使用自动处理重新连接的连接池.

以下是从node-mysql文档获得的手动重新连接示例.
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();
以上是脚本公司为您收集的所有内容. 希望本文能帮助您解决遇到的程序开发问题.
如果您认为Script House网站的内容还不错,欢迎向程序员朋友推荐Script House网站.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-262011-1.html
妓者就是为了点击率和稿费