Node.js-服务器关闭了连接? [英] Node.js - server closed the connection?

查看:218
本文介绍了Node.js-服务器关闭了连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Node.js服务器上运行一个Web应用程序,并且我希望它一直在线,所以我一直在使用.但是一段时间后,我会得到以下结果:

I'm running a web app on a Node.js server and I need it to be online all the time, so I'm using forever. But here is what I'm getting after a period of time:

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查询,但似乎没有任何区别.

I have two more servers that have been running for about 10 days straight. I have a "keepalive" loop on all servers, making a "select 1" mysql query every 5 minutes or so, but it seems like it doesn't make any difference.

有什么想法吗?

编辑1

我的其他服务器也出现了类似的错误,我认为这是连接超时",因此我放置了以下功能:

My other servers were giving a similar error, I think it was "connection timeout", so I put this function:

function keepalive() {
    db.query('select 1', [], function(err, result) {
        if(err) return console.log(err);    
        console.log('Successful keepalive.');
    });
}

它修复了我的另外两台服务器.但是在我的主服务器上,我仍然遇到上面的错误.

And it fixed my other two servers. But on my main server I'm still getting the error above.

这是我启动主服务器的方式:

Here is how I'm starting my main server:

var https = require('https');
https.createServer(options, onRequest).listen(8000, 'mydomain.com');

我不确定您想看什么代码.基本上,服务器是REST API,它需要一直保持运行状态.每分钟大约2-5,可能是10个请求.

I'm not sure what code are you interested in seeing. Basically the server is a REST API and it needs to stay up all the time. It's getting about 2-5, maybe 10 requests per minute.

推荐答案

该错误与您的HTTPS实例无关,而与您的MySQL连接有关.

The error isn't related to your HTTPS instance, it's related to your MySQL connection.

与数据库的连接意外结束,未得到处理.要解决此问题,可以使用手动重新连接解决方​​案,也可以使用自动处理重新连接的连接池.

The connection to the database is ending unexpectedly and isn't being handled. To fix this, you can use a manual reconnect solution, or you can use connection pooling which automatically handles reconnects.

这是从 node-mysql 的文档中摘录的手动重新连接示例.

Here is the manual reconnect example taken from node-mysql's documentation.

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

这篇关于Node.js-服务器关闭了连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆