简体   繁体   中英

Cannot Connect to AWS RDS MySQL Database Using Express JS

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.

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 root@54.12.4.9 or root@ip-52-12-4-9.amazonaws.com instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM