2

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 [email protected]. 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

1
  • Did you resolve this? Please select an answer if so. Commented Nov 22, 2015 at 9:56

7 Answers 7

8

As @Paul stated correctly it's not the problem of NodeJS. If you look carefully You are giving permissions to user [email protected] (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.

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

2 Comments

Hey, I am using goDaddy MySQL database and I have given permission to my IP address, in fact I have put a % wildcard to give permission to any IP that accesses the database but it still keeps showing me the same error @Naresh mentioned above. Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user-portal'@'1.186.37.68' (using password: YES)
@codeinprogress looks like your credentials are not correct (assuming the settings and IP address is correct)
2

I had the same problem and I tried the other solutions listed above.

Following the recommendation of jackleekr at https://github.com/mysqljs/mysql/issues/250#issuecomment-7153315 I was able to connect, using a connection string:

var conn = mysql.createConnection('mysql://root:*******@localhost:3306/company?debug=true');

Comments

1

Have you tried connecting using the same information in the MySQL client?

Are you sure the root user isn't disabled (as it often is by default)?

In short, this doesn't look like a Node problem to me, but an issue getting the right credentials and configuration.

2 Comments

I did try with other users also and granted them full permission in the database. But still wasn't able to resolve the error. Moreover, i was able to connect with the mysql Client.
Are you getting anything useful in the MySQL logs? It looks like you're on a Windows box, it's possible that the access denied error is masking a block by your Firewall on port 3306; you can determine that by looking at your mysql logs to see if the logon attempt was made at all from its perspective. Finally, your MySQL settings themselves (not the user, the settings) might only allow localhost access, in which case the host value 192.168.2.150 won't work even if that's the IP of the localhost.
1

Adding my inputs as it might help others. I faced the same issue and below steps resolved it.

  1. Open WorkBench
  2. Open the connection to your database belongs.
  3. Left hand side, under management, click Users and Privilages.
  4. Click Add Account button, just below User Accounts list.
  5. Enter Login Name, 'host matching' to localhost or your host name, enter password.
  6. Goto Schema Privileges tab, click Add Entry Button and click Ok. Optionally you can specify the single database the user can access.
  7. Select the desired rights for the user, like 'Select', 'Insert', etc
  8. Click Apply and use the new created user to connect from Nodejs.

Comments

0

The command prompt is running with limited privilegies. Uncheck the respective box in the Command Prompt shortcut and it may work properly. I had this issue yesterday.

Comments

0

Try by changing port number to 3307

 var mysql = require('mysql');

 var connection = mysql.createConnection({

   host : '192.168.2.150',
   user : 'root',
   password : '*****',
   port : 3307

 });

connection.connect( function(error) {
  if(error) throw error;
  console.log("Connected to database");
});

It worked for me

Comments

0

I too faced the same situation and the other answers didn't help me.The problem is the number of connections to the database - by default it is zero. You should change it. Steps:

  1. Open WorkBench
  2. Open the connection to your database belongs.
  3. Left hand side, under management, click Users and Privilages.
  4. Go to Account Limits.
  5. Change Concurrent connections to your feasibility.

enter image description here

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.