处理用户localhost的Disle Disconnect Express JS抛出错误访问被拒绝

时间:2017-08-06 13:23:13

标签: mysql node.js express

你好我试图处理断开我的服务器快递js,我使用这个句柄断开代码,并且它在localhost中抛出了拒绝用户的错误访问。

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'
 };

并且它也发生在我的服务器上,这会导致用户localhost的访问被拒绝等错误。

错误显示如下

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

1 个答案:

答案 0 :(得分:1)

错误消息告诉空用户(at)不允许localhost。 这导致我进入db_config: 将db_config替换为代码行中的db_config.connection

connection = mysql.createConnection(db_config.connection);