mariadb npm code example

Example 1: install mariadb

sudo apt update
sudo apt-get -y install mariadb-server
sudo systemctl start mariadb
sudo systemctl enable mariadb

Example 2: install mariadb-server rpm

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

Example 3: connect nodejs to mariadb

const mariadb = require('mariadb');
const pool = mariadb.createPool({
     host: 'mydb.com', 
     user:'myUser', 
     password: 'myPassword',
     connectionLimit: 5
});
async function asyncFunction() {
  let conn;
  try {
	conn = await pool.getConnection();
	const rows = await conn.query("SELECT 1 as val");
	console.log(rows); //[ {val: 1}, meta: ... ]
	const res = await conn.query("INSERT INTO myTable value (?, ?)", [1, "mariadb"]);
	console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }

  } catch (err) {
	throw err;
  } finally {
	if (conn) return conn.end();
  }
}

Tags:

Php Example