Connecting to the MySQL database using NodeJs

Install MySQL npm package 

open the comment prompt and go to directory and run comment

C:\>npm install mysql

Then create the database connection with nodejs using below code.

//mysql.js
var mysql = require('mysql');

var connection=mysql.createConnection({
   host:'localhost',
   user:'username',
   password:'password',
   database:'dbname',
   port:3306
});

connection.connect(function(err) {
  if (err) throw err;
  console.log("Database Connected!");
});

Then run the node comment to check the result

C:\>node mysql.js

Output will be: Database Connected!

 

Thank you.

Connecting to the MySQL Database Server from Node.js
About author
Andrew
Andrew
Nodejs Developer
0 comments
Be a First Comment
Add Your Comment