无法使用nodejs连接到mysql

时间:2017-09-26 17:14:22

标签: javascript mysql node.js cmd server

我是nodejs的新手,在创建与数据库的连接时我遇到了这个错误

  

C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ Parser.js:80           扔错了; //重新抛出非MySQL错误           ^

     

错误:ER_ACCESS_DENIED_ERROR:拒绝用户访问   'root'@'localhost'(使用密码:YES)       at Handshake.Sequence._packetToError(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:52:14)       在Handshake.ErrorPacket(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ sequences \ Handshake.js:103:18)       在Protocol._parsePacket(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ Protocol.js:279:23)       在Parser.write(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ Parser.js:76:12)       在Protocol.write(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ Protocol.js:39:16)       在Socket。 (C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ Connection.js:103:28)       在emitOne(events.js:96:13)       在Socket.emit(events.js:188:7)       在readableAddChunk(_stream_readable.js:176:18)       在Socket.Readable.push(_stream_readable.js:134:10)       --------------------       在Protocol._enqueue(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ Protocol.js:145:48)       在Protocol.handshake(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ protocol \ Protocol.js:52:23)       在Connection.connect(C:\ Users \ devashis khandelwal \ node_modules \ mysql \ lib \ Connection.js:130:18)       在对象。 (C:\ Users \ devashis khandelwal \ node files \ demo_db_connection.js:9:5)       在Module._compile(module.js:570:32)       在Object.Module._extensions..js(module.js:579:10)       在Module.load(module.js:487:32)       在tryModuleLoad(module.js:446:12)       在Function.Module._load(module.js:438:3)       在Module.runMain(module.js:604:10)

var mysql = require('mysql');

var con = mysql.createConnection({
   host     : 'localhost',
  user     : 'Root',
  password : 'my_pass',
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

3 个答案:

答案 0 :(得分:7)

您正在与用户Root建立联系,我相信它是root。您的错误显然是凭据错误。

答案 1 :(得分:2)

错误:ER_ACCESS_DENIED_ERROR:由于许可而发生了。

据我所知,您需要将Root更改为root

var mysql = require('mysql');

var con = mysql.createConnection({
   host     : 'localhost',
  user     : 'root',
  password : 'my_pass',
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

答案 2 :(得分:0)

遇到了类似的问题,我可以使用具有相同凭据的 CLI 登录,但无法使用 nodejs (mysql2) 中的 mysql-server 8.0。使用 % 代替 localhost 解决了这个问题:

CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON db.* TO 'user'@'%';

代替

CREATE USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON db.* TO 'user'@'localhost';
相关问题