πŸ› οΈ

Chapter 1: Environment Setup

Install MySQL 8, configure development environment

1 Installing MySQL 8

macOS (Homebrew)

The easiest way to install MySQL on macOS is through Homebrew. If you don't have Homebrew installed, visit brew.sh first.

# Install MySQL 8
brew install mysql

# Start MySQL service
brew services start mysql

# Secure installation (set root password)
mysql_secure_installation

Ubuntu / Debian

On Ubuntu/Debian systems, use the APT package manager to install MySQL from the official repository.

# Update package index
sudo apt update

# Install MySQL Server
sudo apt install mysql-server

# Start and enable MySQL
sudo systemctl start mysql
sudo systemctl enable mysql

# Run security script
sudo mysql_secure_installation

Windows

On Windows, download the MySQL Installer from the official website and follow the GUI wizard.

# Download from:
# https://dev.mysql.com/downloads/installer/

# Or use winget (Windows Package Manager)
winget install Oracle.MySQL

# Or use Chocolatey
choco install mysql

Docker (Cross-platform)

Docker provides an isolated, reproducible environment regardless of your operating system.

# Pull and run MySQL 8 container
docker run --name mysql8 \
  -e MYSQL_ROOT_PASSWORD=yourpassword \
  -p 3306:3306 \
  -d mysql:8

# Connect to the container
docker exec -it mysql8 mysql -uroot -p

Verify Installation

After installation, verify that MySQL is running correctly.

# Check MySQL version
mysql --version
# Expected output: mysql  Ver 8.x.x for Linux on x86_64 (MySQL Community Server - GPL)

# Check service status (Linux)
sudo systemctl status mysql

# Check service status (macOS)
brew services list | grep mysql
πŸ’‘ PostgreSQL comparison: PostgreSQL uses brew install postgresql@16 on macOS and sudo apt install postgresql on Ubuntu. The default port is 5432 instead of MySQL's 3306.

2 Connecting to MySQL

CLI Login

The MySQL command-line client is the most direct way to interact with your database server.

# Connect as root (will prompt for password)
mysql -u root -p

# Connect with host and port specified
mysql -u root -p -h 127.0.0.1 -P 3306

# Connect and execute a single command
mysql -u root -p -e "SELECT VERSION();"

Basic Database Operations

Once connected, you can explore and manage databases with these fundamental commands.

-- Show all databases
SHOW DATABASES;

-- Expected output:
-- +--------------------+
-- | Database           |
-- +--------------------+
-- | information_schema |
-- | mysql              |
-- | performance_schema |
-- | sys                |
-- +--------------------+

-- Create a new database
CREATE DATABASE sql_tutorial
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

-- Switch to the new database
USE sql_tutorial;

-- Confirm current database
SELECT DATABASE();
-- +---------------+
-- | DATABASE()    |
-- +---------------+
-- | sql_tutorial  |
-- +---------------+

Creating a Dedicated User

For security, create a dedicated user instead of using root for daily development.

-- Create a new user
CREATE USER 'dev'@'localhost' IDENTIFIED BY 'dev_password';

-- Grant all privileges on the tutorial database
GRANT ALL PRIVILEGES ON sql_tutorial.* TO 'dev'@'localhost';

-- Apply privilege changes
FLUSH PRIVILEGES;

-- Now connect as the new user
-- mysql -u dev -p sql_tutorial
πŸ’‘ PostgreSQL comparison: PostgreSQL uses psql -U postgres to connect. The command to list databases is \l instead of SHOW DATABASES, and to switch databases you use \c dbname instead of USE dbname.

3 Client Tools

While the CLI is powerful, graphical clients can boost productivity significantly. Here are the most popular options.

πŸ–₯️ MySQL CLI

Built-in command-line tool, available on all platforms after installation.

  • βœ… No additional installation
  • βœ… Scriptable and automatable
  • βœ… SSH-friendly for remote servers
  • ⚠️ No visual query builder

πŸ”§ MySQL Workbench

Official GUI tool from Oracle, free and feature-rich.

  • βœ… Visual schema designer
  • βœ… Query builder and editor
  • βœ… Performance monitoring
  • ⚠️ Can feel heavy on older machines

🦫 DBeaver

Free, open-source universal database tool. Supports MySQL, PostgreSQL, SQLite, and many more.

  • βœ… Multi-database support
  • βœ… ER diagram generation
  • βœ… Data export/import
  • ⚠️ Java-based, slower startup

πŸ’Ž DataGrip

JetBrains' commercial database IDE with intelligent SQL completion.

  • βœ… Smart code completion
  • βœ… Refactoring support
  • βœ… Version control integration
  • ⚠️ Paid (free for students)
Recommendation: Start with the MySQL CLI to build strong fundamentals, then move to DBeaver (free) or DataGrip (paid) for daily development work. Understanding the CLI helps with debugging and server administration.

4 Creating Your First Table

Let's create a users table that we'll use throughout this tutorial series. This table demonstrates the most common column types and constraints.

-- Make sure you're in the right database
USE sql_tutorial;

-- Create the users table
CREATE TABLE users (
    id         INT           AUTO_INCREMENT PRIMARY KEY,
    name       VARCHAR(50)   NOT NULL,
    email      VARCHAR(100)  NOT NULL UNIQUE,
    age        INT           DEFAULT NULL,
    created_at DATETIME      DEFAULT CURRENT_TIMESTAMP
);

-- Verify table structure
DESCRIBE users;
-- +------------+--------------+------+-----+-------------------+-------------------+
-- | Field      | Type         | Null | Key | Default           | Extra             |
-- +------------+--------------+------+-----+-------------------+-------------------+
-- | id         | int          | NO   | PRI | NULL              | auto_increment    |
-- | name       | varchar(50)  | NO   |     | NULL              |                   |
-- | email      | varchar(100) | NO   | UNI | NULL              |                   |
-- | age        | int          | YES  |     | NULL              |                   |
-- | created_at | datetime     | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
-- +------------+--------------+------+-----+-------------------+-------------------+

Insert Sample Data

Populate the table with some test data for upcoming chapters.

INSERT INTO users (name, email, age) VALUES
('Alice Johnson', 'alice@example.com', 28),
('Bob Smith', 'bob@example.com', 35),
('Charlie Lee', 'charlie@example.com', 22),
('Diana Wang', 'diana@example.com', 31),
('Eve Brown', 'eve@example.com', NULL);

-- Verify the data
SELECT * FROM users;
-- +----+---------------+---------------------+------+---------------------+
-- | id | name          | email               | age  | created_at          |
-- +----+---------------+---------------------+------+---------------------+
-- |  1 | Alice Johnson | alice@example.com   |   28 | 2025-01-01 10:00:00 |
-- |  2 | Bob Smith     | bob@example.com     |   35 | 2025-01-01 10:00:00 |
-- |  3 | Charlie Lee   | charlie@example.com |   22 | 2025-01-01 10:00:00 |
-- |  4 | Diana Wang    | diana@example.com   |   31 | 2025-01-01 10:00:00 |
-- |  5 | Eve Brown     | eve@example.com     | NULL | 2025-01-01 10:00:00 |
-- +----+---------------+---------------------+------+---------------------+

Understanding the Column Definitions

Column Type Constraints Description
id INT PRIMARY KEY, AUTO_INCREMENT Unique identifier, auto-generated
name VARCHAR(50) NOT NULL User's display name, required
email VARCHAR(100) NOT NULL, UNIQUE Email address, must be unique
age INT DEFAULT NULL Optional age field
created_at DATETIME DEFAULT CURRENT_TIMESTAMP Auto-set to insertion time
πŸ’‘ PostgreSQL comparison: PostgreSQL uses SERIAL or GENERATED ALWAYS AS IDENTITY instead of AUTO_INCREMENT. The equivalent would be: id SERIAL PRIMARY KEY or id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY.

5 Chapter Summary

MySQL Installation

Use Homebrew on macOS, APT on Ubuntu, or Docker for a cross-platform approach. Always run mysql_secure_installation after setup.

Connection Basics

Connect via mysql -u root -p. Use SHOW DATABASES to list, CREATE DATABASE to create, and USE to switch.

Client Tools

Master the CLI first, then choose a GUI: MySQL Workbench (free, official), DBeaver (free, multi-DB), or DataGrip (paid, powerful).

First Table

Created a users table with common column types: INT, VARCHAR, DATETIME, and constraints like PRIMARY KEY, NOT NULL, UNIQUE.

Character Set

Always use utf8mb4 encoding for full Unicode support including emojis. Set it at the database or table level.

Security Best Practice

Create a dedicated user for development instead of using root. Grant only necessary privileges on specific databases.

πŸ“š Tutorial Index Next: Chapter 2: Basic Queries