与MySQL的节点服务器连接丢失

时间:2018-07-03 11:03:58

标签: mysql node.js npm amazon-rds

在使用池连接时,

AWS RDS 的连接经常丢失,而不会发出任何错误事件,例如PROTOCOL_CONNECTION_LOST等。当API服务器收到请求时,mysql会返回一个空数组作为结果,而不是错误。

我可以看到RDS仪表板中的连接已断开,但是未发出错误事件。 我已经尝试了像更改mysql_connections_options之类的connectTimeoutwait_timeout等解决方案,此外,我还尝试了以较短的定期间隔ping mysql服务器(这将返回正确的结果),但仍然是连接丢失并且查询返回空数组作为结果,而没有任何错误。

这是我的连接代码:

connection = mysql.createPool(mysqlConnectionOptions);
function pingSqlServerForPersistentConnection() {
    console.log("PINGING : ");
    connection.getConnection(function (err) {
        if (err) {
            console.log('MYSQL ERROR CONNECTION : ' + err);

            pingSqlServerForPersistentConnection();
        }
        else {
            if (config.get("APP_NAME") == "prod") {
                setInterval(selectStar, 150000);
            }
            console.log('Connection established');
        }
    });
    connection.on('error', function (err) {
        console.log("MYSQL DISCONNECTED : ", JSON.stringify(err));
        pingSqlServerForPersistentConnection();
        if (err.code === 'PROTOCOL_CONNECTION_LOST') {
            console.log("MYSQL : \n PROTOCOL_CONNECTION_LOST");
            pingSqlServerForPersistentConnection();
        }
        if (err.code === 'ETIMEDOUT') {
            console.log("MYSQL : ETIMEDOUT");
            pingSqlServerForPersistentConnection();
        }
    });
}

0 个答案:

没有答案