Node.js MySQL Connection

Easily connect your MySQL hosting deployments at ScaleGrid to the Node.js driver to optimize your MySQL management in the cloud.

How to Connect to MySQL from the Node.js Application

  1. Install the MySQL extension for Node.js:
    npm install mysql
  2. Get the following MySQL deployment information from your cluster details page:
    a. Username
    b. Password
    c. Hostname
    d. Port (default 3306)
    e. SSL certificate (if deployment has SSL)
  1. Run the following code to connect to your MySQL deployment after you enter the information from step 2.
var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'SG-SQLCluster-25-master.devservers.scalegrid.io',
  user: 'sgroot',
  password: 'password',
  database:’databasename’,
  port: 3306,  
});
connection.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
var fs = require('fs');
var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'SG-SQLCluster-25-master.devservers.scalegrid.io',
  user: 'sgroot',
  password: 'password',
  database:’databasename’,
  port: 3306,  
  ssl  : {
    ca : fs.readFileSync('<path to CA cert file>'),
    rejectUnauthorized: true
  }
});
connection.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
  1. Run the code using --tls-min-v1.1 option:
    node --tls-min-v1.1 app.js

👍

Your MySQL deployment is now connected to your Node.js Driver!

If you have any questions, please reach out to us at [email protected].