Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » MySql » How To Connect MySQL Database in Node Js App

How To Connect MySQL Database in Node Js App

How To Connect MySQL Database in Node Js App

Posted on December 11, 2019December 17, 2022 By XpertPhp

In this article, we will explain to you how to connect MySQL database in node js application. If you want to use MySQL server with your node.js application, you need to learn MySQL node.js connection.

We can easily connect MySQL database using the MySQL NPM package in node js application. MySQL is a nodejs npm package and it is written in JavaScript so that we can easily connect MySQL with node js.

Node Js Application Setup

First, we will open the command prompt and create the application in our directory. for this, you can follow the below command.

1
2
3
mkdir my_node_app
cd my_node_app
npm init

Install NPM Package

After the done setup node js application, we will install the express and MySQL npm package. so you can see the following npm command.

1
npm install express mysql --save

Create Database Connection

In this step, We will create a config.js file in the node js application. after that, we will configure the database connection.

1
2
3
4
5
6
7
8
9
10
11
12
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;
See also  How to Create Dynamic Xml Sitemap in Codeigniter

How to Connect Node.js to MySQL Database

In this step, We will create a server.js file and set up the node js application using express.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var express=require('express');
var app = express();
var dbConn = require('./config');
 
app.get('/get_data',function(req,res){
dbConn.query('SELECT * FROM students', function (err, results) {
if(err) {
throw err;
} else {
return res.send({ status: true, data: results});
}
    });
});
 
app.listen(3000,function(){
console.log("Express Started on Port 3000");
});

Run Node js Application

we will run the node js application using the below command. so you can follow the below command.

1
node server.js

Now you can run the example using the below Url in the browser.

1
http://localhost:3000/get_data

MySql, Nodejs Tags:nodejs with express, nodejs with mysql

Post navigation

Previous Post: File/Image Upload With Multer in Node js and Express
Next Post: Create Restful CRUD API with Node js MySQL and express

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com