MySQL连接丢失错误Node.js

3

我正在使用以下代码将我的节点连接到mysql,用于我在项目中使用的所有rest api; 我将其作为通用的数据库连接文件放置在所有查询请求中。

var mysql = require('mysql');
var db_connect = (function () {
    function db_connect() {
        mysqlConnConfig = {
            host: "localhost",
            user: "username",
            password: "password",
            database: "db_name"
        };
    }
    db_connect.prototype.unitOfWork = function (sql) {
    mysqlConn = mysql.createConnection(mysqlConnConfig);
        try {
            sql(mysqlConn);
        } catch (ex) {

            console.error(ex);
        } finally {
            mysqlConn.end();
        }
    };
    return db_connect;
})();
exports.db_connect = db_connect;

上述代码运行良好,我将在所有的rest api中使用以下 'sql' 执行我的查询。

var query1 = "SELECT * FROM table1";
 sql.query(query1,function(error,response){
if(error){
    console.log(error);
}
else{
    console.log(response);
    }
 })

一切进行得很顺利,但问题在于我在运行我的永久模块8-12个小时后出现了 SQL 协议连接错误。

forever start app.js

我正在使用上述的永久模块开始我的项目。 8-12小时后,我得到以下错误,所有的REST API都无法工作或崩溃了。

"stack": ["Error: Connection lost: The server closed the connection.", "    at Protocol.end (/path/to/my/file/node_modules/mysql/lib/protocol/Protocol.js:109:13)", "    at Socket.<anonymous> (/path/to/my/file/node_modules/mysql/lib/Connection.js:102:28)", "    at emitNone (events.js:72:20)", "    at Socket.emit (events.js:166:7)", "    at endReadableNT (_stream_readable.js:913:12)", "    at nextTickCallbackWith2Args (node.js:442:9)", "    at process._tickDomainCallback (node.js:397:17)"],
"level": "error",
"message": "uncaughtException: Connection lost: The server closed the connection.",
"timestamp": "2017-09-13T21:22:25.271Z"

我在研究中找到了一种解决方案,用于处理断开连接的问题,如下所示。 但是,我正在努力使用我的代码配置以下SQL连接。

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

有人能帮我修改这段代码吗?

这段代码之前已经给出了。
2个回答

1
SHOW SESSION VARIABLES LIKE '%wait_timeout';
SHOW GLOBAL  VARIABLES LIKE '%wait_timeout';

其中一个设置为28800(8小时)。 增加它。
或者...捕获错误并重新连接。
或者...检查您的框架如何处理“连接池”。
但是...请注意,网络故障可能会发生。 因此,仅增加超时时间无法处理这些故障。
或者...不要保持连接太长时间。 它没有表现良好。 它可能会导致超过max_connections。
(抱歉,我不了解您的应用程序,无法更具体地指出要追求哪个方向。)
max_connections
wait_timeout和max_connections以笨拙的方式一起使用。 如果超时时间“太高”,连接数可能会继续增加,从而威胁到“连接过多”错误。 在典型设计中,最好降低超时时间,以防止客户端浪费时间太长地挂起连接。
如果您的情况是这样的:“希望连接固定数量的客户端并保持永久连接”,那么应该增加超时时间,但不要增加max_connections(至少不要超过固定的客���端数量)。
然而,如果网络出现问题,连接可能会中断。因此,您仍然可能会遇到“连接丢失”的情况。(不过,如果所有内容都在同一台机器上,这种情况相当不太可能发生。)

增加了关于最大连接数的讨论。 - Rick James
现在我已经将超时时间增加到1年,并且每5秒查询一次我的数据库,这个问题没有再次出现了?这是一个好的做法吗? - Jagadeesh
@Jagadeesh "将我的超时时间增加到1年",我认为这不是一个好主意,MySQL会保持太多的“Sleep”连接,如果您的代码没有在某个地方关闭连接,MySQL可能会达到max_connections;“每5秒查询一次我的数据库”是一个好主意,它可以作为心跳,因此MySQL不会关闭“Sleep”连接;连接问题的根本原因大多是网络问题或超时问题,在我看来。 - Wang Wen'an
1
@Jagadeesh 我认为“每5秒查询一次我的数据库”就足够了,它可以作为心跳,并且MySQL不会关闭连接,只需将wait_timeout保留为默认值即可。 - Wang Wen'an
或者,如果我每天使用cron重启我的项目一次,这会对我有帮助吗? - Jagadeesh
显示剩余9条评论

0

我通过使用连接池解决了这个问题。可以尝试这种方式 https://www.npmjs.com/package/mysql

var mysql = require('mysql');
var pool  = mysql.createPool(...);
 
pool.getConnection(function(err, connection) {
  // Use the connection 
  connection.query('SELECT something FROM sometable', function (error, results, fields) {
    // And done with the connection. 
    connection.release();
 
    // Handle error after the release. 
    if (error) throw error;
 
    // Don't use the connection here, it has been returned to the pool. 
  });
});


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接