Chapter 53: Node.js MySQL Create Database

MySQL database in a Node.js application.

I will explain it as if I am sitting next to you right now — I open MySQL Workbench / terminal / VS Code, I show every single command, every decision, every line of code, and I explain why we do things this way, what most beginners get wrong, what intermediate developers often forget, and what production-grade code actually looks like in 2025–2026.

We will build everything from absolute zero — no skipping steps.

Goal of this lesson

We will:

  1. Install and run MySQL locally (or on cloud)
  2. Create a real database and tables with proper structure
  3. Connect Node.js to MySQL using mysql2/promise (the modern & recommended way)
  4. Create a complete mini-API that creates, reads, updates and deletes data
  5. Add validation, error handling, security basics, and best practices

Step 1 – Install and run MySQL locally (Windows / macOS / Linux)

Windows

  1. Download MySQL Community Server from official site → https://dev.mysql.com/downloads/mysql/ → Choose MySQL Installer for WindowsDeveloper Default
  2. Install → choose Server Only or Full → set root password (remember it!)
  3. After install → open MySQL Workbench (comes with installer) or use command line:
Bash

macOS (recommended way – Homebrew)

Bash

Ubuntu / Debian

Bash

Cloud alternative (fastest for learning)

Use PlanetScale free tier, Neon, Railway, Supabase or Aiven → they give you a ready DATABASE_URL — no local install needed

For this tutorial we’ll assume local MySQL — but the code works exactly the same with cloud.

Step 2 – Create the database and tables (with good practices)

Open MySQL Workbench or terminal and run these commands one by one.

SQL

Why these choices?

  • CHAR(36) for UUID → no auto-increment collision issues in distributed systems
  • utf8mb4_unicode_ci → supports emojis, all languages, modern standard
  • ON DELETE CASCADE → when user is deleted → his tasks are automatically deleted
  • TIMESTAMP with ON UPDATE → auto-maintains created/updated times
  • Index on email → login becomes fast

Step 3 – Connect Node.js to MySQL (mysql2/promise – best choice)

src/config/database.ts

TypeScript

src/config/env.ts (Zod validation)

TypeScript

Step 4 – Create first API – Register user

src/controllers/auth.controller.ts

TypeScript

src/routes/auth.routes.ts

TypeScript

src/index.ts (connect everything)

TypeScript

Test it:

Bash

You should get:

JSON

Step 5 – Summary – MySQL + Node.js in 2025–2026 feels like this

You now have:

  • Secure connection pool (mysql2/promise)
  • Environment + database validation (Zod)
  • Raw SQL with prepared statements (prevents SQL injection)
  • UUID primary keys (scalable)
  • Proper error handling
  • TypeScript safety
  • Modern ESM project

This is very close to how many real Node.js + MySQL backends are built today (before adding Prisma/Drizzle/TypeORM).

Which direction would you like to go next?

  • Add login + JWT authentication
  • Add task CRUD endpoints with user ownership
  • Replace raw SQL with Prisma (most popular choice)
  • Replace raw SQL with Drizzle ORM (type-safe SQL)
  • Add connection pooling tuning, slow query logging, monitoring
  • Add Docker + docker-compose with MySQL container

Just tell me what you want to build or understand next — I’ll continue with complete, secure, production-ready code and explanations. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *