用户'root'@'外部ip'拒绝访问(使用密码:YES)

时间:2018-06-06 16:59:14

标签: mysql node.js google-cloud-platform

我正在开发一个谷歌云项目,当我运行节点 index.js /尝试访问 mysql 时出现此错误远程数据库。

这是完整的错误消息:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'external_ip' (using password: YES)
    at Handshake.Sequence._packetToError (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Handshake.ErrorPacket (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/sequences/Handshake.js:130:18)
    at Protocol._parsePacket (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/it21695/nodeproject/node_modules/mysql/lib/Connection.js:103:28)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:277:12)
    at readableAddChunk (_stream_readable.js:262:11)
    at Socket.Readable.push (_stream_readable.js:217:10)
    --------------------
    at Protocol._enqueue (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at Connection.connect (/home/it21695/nodeproject/node_modules/mysql/lib/Connection.js:130:18)
    at Object.<anonymous> (/home/it21695/nodeproject/index.js:10:12)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)

这是我的 index.js 代码(测试数据库和书籍表都存在):

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'external_ip',
  user     : 'root',
  password : 'password',
  database : 'test',
});

connection.connect();

connection.query('SELECT * from books', function (error, results, fields) {
        if (error) throw error;
        console.log(results);
});

connection.end();

我已将 mysql端口(3306)添加到防火墙例外,并且我已授予 root 用户的权限。我将 mysql node.js 外部ips转为静态。我使用谷歌云分配的密码。

+------------------+----------------+------------+
| user             | host           | grant_priv |
+------------------+----------------+------------+
| root             | %              | Y          |
| root             | external_ip    | Y          |
| mysql.infoschema | localhost      | N          |
| mysql.session    | localhost      | N          |
| mysql.sys        | localhost      | N          |
| root             | localhost      | Y          |
| stats            | localhost      | N          |
+------------------+----------------+------------+
  

mysql v8.0.11
node.js v10.1.0
npm v5.6.0

2 个答案:

答案 0 :(得分:0)

在检查应用程序之前,请确保检查数据库主机服务器上的用户凭据和访问权限。

答案 1 :(得分:0)

此问题与不是nodejs的权限有关。

首先,我建议您安装MySQL Workbech并使用该工具执行测试。

此外,您需要对MySQL数据库进行一些配置以允许这些远程root连接,请看一下这篇文章:

How to allow remote connection to mysql