Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize secure connection for azure mysql

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?

like image 488
Alvin Avatar asked Dec 05 '22 13:12

Alvin


2 Answers

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
       }
     }
 }
like image 193
oak Avatar answered Dec 15 '22 16:12

oak


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.

like image 39
Dom Avatar answered Dec 15 '22 15:12

Dom