Naresh Naresh - 5 months ago 111
MySQL Question

Cannot Connect nodejs to mysql

I am running

Node.js 0.10.31
on windows and my
mySQL 5.5.35
on virtual-box ubuntu running on 192.168.2.150. Now to connect node js to mysql i have used following connector :

https://github.com/felixge/node-mysql

Code :

var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.2.150',
user : 'root',
password : '*****',
port : 3306
});

connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}

console.log('connected as id ' + connection.threadId);
});


connection.query('SELECT 1', function(err, rows) {
// connected! (unless `err` is set)
console.log(err, rows);
});


In my.cnf file
bind address
is 0.0.0.0. Moreover, in the user table i have given permission to all database to
root@192.168.2.150
. But, still i am not able to connect from node to mysql and it gives the following error.
I have even tried to the Node js 0.8.2 version but still it doesn't work. can anyone point out the error?

Error:

error connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'
192.168.2.99' (using password: YES)
at Handshake.Sequence._packetToError (G:\Coding_Zone\NodeJs_Work\Output\node
_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mys
ql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mys
ql\lib\protocol\Protocol.js:271:23)
at Parser.write (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\lib\pr
otocol\Parser.js:77:12)
at Protocol.write (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\lib\
protocol\Protocol.js:39:16)
at Socket.Connection.connect (G:\Coding_Zone\NodeJs_Work\Output\node_modules
\mysql\lib\Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:88:17)
at TCP.onread (net.js:397:14)
--------------------
at Protocol._enqueue (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\l
ib\protocol\Protocol.js:135:48)
at Protocol.handshake (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\
lib\protocol\Protocol.js:52:41)
at Connection.connect (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\
lib\Connection.js:109:18)
at Object.<anonymous> (G:\Coding_Zone\NodeJs_Work\Output\mySqlTest.js:9:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
{ [Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'192.168.2.99' (
using password: YES)]
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
fatal: true } undefined


Update :

I did try with other users also and granted them full permission in the database. But still wasn't able to resolve the error.
But the strange thing is that i am able to connect with the mysql Client

Answer

As @Paul stated correctly it's not the problem of NodeJS. If you look carefully You are giving permissions to user root@192.168.2.150 (which says "user root connecting from IP 192.168.2.150"), but in error message is stated:

Access denied for user 'root'@'192.168.2.99'

Try to give your root permission for IP where your NodeJS instance is running (192.168.2.99) and it should work.