Skip to main content

Front-End Development

How to Connect Node JS with MySQL Database

He Puts The Pro In Programmer

Node.js is a popular open-source JavaScript runtime environment used for building web applications. On the other hand, MySQL is a widely used relational database management system. In this blog, I will guide you through a step-by-step process on how to connect Node.js with a MySQL database and workbench.

Step 1: Install Required Software

Before we begin, we need to ensure that we have the following required software installed on our computer:

  1. Node.js
  2. MySQL Server and
  3. MySQL Workbench.

If you haven’t installed them already, you can download and install them from their respective official websites.

Step 2: Create a MySQL Connections and Database on MySQL Workbench

Open MySQL Workbench and create a new connection to your MySQL server. You can do this by clicking the “+” icon located in the “MySQL Connections” section on the workbench home screen; you will be prompted to enter the connection details. Once you’ve entered all the details, click on the “Test Connection” button to test your connection. If your connection is successful, you will see a success message then.

Once you have connected to your MySQL new connection, you can create a new database by clicking on the “Create a new schema in the connected server” button. Give your database a name and click “Apply.”

Step 3: Create a New Node.js Project

To create a new Node.js project, we need to open a terminal or command prompt window and navigate to the directory where we want to create our project. Once we are in the desired directory, we can run the following command to create a new Node.js project:

npm init

This command will initialize a new Node.js project and create a package.json file in the current directory. We can accept the default settings or provide our values for the prompts.

Step 4: Install the MySQL Package for Node.js

To connect Node.js with a MySQL database, you need to install the MySQL package in your Node.js project. Type the command given below in a command prompt or terminal:

npm install mysql2

This command will install the MySQL package and its dependencies in our Node.js project.

Step 5: Write Node.js Code to Connect to MySQL Database

To set up a connection with your MySQL database, create a new JavaScript file and enter the following code:

const mysql = require('mysql2');

// create a new MySQL connection
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'pass@123',
  database: 'database_name'
});
// connect to the MySQL database
connection.connect((error) => {
  if (error) {
    console.error('Error connecting to MySQL database:', error);
  } else {
    console.log('Connected to MySQL database!');
  }
});

// close the MySQL connection
connection.end();

In this code, we first import the mysql2 package that we installed earlier. Then we create a new connection with your MySQL database by using the createConnection method. Replace the host, user, password, and database fields with your MySQL database details. Then we use the connect() method to connect to the MySQL database. We log a message to the console if the connection is successful. Finally, we use the end() method to close the MySQL connection.

Step 6: Run Node.js Code and Test the Connection

To run the Node.js code and test the connection to the MySQL database, we need to open a terminal or command prompt window and navigate to the project directory where our JavaScript file is located. Once we are in the correct directory, we can run the following command to execute our JavaScript file:

node filename.js

Here, “filename.js” refers to the name of the JavaScript file you created for example:

Picture3

Conclusion

In this article, we have provided a step-by-step guide on how to connect Node.js with MySQL database and workbench. We first created a new Node.js project using the npm init command, installed the required packages using npm install, and created a new MySQL database using MySQL Workbench. We then wrote the Node.js code to connect to the MySQL database, and finally, we ran the code and tested the connection. With this guide, you should now be able to successfully connect Node.js with MySQL database and workbench for your web applications.

Thoughts on “How to Connect Node JS with MySQL Database”

  1. Mohd wahid Ansari

    Thankyou for summarising whole process into easily understandable steps.whole process is well sufficed and minimized to best points.

  2. Sadaf Aafreen Ansari

    I really appreciate your tremendous work for making this blog. All the points covered by you is clear cut and definite.

  3. Fantastic tutorial! Your explanations are easy to understand and the examples make it all clear. Looking forward to more from you. Keep it up! ๐Ÿ˜ƒ๐Ÿ‘

  4. Appreciating your insightful explanation thanking you for sharing this knowledge with us. It is helpful and you made it clear.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sufiyan Akbani

Sufiyan Akbani is an Associate Technical Consultant at Perficient, with a keen interest in UI development. With over 2 years of experience in this field, Sufiyan is passionate about exploring the latest trends and emerging technologies in UI and front-end development. He finds joy in watching fiction movies and immersing himself in nature by hiking in hill stations during his free time.

More from this Author

Follow Us