js mariadb on connect code example
Example 1: 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);
const res = await conn.query("INSERT INTO myTable value (?, ?)", [1, "mariadb"]);
console.log(res);
} catch (err) {
throw err;
} finally {
if (conn) return conn.end();
}
}
Example 2: connect mariadb debian
mysql -h host_name -u username -p password database_name