Thursday, December 26, 2024
Google search engine
HomeLanguagesJavascriptNode.js Connect Mysql with Node app

Node.js 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.
  • 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
  • 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 occurred 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.

NOTE-

Sometimes you may encountered error in connecting with Mysql with NodeJS  then try running this command

alter user ‘root’@’localhost’ identified with mysql_native_password by ‘your_password’;

RELATED ARTICLES

Most Popular

Recent Comments