1
votes

i am new to nodejs ,while creating a connection to the database i got this errors

C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\Parser.js:80 throw err; // Rethrow non-MySQL errors ^

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'Root'@'localhost' (using password: YES) at Handshake.Sequence._packetToError (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14) at Handshake.ErrorPacket (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18) at Protocol._parsePacket (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\Protocol.js:279:23) at Parser.write (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\Parser.js:76:12) at Protocol.write (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\Protocol.js:39:16) at Socket. (C:\Users\devashis khandelwal\node_modules\mysql\lib\Connection.js:103:28) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at Socket.Readable.push (_stream_readable.js:134:10) -------------------- at Protocol._enqueue (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\Protocol.js:145:48) at Protocol.handshake (C:\Users\devashis khandelwal\node_modules\mysql\lib\protocol\Protocol.js:52:23) at Connection.connect (C:\Users\devashis khandelwal\node_modules\mysql\lib\Connection.js:130:18) at Object. (C:\Users\devashis khandelwal\node files\demo_db_connection.js:9:5) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at 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

3 Answers

8
votes

You are connecting with user Root, I believe it is root. Your error is clearly a credentials error.

2
votes

Error: ER_ACCESS_DENIED_ERROR: it's occurred because of permission.

As far as I understand your above code you need to change Root to 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!");
});
0
votes

Had a similar issue where I was able to login using the CLI with same creds, but didn't work with mysql2 in nodejs (mysql-server 8.0). Solved it by using % in place of localhost:

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

instead of

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