6

I still cannot figure out why Im still getting this error message when trying to connect to the MYSQL Server on Node.js -

ERROR -

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
at Handshake.Sequence._packetToError (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18)
at Protocol._parsePacket (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Users\Admin\Desktop\Test Files\Hello World!\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)

JS -

var mysql = require('mysql');
var con = mysql.createConnection({
    host: 'localhost', 
    user: 'root', 
    password: '*****', 
    database: 'mydb'
    });

con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
});
16
  • 1
    are you using XAMPP server? Commented Aug 29, 2017 at 20:13
  • 3
    Access denied usually means you used the wrong password. Commented Aug 29, 2017 at 20:24
  • 1
    Try this Commented Aug 29, 2017 at 20:29
  • 1
    @ChristianLuneborg try with mentioning DB name , for eg. database: 'test' and restart XAMPP or execute flush host; on mysql Commented Aug 29, 2017 at 20:31
  • 1
    @ChristianLuneborg you just updated code in question but you missed , after password. Is this typo in here or missed in code :) ? Commented Aug 29, 2017 at 20:42

2 Answers 2

2

There is no prolem in your node.js

node.js require only mysql installation

-> go to installation of nodejs in the command prompt and enter

 npm install mysql 

in Eclipse: 1) install the nodejs plugin 2) open package.json in the nodejs project and enter

       "author": "Krish",
       "dependencies": {
                "mysql": "2.x.x"
      },

3) right click on the project run -> node install

it will install the mysql and you are ready to connect the Database

ER_ACCESS_DENIED_ERROR

1) go to mysql command prompt

   mysql> create user 'root'@localhost IDENTIFIED BY 'password';

2) give permission

   mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT 
        OPTION;

or

   mysql>SHOW GRANTS FOR 'root'@'localhost';

now you can run you JS . it will be conneced

Sign up to request clarification or add additional context in comments.

Comments

0

If you hosting SQL in xampp then the root user in localhost does not have any password, so remove the password.

It worked for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.