Node JS - Node js Create Connection with MySQL - Node - Node JS tutorial - webnode
- We can use Node.js in database applications. Here we use MySQL as a database with Node.js.
Install MySQL on your computer
- Once the MySQL is installed and running, you can access it by using Node.js.
Install MySQL Driver
- You have to install MySQL driver to access a MySQL database with Node.js.
- Download MySQl module from npm.
- To download and install the "mysql" module, open the Command Terminal and execute the following: npm install mysql

Learn Node js - node js Tutorial - npm install mysql - node - Node js Examples
Create Connection
- Create a folder named "DBexample". In that folder create a js file named "connection.js" having the following code:
connection.js
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "wikitechy",
password: "wikitechy12345"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});

Learn Node js - node js Tutorial - node-js mysql create connection - node - Node js Examples
- Now open the command terminal and use the following command: Node connection.js
Output:

Learn Node js - node js Tutorial - nodejs node connection - node - Node js Examples
- Now Node.js is connected with MySQL.