Node.js MySQL连接问题

Node.js MySQL Connection Issue

本文关键字:问题 连接 MySQL js Node      更新时间:2023-09-26

有什么问题

  • 我使用节点mysql连接到mysql
  • 我很难处理这里提到的server disconnects/wait_timeouts:https://github.com/felixge/node-mysql#server-断开
  • 每次在处理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;
}

其他详细信息

CCD_ 5和CCD_ 6必须在CCD_。

[mysqld]
wait_timeout = 10
interactive_timeout = 10

在接收到FIN数据包后,库意外地试图重写TCP套接字,这意味着TCP连接被另一侧关闭了一半。我会解决这个问题,给你一个更好的错误消息,但它仍然是一个错误;要么你的网络正在破坏你的MySQL连接,要么什么的。

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