Create mysql connection with nodejs

Posted by

The tutorial provides the node.js integration with MySQL Database. The node.js requires the ‘mysql’ module to be installed using the below npm command

Installing mysql module using node.js npm

npm install mysql

node.js mysql installation using npm

Installing MySQL

The MySQL can be downloaded as the GPL License version from the below given site https://www.mysql.com/downloads/

Node.js program for creating MySQL connection

As we have mySQL installed on the computer and have the required ‘mysql’ module installed in the node server, let’s see the simple node.js program to connect the mySQL Database Server

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: ""  
});  
con.connect(function(err) {  
  if (err) throw err;  
  console.log("MySQL Database Server is connected successfully!");  
});  
C:\Users\user\Desktop>node nodejs-mysql-db.js
MySQL Database Server is connected successfully!