参考 https://www.cnblogs.com/maomingchao/p/7447370.html ,看能有帮助吗。
或者修改代码,在连接出错时,就重新建立连接,代码参考
function handleDisconnect() {
// Recreate the connection, since
// the old one cannot be reused.
connection = mysql.createConnection(dbConfig);
connection.connect(function(err) {
// The server is either down
// or restarting
if(err) {
// 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.
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000);
}
});
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
}else{
throw err;
}
});}