前言
这是由于mysql自带的一段时间无动作会自动关闭的机制
解决
使用闭包可以完美解决问题
var mysql = require('mysql')
var sqlConfig = {
host: 'localhost',
user: 'root',
password: 'pwd',
database: 'dbbase'
}
var conn = function(){
let connection = mysql.createConnection(sqlConfig)
connection.connect()
connection.on('error',err=>{
console.log('Re-connecting lost connection: ');
connection = mysql.createConnection(sqlConfig)
})
return function(){
return connection
}
}
module.exports = conn()