How to connect Node Sequelize to Amazon RDS MySQL with Multi-AZ probably
Asked Answered
I

3

15

I'm using an Amazon RDS hosted MySQL with Multi-AZ Support. Just could not find any information on how to connect Sequelize to Amazon RDS properly so that Sequelize is handling fail-overs etc. accordingly?

I'm just using the following config, but do not know if this is enough or recommended?

sequelizeConfig = {
  logging: false,
  pool: { maxConnections: 5, maxIdleTime: 30},
  sequelizeConfig[dialectOptions] = {
    ssl: 'Amazon RDS'
  }
}

Using Amazon RDS with Multi-AZ I consider the following is important:

  1. Try reconnecting if connection got lost, until it is available again
  2. Don't cache mysql server ip address too long (Amazon suggests less than 1 min)

Amazon Docs are not writing anything about connection handling and pooling.

Indochina answered 30/11, 2016 at 10:44 Comment(3)
Did you get Node Sequelize.js to work with Amazon RDS MySQL?Gonfalonier
Yes, but just sequelize, not sequelize-cli with migrations. Just use the regular SQL settings via environment variables plus the above settings as described in the aws docsIndochina
I see. Bummer. We started a EC2 instance and connect RDS via Security Group.Gonfalonier
H
18

Here is how i got connected with my RDS:

 var config = require(__dirname + '/../config/config.json')[env];
 // your config file will be in your directory
 var sequelize = new Sequelize(config.database, config.username, config.password, {
    host: '****.****.us-west-1.rds.amazonaws.com',
    port: 5432,
    logging: console.log,
    maxConcurrentQueries: 100,
    dialect: 'postgres',
    dialectOptions: {
        ssl:'Amazon RDS'
    },
    pool: { maxConnections: 5, maxIdleTime: 30},
    language: 'en'
})
Heliotropism answered 1/9, 2017 at 18:13 Comment(8)
can you provide an additional example for iam authentication please?Indochina
Thx for the fast reply anyways!Indochina
What do you put in the ssl? Just the string Amazon RDS??Aguste
Yes, I believe. I did it a year ago.Heliotropism
Worked for me, Except I used dialect: 'mysql' instead of postgresVermont
@Indochina You have to create an auth token which you pass as password: docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDS/Signer.htmlTrephine
I'm using sequelize 6.33.0 and node 18.17.1 and setting dialectOptions.ssl to 'Amazon RDS' results in an error. So this no longer works.Platinize
Can confirm @Mike Lane is right. It’s an error using ‘Amazon RDS’ now.Phail
K
1

The previous answer didn't work for me, after some research, this options object did:

var options = {
  host: settings.database.host,
  port: settings.database.port,
  logging: console.log,
  maxConcurrentQueries: 100,
  dialect: 'mysql',
  ssl: 'Amazon RDS',
  pool: { maxConnections: 5, maxIdleTime: 30 },
  language: 'en',
}

I'm running a RDS MySQL and a EC2 instance in the same default VPC, this options object worked when connecting a node app from that EC2 with the RDS using sequelize.

Kudu answered 15/6, 2020 at 12:58 Comment(1)
FYI maxConcurrentQueries has not been used since 2014: github.com/sequelize/sequelize/issues/2405Schmaltz
D
0

ssl: 'Amazon RDS' didn't work for me.

After some digging, this worked:

var options = {
  host: settings.database.host,
  port: settings.database.port,
  logging: console.log,
  dialect: 'mysql',
  "dialectOptions": {
        "ssl": {
            "ca": fs.readFileSync('./eu-central-1-bundle.pem')
        }
    }
}

Where eu-central-1-bundle.pem is aws CA certificate necessary to connect to the mysql rds DB.

Drava answered 5/1 at 19:45 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ancon
"Some digging" where? edit your question to include supporting details.Tailpiece

© 2022 - 2024 — McMap. All rights reserved.