处理断开连接Express JS抛出错误访问拒绝用户本地主机

你好我正试图处理断开连接我的服务器表示js,我使用句柄断开连接的代码,它会抛出错误访问被拒绝在本地用户。

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

和我的连接主机

  var mysql = require('mysql'); var db_config = module.exports = { 'connection': { 'host': 'localhost', 'user': 'root', 'password': '' }, 'database': 'database' }; 

它发生在我的服务器也抛出错误,如访问拒绝用户本地主机。

错误就是这样显示的

 Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''@'localhost' to database 'databases' 

错误消息告诉空用户(at)localhost是不允许的。 这导致我到db_config:在代码行中用db_config.connectionreplacedb_config

 connection = mysql.createConnection(db_config.connection);