• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Connect Node.js with MySQL

Written by Rahul, Updated on January 22, 2016

Node.js is an popular programming language like PHP & JAVA for web applications. Also MySQL is most popular database used for storing values. MySQL database driver for Node.js is available under NPM repository. In this tutorial we are assuming that you already have Node.js, NPM and MySQL installed on your system. We will help you to install Node.js MySQL driver module on your system and a sample program to connect to MySQL using node.js.

Connect Nodejs with MySQL

Step 1 – Install Node.js MySQL Module

MySQL driver for node.js is available under node package manager (NPM). Use the following command to install node.js MySQL driver on your system.

$ sudo npm install mysql

Step 2 – Sample Node.js Program

Below is sample node.js program which will connection node.js application with MySQL server. It will show success and error messages according to connections results and close the connection at the end of program. Create a JavaScript file app.js.

$ cd myApp
$ vim app.js

and add the following content to above file.

var mysql  = require('mysql');
var dbconn = mysql.createConnection({
  host     : 'localhost',
  user     : '<DB_USER>',
  password : '<DB_PASSWORD',
  database : '<DB_NAME>'
});

dbconn.connect(function(err){
  if(err){
    console.log('Database connection error');
  }else{
    console.log('Database connection successful');
  }
});

dbconn.end(function(err) {
  // Function to close database connection
});

Step 3 – Verify Connection

Now execute node.js script using command line and make sure Node.js is properly connecting with MySQL.

$ node app.js

Database connection successful

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

  1. Avatar HEMANTH Reply
    October 13, 2017 at 7:10 am

    ITS IS SHOWING DATABASE CONNECTION ERROR ..! WHAT TO DO NOW ??

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy