6

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?

2
  • "but not successful" is not a valid problem description. mention what fails, which error you get, etc Commented Aug 21, 2017 at 8:33
  • Unable to connect to database: SequelizeConnectionError: SSL connection is required. Please specify SSL options and retry. Commented Aug 21, 2017 at 8:41

3 Answers 3

17

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
       }
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I needed 3 days .... to realize that "ssl:true" does not work, but "ssl:{require:true}" does. I am so annoyed
6

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.

Comments

0
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

1 Comment

While this answer is one way to connect to a MySQL DB via SSL with node.js it doesn't apply Sequelize like the question asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.