JavaScript, Node.js and Compose for MySQL

The leading driver for MySQL for Node.js is the node-mysql driver. We'll walk through setting up a simple Node.js/MySQL connection here. If you want to follow along, make sure that Node.js and npm are installed on your system.

After initializing a new project using npm init, install the driver package in the project’s folder:

npm install mysql --save

The driver and its dependencies are installed in the node_modules folder, and the driver will appear under dependencies in the package.json file.

You can use the following code to connect to MySQL.

const mysql = require('mysql');  
const fs = require('fs');

const connection = mysql.createConnection(  
        host: '',
        port: 15942,
        user: 'admin',
        password: 'mypass',
        ssl: {
            ca: fs.readFileSync(__dirname + '/cert.crt')

connection.query('SHOW DATABASES', (err, rows) => {  
    if (err) throw err;
    for (let i = 0, len = rows.length; i < len; i++) {


  • SHOW DATABASES is a MySQL command that lists the databases that a user has privileges for.
  • All the databases in the deployment are displayed because the script uses the admin user.
  • The results of the query are returned as an array of objects.

Still Need Help?

If this article didn't solve things, summon a human and get some help!