ER_ACCESS_DENIED_ERROR:对用户'root'@'localhost'的访问被拒绝(使用密码:是)

时间:2019-10-10 15:10:45

标签: mysql node.js

我已经阅读了所有类似/重复的问题,但似乎没有一个帮助。

我正在将 node mysql npm 软件包一起使用。

我可以从终端登录到mysql,没有问题...

sudo mysql -u root -p

...但无法通过节点执行。

这是错误。

if (error) throw error;
               ^

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
    at Handshake.Sequence._packetToError (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
    at Handshake.ErrorPacket (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
    at Protocol._parsePacket (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Parser.js:433:10)
    at Parser.write (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Parser.js:43:10)
    at Protocol.write (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/Connection.js:525:10)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    --------------------
    at Protocol._enqueue (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/mnt/c/ivan/lab/posto/server/node_modules/mysql/lib/Connection.js:119:18)
    at Object.<anonymous> (/mnt/c/ivan/lab/posto/server/database/createDatabase.js:11:12)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)

数据库连接。

rdbms: {
        server: 'localhost',
        user: 'root',
        password: 'root',
        port: 3306
    }

我要执行的代码。

let mysql = require('mysql');
let rdbms = require("../config").rdbms;

let connection = mysql.createConnection(rdbms);

connection.connect();

let query = `
    DROP DATABASE IF EXISTS dbname;
    CREATE DATABASE dbname;
`

connection.query(query, function (error, results, fields) {
    if (error) throw error;
    console.log("Database created!");
});

connection.end();

0 个答案:

没有答案