Nodejs – Connect Mysql with Node app

In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies:

  • NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome’s V8 JavaScript engine. It can be downloaded from here
  • Mysql: An open source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). It is the most popular language for adding, accessing and managing content in a database. Here we will use the Mysql as a database for our node application. It can be downloaded from here.
    After successfully installation let’s verify them by the following commands:

  • Node -v: It will show node version in our system.


  • Mysql -v: It will show Mysql version in our system.

Till now we have installed Node and Mysql successfully to our system. Modules/packages need to be installed for connecting node application to Mysql:

Mysql: NodeJs driver for mysql
    Now let’s come to the coding part:

  • STEP-1:
    Create a separate folder for this task and come inside this folder with terminal or command prompt.
  • STEP-2:
    Now, we will generate a package.json file so that all dependencies can be listed there for future reference.
    For knowing more about package.json click here

    For generating package.json run the following command in terminal of the project folder:

    npm init -y

    Now we have our package.json in our project folder as shown in the below screenshot:

  • STEP-3
    Install Mysql module in our project with the following command:

    npm install mysql

    After successfully installing module our package.json will have structure like this :


  • STEP-4
    Create a javascript file named server.js in the root of the project folder. Code for creating connection is as given below:

Let’s understand the code flow of the server.js file in the project folder:

Line 2: By using this line of code we are importing the mysql module.

const mysql = require(‘mysql’)

Line 6-Line 12: In this section we are creating connection variable and setting all the configuration of MySQL databases like a host, port, user, password and database.
Note: Mysql database has default port 3306 in a system.

const connection = mysql.createConnection({
host: ‘localhost’, // host for connection
port: 3306, // default port for mysql is 3306
database: ‘test’, // database from which we want to connect out node application
user: ‘root’, // username of the mysql connection
password: ‘root’ // password of the mysql connection
});

Line 16-Line 23: Now in this section we are going to establishing a connection of application to the Mysql.
Here we are calling connect function on the connection variable which we have created already.

connection.connect(function (err) {
   if(err){
       console.log("error occured while connecting");
   }
   else{
       console.log("connection created with Mysql successfully");
   }
});

Run the file server.js with following command as:

node server.js

Now we will see the following output on the terminal as shown in the screenshot:

In this way, NodeJs application can be connected with the Mysql database.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.