gpt4 book ai didi

mysql - nodejs mysql错误: Connection lost The server closed the connection

转载 作者:IT老高 更新时间:2023-10-28 12:52:49 27 4
gpt4 key购买 nike

当我使用node 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)

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

最佳答案

尝试使用this code处理服务器断开连接:

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 - nodejs mysql错误: Connection lost The server closed the connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20210522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com