mysql - 处理 Disconnect Express JS Throw Error Access Denied for user localhost

标签 mysql node.js express

你好,我正在尝试处理断开连接我的服务器 express 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'
 };

它也发生在我的服务器上,它抛出错误,例如用户 localhost 的访问被拒绝。

错误是这样显示的

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

最佳答案

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

connection = mysql.createConnection(db_config.connection);

关于mysql - 处理 Disconnect Express JS Throw Error Access Denied for user localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532456/

相关文章:

mysql - 检索具有某些条件的相同列之和的记录

mysql - 将 NULL 值插入 INT 列

javascript - 如何用log4j或winston写入大量日志?记不清

javascript - 按值删除数组 - 实时数据

node.js - EC2 托管的 Node.js 应用程序 - 无法远程连接到端口

node.js - #Handlebars 中的每个助手不会迭代带有括号的键

php - 为什么 count on multiple file input field 在表单输出 1?

javascript - 如何将变量从 Express 传递到 Jade

javascript - 带参数验证的 ExpressJS 嵌套路由

sql - 传递给 SQL 的数组可以有多大,其中 column_value IN (Array) 是多少?