We have the MySQL server that we installed in the previous section. Now we will need a MySQL client that we can connect to the MySQL server and perform SQL queries from our server-side program. mysql is the standard MySQL Node.js module that implements the MySQL protocol, thus we can use this module for handling MySQL connection and SQL queries, whether you are on MySQL server or MariaDB server. So, let's get it started in the following steps:
- Install the mysql module via npm:
$ npm i mysql
- Create the MySQL connection instance in a mysql.js file with your MySQL connection details in the subdirectories in the /src/ directory as follows:
// src/core/database/mysql.js
import util from 'util'
import mysql from 'mysql'
const pool = mysql.createPool({
connectionLimit: 10,
host : 'localhost',
user : '<username>',
password : '<password>',
database : '<database>'
})
pool...