Search code examples
mysqlnode.jsamazon-ec2amazon-rds

Issues connecting my Node.js backend to my AWS RDS MySQL database


how are you?

I'm having issues connecting my Node.js backend to my AWS RDS MySQL database. I migrated from Digital Ocean to AWS, which was working perfectly within Digital Ocean. However, after migrating to AWS and executing npm run start locally, I receive the following error:

getaddrinfo ENOTFOUND databaseX.xxxxxxxxxxx.eu-west-3.rds.amazonaws.com parent: Error: getaddrinfo ENOTFOUND databaseX.xxxxxxxxxxx.eu-west-3.rds.amazonaws.com at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26) { errno: -3007, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'databaseX.xxxxxxxxxxx.eu-west-3.rds.amazonaws.com', fatal: true },

Additionally, within the EC2 instance or using Insomnia locally, I can execute the npm run start command or even connect to the database perfectly. But locally, I can't connect using the same command:

mysql -h databaseX.xxxxxxxxxxx.eu-west-3.rds.amazonaws.com -P 3306 -u admin -p

I've been trying to figure this out for a while, I've opened all connections in the security group but still can't connect in any way. I will share my security groups and my code for connecting to the database.

this is how I am connecting to the databse using node.js

import { Sequelize } from 'sequelize';
import fs from 'fs';
import dotenv from 'dotenv';

dotenv.config();

const sequelize = new Sequelize(process.env.DB_NAME!, process.env.DB_USER!, process.env.DB_PASSWORD!, {
    host: process.env.DB_HOST!,
    port: Number(process.env.DB_PORT),
    dialect: 'mysql',
    dialectOptions: {
        ssl: {
            require: true,
            rejectUnauthorized: true,
            ca: fs.readFileSync('./certificates/ca-certificate-aws.pem').toString(),
        },
    },
});

export default sequelize;


DB_NAME=databaseX
DB_USER=admin
DB_PASSWORD=XXXXXXXXXXXXXXXX
DB_HOST=databaseX.xxxxxxxxxxx.eu-west-3.rds.amazonaws.com
DB_PORT=3306

this is how I configure the security groups, do I need to open more gates?

enter image description here


Solution

  • Is public access enabled in the RDS instance? If not, you can't directly connect to the instance from the local machine. You can connect to it via port forwarding to RDS