2

I'm trying to connect to a database that I have up on my domain. I've created a user called public_guests which can access this database.

I'm using NodeJS to create a connection but I get the following error.

ER_ACCESS_DENIED_ERROR: Access denied for user

The error is followed by my IP address etc.

Here is my code:

mysql = require("mysql");

var connection = mysql.createConnection({
    host: "humadshah.com",
    username: "public_guests",
    password:"hello",
    databse:"my_quotes"
});

connection.connect(function(error){
    if(error){
        console.log("Couldn't connect :(    Error: " + error);
    } else {
        console.log("Connected successfully~!");
    }    
});

I've never used a database before, so I'm expecting this to be a really stupid mistake somewhere.

Human Cyborg Relations
  • 1,202
  • 5
  • 27
  • 52
  • 6
    I'll buy a beer for anyone that solves this in under an hour – Drew Sep 25 '16 at 02:47
  • 2
    Your code is irrelevant. Your password is wrong, your user is not allowed from that IP, or something else. Also, your MySQL host is open to the **entire world** to connect to. This isn't great practice. While you do need proper credentials to get in, it's still likely that a new MySQL vulnerability will be found one day. Don't open this up so broadly unless you have to. Finally, if this is your first database, might I suggest PostgreSQL? Latest version of Postgres supports document style storage as well as columnar, and has many other nice features. – Brad Sep 25 '16 at 03:03
  • 1
    @Drew Looks like they're using GoDaddy. GoDaddy sets the default password to something random, so it might take more than an hour. ;-) – Brad Sep 25 '16 at 03:10

4 Answers4

2

As I think, it's not the problem of Node.js.

it's permissions issue which user has at particular IP address, and in the error message is stated:

Access denied for user @some IP

So try to give your root permission for IP where your Node.js instance is running port xxx.xx.xx.xx.

abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • Is there a way I can give permission to all IPs? I want this database to be accessed by anyone from anywhere – Human Cyborg Relations Sep 25 '16 at 03:04
  • Please have a look here http://stackoverflow.com/questions/8348506/grant-remote-access-of-mysql-database-from-any-ip-address – abdulbarik Sep 25 '16 at 03:08
  • @HumanCyborgRelations Yes, that's possible... is that really what you want? Use `user@%`. – Brad Sep 25 '16 at 03:08
  • Thanks for the response! Yes, that works. I'm going to only allow users to select the data in the tables, and not alter them in any way. Is there a risk I should be worried about? – Human Cyborg Relations Sep 25 '16 at 03:10
  • 1
    @HumanCyborgRelations I guess it depends on what's in those tables, and how sure you really are that you can secure the MySQL server. At a minimum, it's pretty easy to tie up your server so that nobody else can access data. Locks and such are really annoying that way. – Brad Sep 25 '16 at 03:12
  • Thanks again! Apparently, cPanel doesn't give its admins the GRANT privilege, so its preventing me from giving access to all users :( – Human Cyborg Relations Sep 25 '16 at 03:26
1

Its permission issue only, grant required permission to user it will work.

Sanjeev Kumar
  • 193
  • 1
  • 7
0

In my case there were two objects of JSON. First was test and second was development but dbPassword were not same. I changed them to one unique password and then it works. That type of problem usually occurs with beginners.

naeem1098
  • 123
  • 2
  • 9
0

Just create a new user from workbench and use it for your nodejs project

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
K.tin
  • 425
  • 2
  • 7