Tomas Lukac
Tomas Lukac

Reputation: 2225

Connect to RDS MySQL programatically (NodeJs, Java) - ER_ACCESS_DENIED_ERROR: Access denied for user

I am trying to connect to my RDS (AWS MySQL database) using NodeJs mysql library.

const mysql = require('mysql');

var connection;

connection = mysql.createConnection({
      host: 'xxx.us-east-1.rds.amazonaws.com',
      port: 3306,
      user: "username",
      password: "MyPassword",
      database: "DbName",
    });

I was also trying mysql2 and sequelize. All of them got me the same result:

{ error: ER_ACCESS_DENIED_ERROR: Access denied for user 'username'@'myhost, code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlState: '28000' }

However, this remote database is set to public and I am able to connect to it with mysql command:

mysql -u username -pMyPassowrd -h xxx.us-east-1.rds.amazonaws.com

I am also able to connect to it with MySQL Workbench.

Also, the problem is not in NodeJs, because I am able to connect to my local database:

connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: 'root',
      database: 'test',
    });

In conclusion, my AWS database is publicly available and I can connect to it, just not using NodeJs for some reason.

I have not found a useful answer yet. Did anyone encounter this problem ?

UPDATE:

Trying to connect in Java with simple Connection returns the same result as well:

final Connection con = DriverManager.getConnection("jdbc:mysql:/xxx.us-east-1.rds.amazonaws.com:3306/DbName", "username", "MyPassword");

UPDATE 2:

I was missing one slash for Java code. I needed to use jdbc:mysql:// instead of jdbc:mysql:/. But NodeJs implementation is still one big mystery.

Upvotes: 0

Views: 1971

Answers (1)

Tomas Lukac
Tomas Lukac

Reputation: 2225

The answer was rather simple, just add an ssl option with true value

connection = mysql.createConnection({
      host: 'xxx.us-east-1.rds.amazonaws.com',
      port: 3306,
      user: "username",
      password: "MyPassword",
      database: "DbName",
      ssl: true //this does the trick
    });

Upvotes: 2

Related Questions