Durvash Nimje
Durvash Nimje

Reputation: 33

Unable to connect to AWS RDS MariaDB using Sequelize in Node.js

I am working on a Node.js project that connects to a MariaDB database using Sequelize. The connection works fine in my local environment, but when I try to connect to a MariaDB database hosted on AWS RDS, it fails.

I have verified that the AWS RDS connection works using MySQL Workbench, but my Node.js project throws the following error:

Unable to connect to the database: SequelizeConnectionError: (conn=1690, no: 45012, SQLState: 08S01) Connection timeout: failed to create socket after 1001ms

I tried look like this:

const dbConfig = require('../config/dbConfig');
const { Sequelize, DataTypes } = require('sequelize');

const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
    host: dbConfig.HOST,
    port: dbConfig.PORT,
    dialect: "mariadb",
    operatorsAliases: 0,
    define: {
        freezeTableName: true  // it's default behavior is to modified table name automatic, so we restrict it with this param
    },
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
});

sequelize.authenticate()
    .then(() => {
        console.log('DB Connected...');
    })
    .catch((error) => {
        console.error(`Unable to connect to the database: ${error}`);
    })

Upvotes: 0

Views: 411

Answers (1)

josip
josip

Reputation: 308

Is your RDS instance publicly accessible?

  1. You can check that one by going to Connectivity & security and if the Publicly accessible is set to No, you cannot access your instance from public workspace (just from within the VPC). Public access

To enable public access, you can go to Modify and under Connectivity go to Additional configuration and set Public Access to "Publicly Accessible". Also, when asked, do the changes immediately and not on the next schedule - you need to wait for a minute or two until the modification is done before being able to access the instance.

  1. Check if the underlying EC2 instance is reachable - go security group of the RDS instance, under the Connectivity tab. Connectivity tab

  2. In the inbound rules of the security group, add MySQL/Aurora port and My IP as the Source. Add My IP as the Source for the EC2 security group

  3. Make sure that either you have a database created (because you don't have by default). If you haven't provide null as the dbConfig.DB value:

const dbConfig = require('./config/dbConfig');
const { Sequelize, DataTypes } = require('sequelize');

const sequelize = new Sequelize(null, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: 'mariadb',
dialectOptions: {
  // Your additional options here
},
pool: {
  max: 5,
  min: 0,
  acquire: 30000,
  idle: 10000
  }
});

sequelize.authenticate()
.then(() => {
  console.log('Connection has been established successfully.');
})
.catch(err => {
  console.error('Unable to connect to the database:', err);
});

You should be able to see the following message:

Executing (default): SELECT 1+1 AS result
Connection has been established successfully.

Upvotes: 0

Related Questions