What's the best Node.js mysql module for connecting to mysql over ssl?
Asked Answered
P

4

6

I've built my app so far using node-mysql. I just set up a securely accessible mysql database using AWS RDS service and have tested an SSL connection successfully with the mysql command line client. I have Amazon's public key .pem file. node-mysql doesn't seem to have an api for the SSL connect option.

The only one I've found so far is Node-mysql-libmysqlclient but I got errors trying to connect with it, something about a bindings dependency being null so I'm not sure if I should trust it. Suggestions of which module/api to use? Alternatively, if you know what I need to modify in node-mysql I'd be willing to tinker a bit but it looks like it's using lower level Sockets to connect to MySQL so I'm not sure how hard it would be to get the SSL part added on.

Popelka answered 8/10, 2012 at 22:12 Comment(1)
FYI to connect with the command line mysql client all that's needed is, "mysql -h host -u username -ssl_ca=mysql-ssl-ca-cert.pem -p" where mysql-ssl-ca-cert.pem is a public key file provided by Amazon. I didn't need to generate any other keys or certificates.Popelka
F
-4

Use Sequelize! Hands down best ORM around for MariaDB/MySQL. https://github.com/sequelize/sequelize

npm install sequelize
--- then ---
npm install mariadb
--- or ---
npm install mysql
Firedrake answered 8/4, 2018 at 8:51 Comment(2)
why all the downvotes without any comment as to why?Raw
its an irrelevant answer.Mechanical
F
11
var mysql  = require('mysql');
var fs     = require('fs');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db',
  ssl      : {
        ca   : fs.readFileSync('./ssl/server-ca.pem'), // should be enough for AWS
        key  : fs.readFileSync('./ssl/client-key.pem'), // required for google mysql cloud db
        cert : fs.readFileSync('./ssl/client-cert.pem'), // required for google mysql cloud db
  }
});

that should do the trick.

Fructuous answered 6/8, 2016 at 21:18 Comment(0)
A
1

The node-mysql module supports ssl connections as showed here: https://github.com/felixge/node-mysql#ssl-options.

Anarthrous answered 28/10, 2014 at 9:41 Comment(0)
P
0

If you are using node-mysql make sure to manually include the CA:

From the Documentation, Under "SSL Options":

The ssl option in the connection options takes a string or an object. When given a string, it uses one of the predefined SSL profiles included.

If you are using the string option to specify the Certificate Authority (CA), for example, "Amazon RDS" it will resolve to the, now outdated CA, so the connector will throw a, "HANDSHAKE_SSL_ERROR".

For AWS connections, explicitly specify the path to the .pem file containing your CA certificate:

const fs = require('fs');

const connection = mysql.createConnection({
    // ...
    ssl: {
        ca: fs.readFileSync(__dirname + './ssl/us-west-1-bundle.pem')
    }
});
Prosthesis answered 27/8, 2024 at 20:14 Comment(0)
F
-4

Use Sequelize! Hands down best ORM around for MariaDB/MySQL. https://github.com/sequelize/sequelize

npm install sequelize
--- then ---
npm install mariadb
--- or ---
npm install mysql
Firedrake answered 8/4, 2018 at 8:51 Comment(2)
why all the downvotes without any comment as to why?Raw
its an irrelevant answer.Mechanical

© 2022 - 2025 — McMap. All rights reserved.