1

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:

  • I've entered the port:3306 with no help;
  • removed the password and left password:'' empty, but with no results
  • added GRANT privileges to the user atop off all other privileges
  • reset XAMPP after the changes

But I still get the same message:

The image with the error

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.

1 Answer 1

2

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.

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

3 Comments

Thank you for the answer. I'll try reinstalling xampp and see where that will take me, because this isn't working at all.
tried your suggestion and it doesn't work once again.
Did you try to access the mysql database as that user from the command line just to test if the mysql credentials are correct and that you are able to login? If you can't login from the command line, it won't work from nodeJS, that's fore sure

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.