首页 > 解决方案 > 连接丢失服务器关闭连接 - 致命错误后无法排队查询

问题描述

一旦服务器关闭 TCP 连接,我就会收到错误消息。该错误看起来与此问题相同

Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/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:920:16
at process._tickCallback (node.js:415:13)

所以为了解决这个问题,我使用了这个问题中发布的答案。我做了这个

var mysql = require('mysql');
var db_config = {
    host     : 'localhost',
    user     : 'xxxx',
    password : 'xxxx',
    database : 'xxxx'
}
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 {
     console.log('error is err:', err);                                      // connnection idle timeout (the wait_timeout
     // throw err;                                  // server variable configures this)
    }
  });
}

handleDisconnect();
module.exports = connection;

直到今天,当我遇到用户没有收到通知等问题时,我的程序才崩溃。因为它与数据库有关。所以当我检查日志文件时。这是错误

sql error isError: ER_QUERY_INTERRUPTED: Query execution was interrupted
db error { Error: Connection lost: The server closed the connection.
    at Protocol.end (/var/www/html/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/var/www/html/mysite/node/node_modules/mysql/lib/Connection.js:97:28)
    at Socket.<anonymous> (/var/www/html/mysite/node/node_modules/mysql/lib/Connection.js:502:10)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
error when connecting to db: { Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)

然后它继续在我的日志文件中打印这个上面的错误,以及这个

Cannot enqueue Query after fatal error.

请帮我解决这个问题。我必须重新启动服务器才能使其正常工作

标签: mysqlnode.jssocketstcp

解决方案


推荐阅读