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:

Node.js
MySQL Server and
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:

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.