Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to MySQL Database on Node.js

Tags:

node.js

mysql

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!");
});
like image 470
Christian Luneborg Avatar asked Nov 08 '22 17:11

Christian Luneborg


1 Answers

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

like image 188
Krishnamoorthy Avatar answered Nov 14 '22 21:11

Krishnamoorthy