nodejs mysql Error: Connection lost The server closed the connection

当我使用节点 mysql 时,在12:00到2:00之间出现一个错误,即服务器关闭了 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)

这是 解决方案。但是,在我尝试了这种方法之后,问题也出现了。现在我不知道该怎么办。有人遇到这个问题吗?

Here is the way I wrote follow the solution:

    var handleKFDisconnect = function() {
kfdb.on('error', function(err) {
if (!err.fatal) {
return;
}
if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
console.log("PROTOCOL_CONNECTION_LOST");
throw err;
}
log.error("The database is error:" + err.stack);


kfdb = mysql.createConnection(kf_config);


console.log("kfid");


console.log(kfdb);
handleKFDisconnect();
});
};
handleKFDisconnect();
163621 次浏览

Try to use 这个密码 to handle server disconnect:

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

在你的代码中,我遗漏了 connection = mysql.createConnection(db_config);之后的部分

我不记得这个机制的原始用例了,现在我想不出任何有效的用例。

您的客户端应该能够检测到连接何时丢失,并允许您重新创建连接。如果使用相同的连接执行部分程序逻辑很重要,那么就使用事务。

不要使用此方法。


一个实用的解决方案是强制 MySQL 保持连接活跃:

setInterval(function () {
db.query('SELECT 1');
}, 5000);

相对于连接池和处理断开连接,我更喜欢这种解决方案,因为它不需要以一种能够感知连接存在的方式来构造代码。每5秒进行一次查询可以确保连接保持活动状态,并且不会发生 PROTOCOL_CONNECTION_LOST

Furthermore, this method ensures that 你保持着同样的联系, as opposed to re-connecting. This is important. Consider what would happen if your script relied on LAST_INSERT_ID() and mysql connection have been reset without you being aware about it?

但是,这只能确保不会发生连接超时(wait_timeoutinteractive_timeout)。正如预期的那样,在其它所有情况下,它都将失败。因此,一定要处理其他错误。

模拟删除连接尝试

connection.destroy();

更多信息请点击: https://github.com/felixge/node-mysql/blob/master/Readme.md#terminating-connections

在每个查询中创建和破坏连接可能很复杂,当我决定安装 MariaDB 而不是 MySQL 时,我在服务器迁移方面遇到了一些麻烦。由于某种原因,etc/my.cnf 文件中的 wait _ timeout 参数的默认值为10秒(这导致无法实现持久性)。然后,溶液被设置在28800,也就是8小时。嗯,我希望能帮助别人,这个“ Güevonada”... 原谅我的糟糕的英语。

better solution is to use the pool - it will handle this for you.

const pool = mysql.createPool({
host: 'localhost',
user: '--',
database: '---',
password: '----'
});


// ... later
pool.query('select 1 + 1', (err, rows) => { /* */ });

https://github.com/sidorares/node-mysql2/issues/836