We will learn How to connect mysql database in node js with example
Install Nodejs
sudo apt-get update
sudo apt-get install nodejs
Install mysql package using npm command
sudo apt-get install npm
install MySQL node.js driver
sudo npm install mysql
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'test'
});
connection.connect();
connection.query('SELECT * FROM users', function(err, result, fields)
{
if (err) throw err;
console.log(result);
});
connection.end();
Run this file by following command
nodejs server