Reputation: 139
I'm trying to connect to MySQL with NodeJS. This is the code I have for connecting:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "pass"
});
con.connect(function(err) {
if (err){
console.error('error connectiong'+ err.stack);
return;
}
console.log('Connected as Id' + connection.threadId);
});
Pretty straightforward and it should work but it doesn't. I've tried the following to resolve this with no luck:
But I still get the same message:
I can't understand why I can't connect. I've checked other questions like this, tried some of the solutions and they didn't work. Any ideas? Thx.
Upvotes: 1
Views: 775
Reputation: 4539
As the error message states: access denied for user 'nebojsa82'@'localhost'
. That means that either that user has no privileges or the password for that user is not correct.
If I were you I would try this:
mysql -h localhost -u nebojsa82 -p
Then enter password and see if you can connect. I am pretty sure that you won't be able to connect and you will get same access denied error.
Be sure that the user you are using has also at least READ privileges for the desired database.
If you have mysql root access I would do this:
mysql> create database test;
mysql> create user 'test'@'localhost' identified by 'test123';
mysql> grant all on test.* to 'test'@'localhost';
mysql> flush privileges;
That should create an empty database test, a mysql user test with password test123 and the user has full privileges over the test database. Then, having these details in mind, try to connect using the nodejs code you mentioned above.
Upvotes: 2