Christian Luneborg
Christian Luneborg

Reputation: 511

Cannot connect to MySQL Database on Node.js

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!");
});

Upvotes: 6

Views: 9729

Answers (2)

Sarthak Kavidayal
Sarthak Kavidayal

Reputation: 11

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.

Upvotes: 0

Krishnamoorthy
Krishnamoorthy

Reputation: 1326

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

Upvotes: 2

Related Questions