jac*_*Lin 76 mysql dbconnection node.js
当我使用节点mysql时,在服务器关闭TCP连接的12:00到2:00之间会出现错误.这是完整的信息:
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)
Run Code Online (Sandbox Code Playgroud)
有解决方案.但是,在我尝试这种方式后,问题也出现了.现在我不知道该怎么做.有人遇到过这个问题吗?
以下是我按照解决方案编写的方式:
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();
Run Code Online (Sandbox Code Playgroud)
Clo*_*ble 139
尝试使用此代码来处理服务器断开连接:
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();
Run Code Online (Sandbox Code Playgroud)
在你的代码中,我错过了之后的部分 connection = mysql.createConnection(db_config);
Gaj*_*jus 39
我不记得这个机制的原始用例.如今,我想不出任何有效的用例.
您的客户端应该能够检测到连接丢失的时间并允许您重新创建连接.如果使用相同的连接执行部分程序逻辑很重要,那么使用事务.
TL;博士; 不要使用这种方法.
一个实用的解决方案是强制MySQL保持连接活着:
setInterval(function () {
db.query('SELECT 1');
}, 5000);
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种连接池和处理断开连接的解决方案,因为它不需要以了解连接存在的方式构建代码.每5秒进行一次查询可确保连接保持活动状态并且PROTOCOL_CONNECTION_LOST不会发生.
此外,此方法可确保您保持相同的连接,而不是重新连接.这个很重要.考虑如果您的脚本依赖LAST_INSERT_ID()并且在没有您意识到的情况下重置了mysql连接会发生什么?
但是,这只能确保不会发生连接超时(wait_timeout和interactive_timeout).正如所料,它将在所有其他情况下失败.因此,请务必处理其他错误.
小智 11
更好的解决方案是使用池 - 它会为您处理。
const pool = mysql.createPool({
host: 'localhost',
user: '--',
database: '---',
password: '----'
});
// ... later
pool.query('select 1 + 1', (err, rows) => { /* */ });Run Code Online (Sandbox Code Playgroud)
https://github.com/sidorares/node-mysql2/issues/836
小智 9
要模拟断开的连接,请尝试
connection.destroy();
Run Code Online (Sandbox Code Playgroud)
更多信息在这里:https : //github.com/felixge/node-mysql/blob/master/Readme.md#termination-connections
| 归档时间: |
|
| 查看次数: |
92546 次 |
| 最近记录: |