MariaDB Connector/Node.js

Overview

Node.js developers can connect to MariaDB SkySQL from their application using MariaDB Connector/Node.js.

MariaDB Connector/Node.js can be used with:

  • MariaDB Platform for Transactions

  • MariaDB Platform for Analytics

  • MariaDB Platform for Smart Transactions (HTAP)

  • MariaDB Platform for Distributed SQL

Connector

MariaDB Connector/Node.js

Supported Versions

2.3

Programming Language

JavaScript

Programming Language Version

Node.js 6 or later

API

Promise API
Callback API

Supports TLS

Yes

Supports Connection Pools

Yes

License

GNU Lesser General Public License v2.1

MariaDB Connector/Node.js installation and connection instructions follow. Additional information on MariaDB Connector/Node.js can be found in the Connector/Node.js reference.

Installing MariaDB Connector/Node.js

MariaDB Connector/Node.js is typically installed either from the Node.js repository or manually from the source code package.

Installing MariaDB Connector/Node.js via Repository

  1. Install MariaDB Connector/Node.js from the Node.js repository by executing the following:

    $ npm install mariadb
    

Installing MariaDB Connector/Node.js via Source Code

  1. Go to the MariaDB Connectors download page:

  2. In the "Product" drop-down, select "Node.js connector".

  3. In the "Version" drop-down, select the version you want to use.

  4. Click on the "Download" button to download the source code package.

  5. When the source code package finishes downloading, install it by executing:

    $ npm install mariadb-connector-nodejs-2.3.1.tar.gz
    

Connecting with MariaDB Connector/Node.js

  1. Determine the connection information for your MariaDB SkySQL database service:

    What

    Where to find it

    createPool()
    host option

    Fully Qualified Domain Name in the Service Details view

    createPool()
    user option

    Default username in the Service Credentials view, or the username you created

    createPool()
    password option

    Default password in the Service Credentials view, the password you set on the default user, or the password for the user you created

    createPool()
    port option

    Read-Write Port or Read-Only Port in the Service Details view

    createPool()
    ssl.ca option

    Contents of the skysql_chain.pem file containing the "Certificate Authority Chain" (Download)

  2. Create a file named .env to store your SkySQL credentials:

    SKYSQL_HOST = example.skysql.net
    SKYSQL_USER = db_user
    SKYSQL_PORT = 5001
    SKYSQL_CA_PEM = /path/to/skysql_chain.pem
    
  3. Import MariaDB Connector/Node.js into your application by calling the require() function.

  4. Load the environmental variables from the file named .env by:

    • Importing the dotenv module by calling the require() function;

    • And then calling the module's config() method.

  5. Read the skysql_chain.pem file into a variable.

    • The fs.readFileSync() function can be used for this, as shown in the example.

  6. Create a connection pool for the database service by calling the createPool() function.

  7. Get a connection from the connection pool by calling the pool.getConnection() function.

    // Module Imports
    const fs = require("fs");
    var mariadb = require('mariadb');
    require('dotenv').config()
    
    // Certificate Authority (CA)
    var serverCert = [fs.readFileSync(process.env.SKYSQL_CA_PEM, "utf8")];
    
    // Create Connection Pool
    var pool = mariadb.createPool({
       host: process.env.SKYSQL_HOST,
       user: process.env.SKYSQL_USER,
       password: "db_user_passwd",
       port: process.env.SKYSQL_PORT,
       ssl: {
         ca: serverCert
       }
    });
    
    // Retrieve Connection from Connection Pool
    var conn = pool.getConnection();
    
  8. Execute your script:

    $ node mariadb_connection_test.js