How to connect MySQL database using node js?

If you don’t install MySQL then you can install MySQL by npm install mysql.

Example

Create Connection

You can write connection code in a single file and call in all pages where you need to interact with database.

var mysql = require(‘mysql’);

var con = mysql.createConnection({

host: “localhost”,

user: “your database username”,

password: “database password”

});

con.connect(function(err) {

if (err) throw err;

console.log(“Database Connected!”);

});