加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

javascript – Node.js – 服务器关闭连接?

发布时间:2021-03-07 13:01:40 所属栏目:编程 来源:网络整理
导读:我在Node.js处事器上运行一个Web应用措施,我必要它一向在线,以是我一向在行使.但这是我颠末一段时刻后获得的: Error: Connection lost: The server closed the connection. at Protocol.end (/home/me/private/app/node_modules/mysql/lib/protocol/Protoco

我在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查询,但好像没有任何区别.

有任何设法吗?

编辑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-5个,大概10个哀求. 最佳谜底 该错误与您的HTTPS实例无关,它与您的MySQL毗连有关.

与数据库的毗连不测竣事,未举办处理赏罚.要办理此题目,您可以行使手动从头毗连办理方??案,也可以行使自动处理赏罚从头毗连的毗连池.

以下是从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();

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读