Menu Close

Connecting to the MySQL database using NodeJs

0
(0)

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! 

View More Details about the MySQL package – https://www.npmjs.com/package/mysql

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share

Posted in NodeJs, NPM

Leave a Reply

Your email address will not be published. Required fields are marked *

You can also read...