Alvin
Alvin

Reputation: 8499

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?

Upvotes: 6

Views: 8924

Answers (3)

oak
oak

Reputation: 3036

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
       }
     }
 }

Upvotes: 17

Dom
Dom

Reputation: 53

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.

Upvotes: 6

EMX
EMX

Reputation: 6211

var conn = mysql.createConnection({
       host: 'myserver4demo.mysql.database.azure.com',
       user: 'myadmin@myserver4demo',
       password: 'your_password',
       database: 'quickstartdb',
       port: 3306,
       ssl: {
         key: fs.readFileSync('./certs/client-key.pem'),
         cert: fs.readFileSync('./certs/client-cert.pem')
       }
    });

Configure SSL connectivity in your application to securely connect to Azure Database for MySQL

Upvotes: 0

Related Questions