How can I connect to Azure MySQL using sequelize?
From Azure nodejs example:
const mysql = require('mysql2');
var config =
{
    host: 'myserver4demo.mysql.database.azure.com',
    user: 'myadmin@myserver4demo',
    password: 'your_password',
    database: 'quickstartdb',
    port: 3306,
    ssl: true
};
what is the configuration for sequelize:
I tried using ssl but not successful:
ssl: true,
I got this error:
Unable to connect to database: SequelizeConnectionError: SSL connection is required. Please specify SSL options and retry.
I got this work:
dialectOptions: {
      encrypt: true,
      ssl : {
        rejectUnauthorized: false
      }
    },
but where to find the cert?
For us, the solution was to provider dialectOptions i.e 
{
    "host":<hostname>,
    "port":<port>,         
    "user":<username>,
    "password":<password>,
    "port": 3306,
    "dialect": "mysql",
    "ssl": true,
    "dialectOptions": {
       "ssl": {
          "require": true
       }
     }
 }
                        The Sequelize documentation is lacking on the SSL front, and the accepted answer is not for Sequelize like the comments state.
I struggled to know how to add SSL options too, but finely found an issue on Github containing a snippet.
const config = {
   username: process.env["DB_USER"],
   password: process.env["DB_PASS"],
   host: process.env["DB_HOST"],
   dialect: "mysql",
   database: dbs[process.env["USE_DB"]],
   pool: {
       max: 5,
       idle: 30000,
       acquire: 60000
   },
   dialectOptions: {
       ssl: {
           ca: fs.readFileSync(__dirname + '/ssl/BaltimoreCyberTrustRoot.crt.pem')
       }
   }
}
The Github issue that helped me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With