Featured Article

Master MySQL with Node.js: A Complete Example Guide

Adam Jul 13, 2026

MySQL and Node.js are two powerful tools frequently used together to build robust and scalable server-side and database-driven applications. MySQL, an open-source relational database management system, ensures data reliability and integrity, while Node.js, a JavaScript runtime environment, delivers exceptional performance and real-time capabilities.

NodeJS Import CSV To Database
NodeJS Import CSV To Database

In this comprehensive guide, we will demonstrate how to connect to a MySQL database and perform various operations using Node.js. By the end of this article, you'll have a solid understanding of integrating MySQL with Node.js and an example project to boot.

Crud Operation in Nodejs and MySQL with Source Code
Crud Operation in Nodejs and MySQL with Source Code

Setting Up the Node.js and MySQL Environment

The first step in our demonstration is to install the necessary packages and set up our development environment. We'll need the 'mysql' package, which is a pure Node.js JavaScript client implementing the MySQL protocol.

Push Notifications With NodeJS
Push Notifications With NodeJS

You can install it via npm (Node Package Manager) using the following command: ```shell npm install mysql ```

Connecting to a MySQL Database

Node.js Cheat Sheet for Web Development | Backend Basics & NPM Guide | AKTU Notes
Node.js Cheat Sheet for Web Development | Backend Basics & NPM Guide | AKTU Notes

Establishing a connection to your MySQL database is the foundation of our application. Here's a simple example of connecting to a MySQL database using 'mysql' package: ```javascript const mysql = require('mysql'); const con = mysql.createConnection({ host: "localhost", user: "your_username", password: "your_password", database: "your_database" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); }); ```

Performing CRUD Operations

Once connected, we can perform the basic Create, Read, Update, and Delete (CRUD) operations. Let's start with inserting data into a table.

the cover of node j s notes for professionals, with three pages on each side
the cover of node j s notes for professionals, with three pages on each side

Here's an example of inserting data into a table named 'users': ```javascript let sql = "INSERT INTO users (name, email) VALUES ?"; let values = [ ['John Doe', 'johndoe@gmail.com'], ['Jane Doe', 'janedoe@gmail.com'] ]; con.query(sql, [values], function (err, result) { if (err) throw err; console.log("Number of records inserted: " + result.affectedRows); }); ```

You can retrieve data from a table using the 'SELECT' statement. Here's an example: ```javascript con.query("SELECT * FROM users", function (err, result) { if (err) throw err; console.log(result); }); ```

Error Handling and Promises

Simplify Input Validation with Zod in Node.js
Simplify Input Validation with Zod in Node.js

In the examples above, we've been using the traditional callback functions for handling errors. Modern JavaScript applications often use Promises for asynchronous operations.

We can connect to our MySQL database using Promises like this: ```javascript const mysql = require('mysql2/promise'); (async () => { try { const con = await mysql.createConnection({ host: "localhost", user: "your_username", password: "your_password", database: "your_database" }); console.log("Connected!"); } catch (err) { console.error(err); } })(); ```

Ways To Read Files In NodeJS
Ways To Read Files In NodeJS
Learn to use MySQL with Nodejs
Learn to use MySQL with Nodejs
an image of a computer screen with some text on it
an image of a computer screen with some text on it
Read CSV Files In NodeJS
Read CSV Files In NodeJS
Node.JS Cheatsheet
Node.JS Cheatsheet
Node.js Express Login example with MySQL database
Node.js Express Login example with MySQL database
Best Node.js Project Ideas
Best Node.js Project Ideas
12 Best Node.Js Application Examples
12 Best Node.Js Application Examples
Complete Node.js Cheatsheet for Beginners
Complete Node.js Cheatsheet for Beginners
Introduction to NodeJS
Introduction to NodeJS
How To Check NodeJS Version
How To Check NodeJS Version
Logging in Node.js the Right Way
Logging in Node.js the Right Way
Cheat Sheets
Cheat Sheets
MySQL | Tpoint Tech
MySQL | Tpoint Tech
π—‘π—Όπ—±π—²π—·π˜€ console module provides a π—–π—Όπ—»π˜€π—Όπ—Ήπ—² class
π—‘π—Όπ—±π—²π—·π˜€ console module provides a π—–π—Όπ—»π˜€π—Όπ—Ήπ—² class
Top NodeJS Framework
Top NodeJS Framework
Node JS Key Features
Node JS Key Features
a man sitting in front of a computer screen with the words node on it
a man sitting in front of a computer screen with the words node on it
a blue poster with the words crud operations and other important information in different languages
a blue poster with the words crud operations and other important information in different languages
25 SQL QUERIES EVERY DEVELOPER SHOULD KNOW
25 SQL QUERIES EVERY DEVELOPER SHOULD KNOW

Promises with Query Execution

Once connected, we can perform operations using Promises. Here's an example of inserting data and then fetching the results: ```javascript (async () => { try { const con = await mysql.createConnection({ host: "localhost", user: "your_username", password: "your_password", database: "your_database" }); const [result] = await con.execute("INSERT INTO users (name, email) VALUES ?", [["John Doe", "johndoe@gmail.com"]]); console.log("Number of records inserted: " + result.affectedRows); const [rows] = await con.execute("SELECT * FROM users"); console.log(rows); } catch (err) { console.error(err); } })(); ```

In conclusion, integrating MySQL with Node.js opens up a world of possibilities for building powerful, real-time applications. Whether you're a seasoned developer or a Node.js beginner, understanding how to connect with MySQL and perform CRUD operations using Node.js is a valuable skill to have in your toolkit. Happy coding!