【问题标题】:Node.js MySQL Connection IssueNode.js MySQL 连接问题
【发布时间】:2014-06-13 12:56:52
【问题描述】:

有什么问题?

  • 我正在使用 node-mysql 连接到 mysql。
  • 我很难处理server disconnects / wait_timeouts 如此处所述:https://github.com/felixge/node-mysql#server-disconnects
  • 每次在处理PROTOCOL_CONNECTION_LOST 错误时尝试重新创建连接后,我都会收到错误消息This socket has been ended by the other party

我想要做什么

// Connect Function
db.connect = function(callback){

    // Destory the Connection if there is already one
    if(db.connection) {
        console.log('[mysql]','connection destroy');
        db.connection.destroy();
        db.connection = null;

    }

    // Create the Connection
    db.connection = MySQL.createConnection(options);

    // Connect using the Connection
    db.connection.connect(function(error) {
        if(error){ 
            console.log('[mysql]', 'connect failed', error);
        } else {
            console.log('[mysql]', 'connection success');
            db.connection.database = options.database;
            callback(db.connection, error);
        }
    });

    // Listen on Connection Errors
    db.connection.on('error', function(error) {
        // Connection to the MySQL server is usually
        // lost due to either server restart, or a
        // connnection idle timeout (the wait_timeout server variable configures this)
        if(error.code === 'PROTOCOL_CONNECTION_LOST') { 
            console.log('[mysql]', 'PROTOCOL_CONNECTION_LOST')
            db.connect(callback);
        } else {                                    
            console.log('[mysql] Connection Error: ', error);               
        }
    });

    // Return Connection Instance
    return db.connection;
}

其他详情

wait_timeoutinteractive_timeout 必须在 my.cnf 中设置大约 10 秒才能测试问题。

[mysqld]
wait_timeout = 10
interactive_timeout = 10

【问题讨论】:

    标签: javascript mysql node.js error-handling connection


    【解决方案1】:

    在收到 FIN 数据包后,库意外尝试覆盖 TCP 套接字,这意味着 TCP 连接被另一端半关闭。我会解决这个问题,给你一个更好的错误信息,但它仍然是一个错误;您的网络正在杀死您的 MySQL 连接或其他什么。

    你在使用连接池吗?根据堆栈跟踪,您似乎只是在超时后对连接进行查询。可能是您保持非活动连接的时间过长,而网络或 MySQL 服务器上的某些东西正在终止连接。

    【讨论】:

      猜你喜欢
      • 2019-04-21
      • 1970-01-01
      • 2011-09-21
      • 2010-09-23
      • 2011-02-16
      • 1970-01-01
      • 2021-05-07
      相关资源
      最近更新 更多