Naresh Naresh - 1 year ago 274
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 Now to connect node js to mysql i have used following connector :

Code :

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

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

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 Moreover, in the user table i have given permission to all database to
. 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 connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'' (using password: YES)
at Handshake.Sequence._packetToError (G:\Coding_Zone\NodeJs_Work\Output\node
at Handshake.ErrorPacket (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mys
at Protocol._parsePacket (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mys
at Parser.write (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\lib\pr
at Protocol.write (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\lib\
at Socket.Connection.connect (G:\Coding_Zone\NodeJs_Work\Output\node_modules
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
at Protocol.handshake (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\
at Connection.connect (G:\Coding_Zone\NodeJs_Work\Output\node_modules\mysql\
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'@'' (
using password: YES)]
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 Source

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

Access denied for user 'root'@''

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download