0

I am trying to connect to my Amazon RDS cloud database using Express framework. My server.js connection looks as follows:

const express = require('express');
var bodyParser = require('body-parser')
const mysql = require('mysql');
const cors = require('cors');
const port = process.env.PORT || 3300;
const connection = mysql.createConnection({
host     : 'endpoint-of-my-rds-database.amazonaws.com',
user     : 'user',       
password : 'password',           
database : 'database', 
port: 3300,                
socketPath: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'
});

const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

connection.connect(function(err) {
    if(err) throw err;

    console.log("connected to database");
})

After running node server.js I get the following standard error:

Access denied for user 'root'@'localhost' (using password: YES)

I am able to connect to my cloud database with the same credentials using MySQL Workbench. Also, I can connect to my local MySQL database through Express by changing credentials to those of my local MySQL database.

Researching online, it appears that this error is due to invalid credential, but, as I've mentioned, I can connect to my RDS database through Workbench with no issues. It is only Express that seems to give me trouble. Will be happy to provide additional info. Any help will be appreciated. Thank you in advance.

2
  • So are you not using default port for mysql? 3306, but instead using 3300? Commented May 15, 2020 at 0:04
  • I am running my local MySQL server on port 3306, so if I change 3300 to 3306 I get a "listen EADDRINUSE :::3306" error since 3306 is where I'm running MySQL. If I run server.js without starting my local MySQL server I get an error: Error: connect ENOENT /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock It seems that it attempts to connect to RDS database only if I run local MySQL server. Or I am completely misunderstanding it. Commented May 15, 2020 at 0:20

1 Answer 1

2

You'll want to remove that socketPath. Per the docs on socketPath:

The path to a unix domain socket to connect to. When used host and port are ignored.

You're overriding your other connection details and trying to connect to whatever database is listening on that socket.

The clue is the error message: 'root'@'localhost'. That host is where your connection originates from. In this case localhost indicates that you're connecting to a database on the same machine that your code is running on, so you're not getting to your RDS instance.

If you were failing to authenticate against an RDS instance, I'd expect to see something like [email protected] or [email protected] instead.

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

1 Comment

Oh, god, I am so dumb. Thank you so much, Sir, this surely did the trick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.