Connecting to MariaDB databases using JavaScript/Node.js

You can connect to MariaDB using the MariaDB Connector/Node.js.

Install MariaDB Connector/Node.js

Install MariaDB Connector/Node.js using npm:

npm install mariadb

Connecting and querying

Use the Promise API to connect to a local MariaDB database runing on your machine and execute a SQL query:

const mariadb = require('mariadb');

async function main() {
    let connection;
    try {
        connection = await mariadb.createConnection({
            host: 'localhost',
            port: '3306',
            database: 'database_name',
            user: 'user',
            password: 'password'
        });

        let rows = await connection.query('SELECT name, email FROM contact');
        rows.forEach(row => {
            console.log(`${row.name} ${row.email}`);
        });

    } catch (err) {
        console.log(err);
    } finally {
        if (connection) await connection.close();
    }
}

main();

Check the documentation and the Developer Code Central on GitHub for more information and examples.

Need more help?

If you have questions, comments, or are looking for more information on MariaDB, please check out:

You can also reach out to us via: