Featured Article

MySQL Node Example: A Step-by-Step Guide for Beginners

Adam Jul 13, 2026

MySQL is one of the most popular open-source database management systems, widely used for its reliability, performance, and rich feature set. When it comes to deploying MySQL, you have several options, with one increasingly popular approach being to use a MySQL Node.js example. Node.js is a JavaScript runtime that executes JavaScript code on the server, making it an excellent choice for building scalable and efficient applications. Today, we're going to explore how to set up and connect to a MySQL server using Node.js.

Node.js Express Login example with MySQL database
Node.js Express Login example with MySQL database

Before we dive into our MySQL Node.js example, let's ensure you have the necessary prerequisites. First, you'll need to have Node.js and npm (Node Package Manager) installed on your system. If you haven't already, download and install Node.js from the official website, which comes bundled with npm. Next, you'll need to install the 'mysql' package, which allows Node.js applications to interact with MySQL databases. You can do this by running npm install mysql in your project's root directory.

Node-RED MYSQL How to?
Node-RED MYSQL How to?

Setting Up a MySQL Server

A MySQL server needs to be set up before you can connect to it from your Node.js application. Assuming you have already installed MySQL, create a new database and user for your application.

Push Notifications With NodeJS
Push Notifications With NodeJS

Here's an example of how you can create a new database named 'mydatabase' and a user named 'myuser' with the password 'mypassword' using the MySQL command-line client:

```sql CREATE DATABASE mydatabase; CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES; ```

Connecting to a MySQL Server

NodeJS Import CSV To Database
NodeJS Import CSV To Database

Now that your MySQL server is set up, it's time to connect to it from your Node.js application. In your JavaScript file, import the 'mysql' module and use the 'createConnection' method to establish a connection with the server.

Here's a simple example demonstrating how to connect to a MySQL server using Node.js:

```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'myuser', password: 'mypassword', database: 'mydatabase' }); connection.connect((err) => { if (err) throw err; console.log('Connected to the database'); }); ```

Executing Queries

MySQL | Tpoint Tech
MySQL | Tpoint Tech

Once connected to the MySQL server, you can execute SQL queries using the 'query' method on the established connection.

Here's an example of how to insert data into a table and retrieve the results using Node.js:

```javascript connection.query('INSERT INTO users (name, email) VALUES (?, ?)', ['John Doe', 'john.doe@example.com'], (error, results, fields) => { if (error) throw error; console.log('User added with ID:', results.insertId); connection.query('SELECT * FROM users', (error, results, fields) => { if (error) throw error; console.log(results); connection.end(); }); }); ```

Handling Promises and Async/Await

Node.JS Cheatsheet
Node.JS Cheatsheet

In modern Node.js applications, it's more common to see promises and async/await used to handle asynchronous operations. The 'mysql' package supports these features, allowing you to write cleaner and more readable code.

Here's an example of how to insert data into a table and retrieve the results using promises and async/await:

Learn to use MySQL with Nodejs
Learn to use MySQL with Nodejs
Ways To Read Files In NodeJS
Ways To Read Files In NodeJS
Simplify Input Validation with Zod in Node.js
Simplify Input Validation with Zod in Node.js
Node.js Project Folder Structure
Node.js Project Folder Structure
How To Check NodeJS Version
How To Check NodeJS Version
Build a Real-Time Node.js App with MySQL Database
Build a Real-Time Node.js App with MySQL Database
Simple CRUD Node.js & MySQL
Simple CRUD Node.js & MySQL
Read CSV Files In NodeJS
Read CSV Files In NodeJS
Distributed SQL vs NewSQL vs Traditional RDBMS β€” 2025 Comparison
Distributed SQL vs NewSQL vs Traditional RDBMS β€” 2025 Comparison
Structure of URL
Structure of URL
π—‘π—Όπ—±π—²π—·π˜€ console module provides a π—–π—Όπ—»π˜€π—Όπ—Ήπ—² class
π—‘π—Όπ—±π—²π—·π˜€ console module provides a π—–π—Όπ—»π˜€π—Όπ—Ήπ—² class
SQL LEFT() Explained with Examples | Extract Characters from the Left in SQL Server
SQL LEFT() Explained with Examples | Extract Characters from the Left in SQL Server
a diagram with the words what is sol? and two different types of commands
a diagram with the words what is sol? and two different types of commands
a screen shot of a web page with the words'html input types '
a screen shot of a web page with the words'html input types '
Use MySQL in Node.js - Geeky Hacker
Use MySQL in Node.js - Geeky Hacker
a piece of paper that has been drawn with numbers on it and lines in the middle
a piece of paper that has been drawn with numbers on it and lines in the middle
Read Files In Javascript
Read Files In Javascript
Node JS Key Features
Node JS Key Features
an image of a black background with colorful lines and numbers on it, all in different colors
an image of a black background with colorful lines and numbers on it, all in different colors
SQL Execution Order Explained β€” Why SELECT Runs 5th (Cheat Sheet)
SQL Execution Order Explained β€” Why SELECT Runs 5th (Cheat Sheet)

```javascript const connection = mysql.createConnection({ host: 'localhost', user: 'myuser', password: 'mypassword', database: 'mydatabase' }); (async () => { try { await connection.connect(); console.log('Connected to the database'); const result = await connection.query('INSERT INTO users (name, email) VALUES (?, ?)', ['Jane Doe', 'jane.doe@example.com']); console.log('User added with ID:', result.insertId); const users = await connection.query('SELECT * FROM users'); console.log(users); await connection.end(); } catch (error) { console.error('Error:', error); } })(); ```

In conclusion, using a MySQL Node.js example allows you to create powerful, scalable, and efficient applications that can interact with MySQL databases seamlessly. Whether you're building a web application, a backend API, or any other type of application that requires a database, exploring MySQL with Node.js is an essential skill to have. As you've seen in this article, connecting to a MySQL server and executing queries using Node.js is straightforward and can be achieved even without extensive knowledge of either MySQL or Node.js.