• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to secondary sidebar

CodingStatus

- Level Up Your Status with Coding Expertise!

  • Tutorials
    • React Js Tutorials
    • JavaScript Tutorials
    • PHP Tutorials
    • HTML Tutorials
    • SQL Tutorials
    • Laravel Tutorials
  • Snippets
    • React Js Code Snippets
    • Ajax Code Snippets
    • Node.js Code Snippets
    • JavaScript Code Snippets
    • jQuery Code Snippets
    • SQL Code Snippets
  • Programs
    • PHP Program
    • Python Programs
    • Java Programs
  • How To
    • React Js How To
    • Node Js How To
    • Ajax How To
    • PHP How To
    • jQuery How To
    • JavaScript How to
  • Scripts
    • PHP Script
    • Js Script
    • React Js Script
    • JQuery Script
  • Projects
    • PHP Project
  • Interview Questions
  • Guide
  • API Integration
  • Installation

Node.js MySQL Connection Step By Step

October 18, 2020 By Md Nurullah

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

Table of Contents

  • Connect MySQL Database to Node.js App
    • 1. Install MySQL Module
    • 2. Include MySQL Module
    • 3. Create a Database
    • 4. Create Database Connection
  • How to Connect Node.js to MySQL Database
    • Tutorial Summary

Connect MySQL Database to Node.js App

NPM provides a MySQL module to create a database connection. But you will have to configure the 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 the MySQL module in your Node.js app using the following line of code

var mysql= require('mysql')

If you forget to install & include the MySQL module,  then the SQL query will not work. So, you must always make sure that It must exist in your Node.js app.

3. Create a Database

To create a database, you will have to configure the following points –

  • First of all, Include the MySQL module
  • Create a connection with MySQL
  • Declare  host, user & password with the valid credentials
  • Write SQL query to create a database within the connection method

Query –

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

You have to create a database connection with the help of the following steps –

  • First, Include the MySQL Module and assign it to mysql
  • Create a connection and assign it to conn
  • Define host, user, password & database name with the valid connection details.
  • Access connect() method from the  conn
  • If the database is not connected successfully, then return an error message
  • At last export connection

Query –

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;

Now, you can use this connection query directly in your project if you want to develop your project with Node.js & MySQL. Even It can quickly execute all kinds of SQL queries.

How to Connect Node.js to MySQL Database

  • Install Express Application Structure for creating a Node.js app
  • Create a database 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.

Filed Under: Node.js Code Snippets

Hello Developer, Welcome to my Blog. I'm Md Nurullah, a software engineer with 5+ years in full-stack web development. As the founder of CodingStatus.com, I'm passionate about sharing coding knowledge and collaborating for a brighter future. Let's connect and code together!

Primary Sidebar

Secondary Sidebar

Node.js Code Snippets,
  • Upload Multiple Files Using Multer in Node.js and Express
  • Node.js Send Email – Sending mail using Nodemailer
  • Fetch data from MongoDB Using Mongoose and Node.js Express
  • Node.js Form Validation with an Example
  • Node.js MySQL CRUD Operation with Example
  • Create Registration and Login Form in Node.js & MySQL
  • Node.js MySQL Connection Step By Step
  • MongoDB CRUD Operations Using Node.js Express and Mongoose
  • Connect MongoDB with Node.js Using Mongoose
  • Express MVC Structure – Model, View and Controller
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved