DEV Community

Mariam Adeyemi
Mariam Adeyemi

Posted on

Connect Your Node.js Project to MySQL Database Locally

There is more than one way to connect MySQL database to node.js and in this tutorial, we are going use one of the easy ways to do it. With the use of XAMPP and mysql2 package, our connection will be set up in no time. No worries if you don't know these two, we are going to do this step by step.

Install Xampp

Xampp is a control panel or cross-platform server that enables developers to create and test their programs locally. Xampp is going to provide the GUI(Graphical User Interface) of our database. It will allow us to create databases and the data we need for our project.

Visit the apachefriends website to download the suitable xampp for your operating system. Follow all the necessary procedures and install it. After the successful installation, open the program:

Xampp control panel

Start the first two modules on the panel(Apache and MySQL).

Xampp control panel with apache and mysql started

This will start the SQL database locally which you can access on a localhost server. Open your browser and go to localhost/phpmyadmin/.

phpmyadmin tab

To create a new database, click on the new button at the side panel.

create database

Give your database a new name and click create. It should show up in the side panel after it has been successfully created. You can access it and go on to create your tables manually or import a SQL file with your data.

Set Up Connection

For this, we will make use of the mysql2 package. Install mysql2 to your project with npm install mysql2. You should read the mysql2 documentation to learn more about how to work with the package.

In your model folder, create a connection.js file and copy the code to establish a connection to your database:

const mysql = require('mysql2');
const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: '', //your password. You could leave it like this, for now
    database: 'database_name'
})

connection.connect((err)=>console.log(err || 'Database Connected'))

module.exports = connection.promise()
Enter fullscreen mode Exit fullscreen mode

You can go on to import the connection function(please be aware that it is asynchronous) anywhere in your project you want to query your database.

And that's it, our node.js project is connected to MySQL database in few steps.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

MongoDB Atlas lets you build and run modern apps in 125+ regions across AWS, Azure, and Google Cloud. Multi-cloud clusters distribute data seamlessly and auto-failover between providers for high availability and flexibility. Start free!

Learn More

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay