Connect with MariaDB Connector/Node.js (Callback API)


Node.js developers can use MariaDB Connector/Node.js to establish client connections with MariaDB Enterprise.

Require Callback API

MariaDB Connector/Node.js provides two different connection implementations: one built on the Promise API and the other built on the Callback API.

To use the Callback API, use the following module:

const mariadb = require('mariadb/callback');


createConnection(options) -> Connection is the base function used to create a Connection object.

The createConnection(options) function returns a Connection object. The commonly used options in createConnection(options) are listed in table:




User name to connect with database


User password


IP address or DNS of the database server. Default is localhost.


Port # to connect database at. Default is 3306.


Database name to establish a connection to. No default is configured.


Connection timeout in milliseconds.


A boolean value to indicate whether to return result sets as array instead of the default JSON. Arrays are comparatively faster.

Code Example: Connect

The following code example connects to a server at the IP address using the database and user account created in the example setup:

const mariadb = require('mariadb/callback');

// Declare async function
function main() {
   let conn;

   try {
      conn = mariadb.createConnection({
          user: "db_user",
          host: "",
          password: "db_user_password",
          database: "test",

      // Use Connection
      // ...

   } catch (err) {
      // Manage Errors
      console.log("SQL error in establishing a connection: ", err);

   } finally {
      // Close Connection
      if (conn) conn.end(err => {if(err){
         console.log("SQL error in closing a connection: ", err);}

  • A try...catch...finally statement is used for exception handling.

  • New connections are created in auto-commit mode by default.

  • When you are done with a connection, close it to free resources. Close the connection using the connection.end([callback]) function.

  • The script calls the connection.end([callback]) function to close/end the connection in the finally block after the queries that are running have completed.

  • The end() function takes a callback function that defines one implicit argument for the Error object if thrown in closing the connection as argument. If no error is generated in closing a connection the Error object is null.