最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js - server closed the connection? - Stack Overflow

programmeradmin3浏览0评论

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

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.

Any ideas?

EDIT 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');

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.

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

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.

Any ideas?

EDIT 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');

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.

Share Improve this question edited Oct 14, 2013 at 11:00 Nikolay Dyankov asked Oct 14, 2013 at 9:49 Nikolay DyankovNikolay Dyankov 7,23411 gold badges65 silver badges89 bronze badges 5
  • It looks like your application script has some errors.. Trying running node app.js instead of forever to see the error. – Sriharsha Commented Oct 14, 2013 at 10:30
  • There are no errors visible in the log, I tried running it with node-dev. This is the only error message that I see. – Nikolay Dyankov Commented Oct 14, 2013 at 10:41
  • Let us know more about your app, so we can understand what's going on. You leaving a socket open against mysql? Why? Is it a remote database? – Salvatorelab Commented Oct 14, 2013 at 10:55
  • are you not connecting to some other tcp server. its may be related to your db? – Gaurav Commented Oct 14, 2013 at 11:59
  • possible duplicate of nodejs mysql Error: Connection lost The server closed the connection – Gajus Commented Jan 29, 2015 at 13:08
Add a comment  | 

1 Answer 1

Reset to default 16

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.

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();
发布评论

评论列表(0)

  1. 暂无评论