Node.js MySQL Connection

If you want to use the MySQL server with your node.js App then you have to learn the node.js MySQL connection. So, here I will tell you How to Connect Node.js to MySQL Database. Once you complete this tutorial, you will easily configure it in your application.

node.js mysql connection

Connect MySQL Database to Node.js App

NPM provides a MySQL module to create a database connection. But you will have to configure some following steps to use it with Node.js.

Important Notes –

If you want to create a database with the help of the MySQL command then you will have to install the MySQL package in your system.

If you are working with PHP & MySQL, you need not install MySQL. Your MySQL Module will work in Node.js by just starting  PHP Xampp/Wamp server.

1. Install MySQL Module

You have to install MySQL Module using the following command line

npm install mysql

2. Include MySQL Module

You have to also include mysql module in your Node.js app using the following line of code

var mysql= require('mysql')

3. Create a Database

You can create a database using the following script –

var mysql = require('mysql');
var conn = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});
conn.connect(function(err) {
  if (err) throw err;
  conn.query("CREATE DATABASE nodeapp", function (err, result) {
    if (err) throw err;
    console.log("Database is created successfully");
  });
});

If you are working with PHP, You need not create a MySQL database connection using the above script. You can create a MySQL database in the localhost/PHPMyAdmin and use it for the Node.js MySQL Connection.

4. Create Database Connection

At, last you have to create a database connection with the help of the following lines of code

var mysql = require('mysql'); 
var conn = mysql.createConnection({
    host: 'host_name', 
    user: 'database_username', 
    password: 'database_password',
    database: 'database_name' 
}); 
conn.connect(function(err) { 
  if (err) throw err;
  console.log('Database is connected successfully !'); 
}); 
module.exports = conn;

 

How to Connect Node.js to MySQL Database

  • Install Express Application Structure for creating a Node.js app
  • Create a databse like codingstatus
  • Install Mysql module using npm install mysql
  • Create database.js file in Node.js app
  • Write database connection code in database.js file

File Name – database.js

var mysql = require('mysql');

var conn = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'codingstatus'
});

conn.connect(function(err) {
  if (err) throw err;
  console.log('Database is connected successfully !');
});

module.exports = conn;

Now, You can include the database.js file in the other file by using require('./database.js')

Tutorial Summary

Dear developers, I hope you have understood the above script, Now you are able to stabilize the Node.js MySQL database connection. So, Continue to visit my site, I will share more tutorials related to Node.js / Express as soon as possible.

If you have any doubts or questions. You can directly ask me through the below comment box.

Leave a Comment