错误:ER_ACCESS_DENIED_ERROR:用户'node'@'127.0.0.1的访问被拒绝

时间:2018-08-03 08:06:34

标签: node.js

我看到这个问题已经问了很多遍了,但是没有一个答案符合我的情况:

技术:具有mysql node_module的node.js

节点代码:

app.get('/test', function(req, res) {
   var mysql = require('mysql');

   var con = mysql.createConnection({
      host: "localhost",
      user: "node",
      password: "myPassword",
      database : "myDatabase"
   });
con.connect(function(err) {
  if (err) throw err;
  console.log('CONNECTED');
});

错误:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'node'@'127.0.0.1' (using password: YES)
    at Handshake.Sequence._packetToError (/var/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
    at Handshake.ErrorPacket (/var/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:124:18)
    at Protocol._parsePacket (/var/node/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (/var/node/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/var/node/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/var/node/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/var/node/node_modules/mysql/lib/Connection.js:502:10)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    --------------------
    at Protocol._enqueue (/var/node/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/var/node/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/var/node/node_modules/mysql/lib/Connection.js:118:18)
    at /var/node/index.js:24:5
    at Layer.handle [as handle_request] (/var/node/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/node/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/node/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/node/node_modules/express/lib/router/layer.js:95:5)
    at /var/node/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/var/node/node_modules/express/lib/router/index.js:335:12)
[nodemon] app crashed - waiting for file changes before starting...

mysql工作正常:

root@www:/var/node# mysql -u node -p myDatabase
Enter password:

从命令行与mysql完全没有问题;但不适用于节点。

来自package.json

"dependencies": {
    "express": "^4.16.3",
    "mysql": "^2.16.0",
    "nodemon": "^1.18.3"
  },

为什么为什么...?哈哈

1 个答案:

答案 0 :(得分:0)

解决方案是:

port: '/var/run/mysqld/mysqld.sock'