Featured Article

Master MySQL and Node JS Tutorial: Build Fast Databases

Adam Jul 13, 2026

Embarking on a journey to master server-side scripting? Look no further than MySQL and Node.js, a powerful combination that's an industry standard for high-performance, scalable, and secure web applications. Let's dive right in, exploring how to create, connect, and manipulate databases using MySQL with Node.js.

Ways To Read Files In NodeJS
Ways To Read Files In NodeJS

Before we proceed, ensure you've installed Node.js, npm (Node Package Manager), and have MySQL Server running on your local machine. If not, visit the official websites for easy downloads and installation guides. Once setup, we're ready to roll!

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

Initializing Our Project & Setting Up MySQL

Creating a new Node.js project is a breeze. Open your terminal and type: npm init -y to create a new project with default settings. Now, we need to install the necessary dependencies. Type npm install mysql to add MySQL to your project.

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

With the server running, locate the MySQL configuration file (my.cnf) and adjust settings like root username, password, and TCP port (if needed). Also, ensure the server starts on system boot by adding it to your system's startup scripts.

Connecting to MySQL from Node.js

NodeJS Import CSV To Database
NodeJS Import CSV To Database

Now, let's write our first MySQL script with Node.js. Create a new file app.js and initialize a connection to your MySQL server. Here's a basic example:

const mysql = require('mysql');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'your_password',
  database: 'your_database'
});

Replace the placeholders with your MySQL username, password, and database name.

Basic CRUD Operations

React Node.js MySQL CRUD Tutorial for Beginners
React Node.js MySQL CRUD Tutorial for Beginners

Now, let's perform basic Create, Read, Update, and Delete (CRUD) operations.

  • Create - Use connection.query('INSERT INTO table SET column=?', [value], (err, results, fields) => {...}).
  • Read - Use connection.query('SELECT * FROM table', (error, results, fields) => {...}).
  • Update - Use connection.query('UPDATE table SET column=? WHERE id=?', [value, id], (err, result) => {...}).
  • Delete - Use connection.query('DELETE FROM table WHERE id=?', [id], (err, result) => {...}).

Here's an example of creating a new user:

Read CSV Files In NodeJS
Read CSV Files In NodeJS

connection.query('INSERT INTO users SET username=?, email=?', ['johndoe', 'johndoe@example.com'], (err, results, fields) => {
  if (err) throw err;
  console.log('User created!');
});

Async/Await & Promises for Enhanced Readability

Nairobi, Kenya's fast-growing capital, offers a perfect blend of urban bustle and natural beauty, especially in its national parks like Nairobi National Park and Karura Forest. Certainly, a must-visit for nature lovers. Similarly, embracing async/await and promises in Node.js increases readability and simplifies error handling when dealing with MySQL.

Push Notifications With NodeJS
Push Notifications With NodeJS
Logging in Node.js the Right Way
Logging in Node.js the Right Way
a man with glasses is smiling in front of a sign that says noodlej's in 1 hour
a man with glasses is smiling in front of a sign that says noodlej's in 1 hour
an image of a computer screen with some text on it
an image of a computer screen with some text on it
Node.JS Cheatsheet
Node.JS Cheatsheet
NodeJS + MySQL Database Connection Tutorial
NodeJS + MySQL Database Connection Tutorial
Node.js MySQL tutorial: a step-by-step getting started guide with Express js REST API
Node.js MySQL tutorial: a step-by-step getting started guide with Express js REST API
SQL Joins Explained Better Than Most Tutorials
SQL Joins Explained Better Than Most Tutorials
Javascript String Method
Javascript String Method
Simplify Input Validation with Zod in Node.js
Simplify Input Validation with Zod in Node.js
Nodejs Development Company | Best Services by Top Nodejs Developers
Nodejs Development Company | Best Services by Top Nodejs Developers
Node.js & Express.js: Build Fast & Scalable Web Apps 🚀🌍
Node.js & Express.js: Build Fast & Scalable Web Apps 🚀🌍
Build a Node.js API - tutorial
Build a Node.js API - tutorial
tg: webguild
tg: webguild
Introduction to NodeJS
Introduction to NodeJS
an image of some type of webpage with dark colors and blue text on it
an image of some type of webpage with dark colors and blue text on it
Node.js tutorial for beginners - an introduction to Node.js with Express.js
Node.js tutorial for beginners - an introduction to Node.js with Express.js
Start Building with Node.js: A Beginner-Friendly Guide
Start Building with Node.js: A Beginner-Friendly Guide
JavaScript Tutorial
JavaScript Tutorial
Node.js roadmap
Node.js roadmap

Here's our previous example with async/await:

async function createUser(username, email) {
  const connection = await mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'your_password',
    database: 'your_database'
  });

  try {
    await connection.query('INSERT INTO users SET username=?, email=?', [username, email]);
    console.log('User created!');
  } catch (err) {
    throw err;
  } finally {
    connection.release();
  }
}

Error Handling & Transactions

Proper error handling and transactions are essential for smooth database operations. To handle errors, use try/catch blocks:

try {
  // Attempt to execute your SQL query
} catch (err) {
  // Handle or log your error
}

For complex operations involving multiple queries, commit transactions to ensure atomicity:

  • Start a transaction: connection.beginTransaction()
  • Perform your operations
  • Commit: connection.commit()
  • Or rollback: connection.rollback()

The journey's not over yet, but you've learned the fundamentals of MySQL with Node.js. Ready to explore more? Dive deeper into MySQL's extensive documentation and Node.js libraries. Happy coding!