Javascript Node.js-服务器是否关闭了连接?

Javascript Node.js-服务器是否关闭了连接?,javascript,mysql,node.js,rest,Javascript,Mysql,Node.js,Rest,我在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)

我在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分钟左右进行一次“select1”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');

我不确定你对什么代码感兴趣。基本上,服务器是一个RESTAPI,它需要一直保持运行。它大约每分钟收到2-5个请求,可能是10个请求。

该错误与您的HTTPS实例无关,它与您的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();

您的应用程序脚本似乎有一些错误。。尝试运行
node app.js
而不是一直运行以查看错误。日志中没有可见错误,我尝试使用node-dev运行它。这是我看到的唯一错误消息。请让我们了解有关您的应用程序的更多信息,以便我们了解发生了什么。你让mysql的套接字打开了吗?为什么?它是远程数据库吗?您没有连接到其他tcp服务器吗。这可能与您的db有关?可能是重复的,很抱歉回复太晚。我刚刚用你的答案替换了我的代码,我正在启动服务器。我们明天就会知道它是否管用了:)干杯,管用吗?还有,@hexa氰化物,你有没有想过为什么需要这个?为什么MySql连接偶尔会丢失?我第一次遇到这个错误。经过数月的正常运行后,MySQL连接突然不可用。事实证明,我安装了一个名为“无人值守升级”的包,它安排自动应用MySQL安全更新,这导致MySQL服务器重新启动。