🔧 Lesson 2: Installing MySQL (LAMP Stack)
Time to get MySQL running on your machine. This lesson walks you through installation on Windows (via WSL), macOS (via Homebrew), and native Linux — plus setting up Apache, PHP, and phpMyAdmin so you have a complete local development environment.
🎯 Learning Objectives
By the end of this lesson, you will be able to:
- Install MySQL on your operating system (Windows, macOS, or Linux)
- Start and stop the MySQL server
- Set a secure root password
- Install Apache and PHP for phpMyAdmin (optional on macOS/Windows)
- Verify that MySQL is running and accepting connections
Estimated Time: 45 minutes
What You'll Need: A computer with an internet connection and admin/sudo access
📑 In This Lesson
The Big Picture: What We're Installing
In Lesson 1, we mentioned the LAMP stack — Linux, Apache, MySQL, PHP. Here's what each piece does and why we need it:
Operating System
The foundation"] A --> C["Apache
Web Server
Serves web pages"] A --> D["MySQL
Database Server
Stores your data"] A --> E["PHP
Scripting Language
Powers phpMyAdmin"] style D fill:#3b82f6,stroke:#1e40af,color:#fff
| Component | What It Does | Why We Need It |
|---|---|---|
| Linux | Operating system | MySQL runs natively on Linux; Windows users get it through WSL |
| Apache | Web server | Serves the phpMyAdmin interface in your browser |
| MySQL | Database server | This is what we're here to learn — the star of the show |
| PHP | Server-side language | phpMyAdmin is written in PHP, so we need PHP to run it |
💡 Do I Need the Full LAMP Stack?
Strictly speaking, you only need MySQL to follow this course — you can do everything from the command line. Apache and PHP are only required if you want to use phpMyAdmin, a visual browser-based tool for managing databases. We highly recommend it, especially for beginners, but it's optional. If you prefer the command line only, you can skip the Apache/PHP steps.
Choose Your Installation Path
Pick the section that matches your operating system:
| Your OS | Installation Method | Jump To |
|---|---|---|
| Windows 10/11 | WSL 2 + Ubuntu (recommended) | Windows Section ↓ |
| macOS | Homebrew | macOS Section ↓ |
| Ubuntu / Debian Linux | APT (native) | Linux Section ↓ |
📖 Why WSL for Windows?
While MySQL does offer a native Windows installer, using WSL (Windows Subsystem for Linux) gives you a real Linux environment inside Windows. This is the industry-standard approach for web development on Windows — it matches what you'd use on a production server, makes Apache/PHP/phpMyAdmin setup smoother, and means the Linux commands you learn here work exactly the same in deployment. Most professional developers on Windows use WSL.
Windows: Installing via WSL (Ubuntu)
Step 1: Install WSL and Ubuntu
If you don't already have WSL set up, open PowerShell as Administrator (right-click the Start button → "Terminal (Admin)" or "PowerShell (Admin)") and run:
wsl --install
This installs WSL 2 with Ubuntu as the default Linux distribution. When it finishes:
- Restart your computer when prompted
- After reboot, Ubuntu will launch automatically and ask you to create a username and password — this is your Linux user account (separate from your Windows login)
- Remember this password! You'll need it for
sudocommands
⚠️ Already Have WSL?
If you already have WSL and Ubuntu installed, just open your Ubuntu terminal and skip to Step 2. You can check by running wsl --list in PowerShell.
Step 2: Update Your System
Open your Ubuntu terminal (search "Ubuntu" in the Start menu) and update your package lists:
sudo apt update && sudo apt upgrade -y
Step 3: Install MySQL Server
sudo apt install mysql-server -y
Step 4: Start MySQL
In WSL, services don't auto-start like on a regular Linux server. Start MySQL manually:
sudo service mysql start
Check that it's running:
sudo service mysql status
You should see output indicating the service is active (running).
✅ WSL Tip: Auto-Starting MySQL
Since WSL doesn't auto-start services on boot, you'll need to run sudo service mysql start each time you open a new Ubuntu session. You can add it to your ~/.bashrc file to automate this:
echo "sudo service mysql start" >> ~/.bashrc
You may also want to configure WSL boot commands for a cleaner solution.
Step 5: Install Apache and PHP (for phpMyAdmin)
If you want to use phpMyAdmin (recommended), install Apache and PHP:
sudo apt install apache2 php php-mysql libapache2-mod-php -y
Start Apache:
sudo service apache2 start
Verify Apache is working by opening your browser and visiting http://localhost. You should see the Apache default page.
⚠️ Port Conflict?
If http://localhost doesn't load, another program may be using port 80. Common culprits on Windows include IIS, Skype, or other web servers. You can check with sudo lsof -i :80 in WSL or change Apache's port in /etc/apache2/ports.conf.
Now skip ahead to the Securing Your Installation section.
macOS: Installing via Homebrew
Step 1: Install Homebrew (if needed)
Homebrew is the standard package manager for macOS. If you don't have it, open Terminal (Applications → Utilities → Terminal) and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the prompts. When it finishes, you may need to add Homebrew to your PATH. The installer will tell you exactly what to run — it's usually something like:
# For Apple Silicon Macs (M1/M2/M3/M4):
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
# For Intel Macs:
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/usr/local/bin/brew shellenv)"
Verify Homebrew is working:
brew --version
Step 2: Install MySQL
brew install mysql
Step 3: Start MySQL
Start MySQL and configure it to launch automatically on login:
brew services start mysql
Verify it's running:
brew services list
You should see mysql listed with a status of started.
💡 Homebrew Services
Using brew services start sets MySQL to auto-start whenever you log in. If you prefer manual control, use mysql.server start and mysql.server stop instead.
Step 4: Install Apache and PHP (for phpMyAdmin)
macOS comes with Apache pre-installed, but for a cleaner setup with PHP, use Homebrew:
# Install PHP
brew install php
# Start the built-in Apache (macOS already has it)
sudo apachectl start
Verify Apache by visiting http://localhost in your browser. You should see an "It works!" page.
📖 Alternative: Skip Apache/PHP
On macOS, many developers skip the full LAMP setup and just use MySQL from the command line. If you'd prefer not to install Apache and PHP, that's perfectly fine — you can follow this entire course using just the MySQL CLI. The phpMyAdmin setup is optional.
Now skip ahead to the Securing Your Installation section.
Linux (Native Ubuntu/Debian)
If you're running Ubuntu or Debian natively (not through WSL), the process is the same as the WSL instructions but services auto-start.
Step 1: Update Your System
sudo apt update && sudo apt upgrade -y
Step 2: Install the Full LAMP Stack
You can install everything in one command:
sudo apt install apache2 mysql-server php php-mysql libapache2-mod-php -y
Step 3: Verify Services Are Running
On native Linux, services usually start automatically after installation:
# Check MySQL
sudo systemctl status mysql
# Check Apache
sudo systemctl status apache2
Both should show active (running). If not, start them:
sudo systemctl start mysql
sudo systemctl start apache2
Enable them to start on boot:
sudo systemctl enable mysql
sudo systemctl enable apache2
Now continue to the Securing Your Installation section.
Securing Your Installation
By default, MySQL's root account may have no password or use a socket-based authentication method. Let's secure it.
Run the Security Script
MySQL ships with a built-in security script that walks you through hardening your installation:
sudo mysql_secure_installation
The script will ask you several questions. Here are the recommended answers for a development environment:
| Prompt | Recommended Answer | Why |
|---|---|---|
| VALIDATE PASSWORD component? | No | Keeps things simple for development; use Yes for production |
| Set root password? | Yes — choose a password you'll remember | Secures the root account |
| Remove anonymous users? | Yes | Prevents unauthenticated access |
| Disallow root login remotely? | Yes | Root should only connect from localhost |
| Remove test database? | Yes | Removes the insecure default test database |
| Reload privilege tables? | Yes | Applies all changes immediately |
Set Up Password Authentication for Root
On Ubuntu, MySQL's root user defaults to auth_socket authentication (login via sudo without a password). For this course, we'll switch to password authentication so you can log in from phpMyAdmin and other tools:
# Log in as root (uses sudo/socket auth)
sudo mysql
Now run these SQL commands inside the MySQL prompt:
-- Switch root to password authentication
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password_here';
-- Apply changes
FLUSH PRIVILEGES;
-- Exit
EXIT;
⚠️ Choose a Real Password
Replace your_password_here with an actual password. For a local development environment, something simple but memorable is fine (e.g., mysql_dev_2026). Never use a blank password or "password", even for development.
Test that password login works:
mysql -u root -p
You'll be prompted for the password you just set. If you see the mysql> prompt, you're in! Type EXIT; to leave.
💡 macOS Homebrew Note
On macOS with Homebrew, MySQL typically starts with no root password. Run mysql_secure_installation the same way. If you get an error about connecting, try mysql -u root first (no sudo needed on macOS), set the password, then proceed with the security script.
Installing phpMyAdmin
phpMyAdmin is a free, web-based tool for managing MySQL databases. It lets you create databases, design tables, run queries, import/export data, and manage users — all from your browser. It's especially helpful when you're learning because you can see your data visually.
Ubuntu / WSL Installation
sudo apt install phpmyadmin -y
During installation, you'll see some prompts:
- Web server to reconfigure automatically: Select
apache2(use spacebar to select, then Enter) - Configure database for phpmyadmin with dbconfig-common? Select Yes
- Password for phpMyAdmin's database user: Choose a password (this is for phpMyAdmin's own internal database, not your root password)
If Apache doesn't pick up phpMyAdmin automatically, enable it manually:
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin
sudo service apache2 reload
Now open your browser and visit http://localhost/phpmyadmin. Log in with:
- Username:
root - Password: the root password you set in the previous section
macOS Installation
On macOS, the easiest approach is to download phpMyAdmin manually:
- Download the latest version from phpmyadmin.net/downloads
- Extract the archive and rename the folder to
phpmyadmin - Move it to your web server's document root:
# Default Apache document root on macOS sudo mv ~/Downloads/phpmyadmin /Library/WebServer/Documents/phpmyadmin - Create a configuration file:
cd /Library/WebServer/Documents/phpmyadmin cp config.sample.inc.php config.inc.php - Edit
config.inc.phpand set a blowfish secret (any random 32-character string):$cfg['blowfish_secret'] = 'put-a-random-32-character-string-here!!'; - Restart Apache:
sudo apachectl restart - Visit http://localhost/phpmyadmin
📖 macOS Alternative: Use MySQL Workbench
If setting up Apache/PHP on macOS feels like overkill, consider using MySQL Workbench instead — it's a free, native macOS app from Oracle that provides a visual database editor. Download it from the MySQL website or install via Homebrew: brew install --cask mysqlworkbench
Verifying Everything Works
Let's run through a quick checklist to make sure your entire setup is working.
1. MySQL Is Running
# Ubuntu / WSL
sudo service mysql status
# macOS (Homebrew)
brew services list
# Native Linux
sudo systemctl status mysql
You should see active or started.
2. You Can Log In
mysql -u root -p
Enter your password. If you see the mysql> prompt, run a quick test:
-- Check MySQL version
SELECT VERSION();
-- List existing databases
SHOW DATABASES;
-- Exit
EXIT;
Expected Output:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.xx |
+-----------+
1 row in set (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
Those four databases are MySQL's built-in system databases — you'll never need to touch them directly. Your own databases will appear in this list as you create them.
3. Apache Is Serving Pages (if installed)
Open your browser and go to http://localhost. You should see a default Apache page.
4. phpMyAdmin Is Accessible (if installed)
Visit http://localhost/phpmyadmin and log in with your root credentials. You should see the phpMyAdmin dashboard with your databases listed on the left.
✅ All Systems Go!
If all four checks pass, congratulations — your development environment is fully set up! You have MySQL running, you can log in via the command line, and (optionally) you have phpMyAdmin for visual database management.
Troubleshooting Common Issues
⚠️ "Can't connect to local MySQL server through socket"
This usually means MySQL isn't running. Start it:
# WSL / Ubuntu
sudo service mysql start
# macOS
brew services start mysql
# Native Linux
sudo systemctl start mysql
⚠️ "Access denied for user 'root'@'localhost'"
Your password is wrong, or root is still using socket authentication. Try:
# Log in via sudo (bypasses password)
sudo mysql
# Then reset the password inside MySQL:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
FLUSH PRIVILEGES;
EXIT;
⚠️ phpMyAdmin shows a blank page or error
Make sure the PHP MySQL extension is installed and Apache is reloaded:
sudo apt install php-mysql php-mbstring php-zip php-gd php-json php-curl -y
sudo service apache2 restart
⚠️ "dpkg: error processing package mysql-server"
If the MySQL install fails on Ubuntu/WSL, try:
sudo apt --fix-broken install
sudo apt install mysql-server -y
⚠️ macOS: "ERROR 2002 (HY000): Can't connect"
If you installed via Homebrew and get connection errors:
# Make sure MySQL is running
brew services restart mysql
# Try connecting without sudo
mysql -u root
On macOS, you typically don't use sudo with MySQL commands.
⚠️ WSL: localhost not loading in browser
Newer versions of WSL2 may use a different IP. Try:
# Find your WSL IP
hostname -I
Then visit http://<that-ip>/phpmyadmin instead of http://localhost/phpmyadmin. Alternatively, check that WSL networking is configured for localhost forwarding.
Exercises
🏋️ Exercise 1: Confirm Your Setup
Run through this checklist and verify each item:
- Open your terminal and run
mysql --version— what version are you running? - Log into MySQL with
mysql -u root -p - Run
SELECT VERSION();— does it match? - Run
SHOW DATABASES;— how many system databases do you see? - Type
EXIT;to leave
✅ What You Should See
mysql --versionshows something likemysql Ver 8.0.xxSELECT VERSION();returns the same version numberSHOW DATABASES;shows 4 system databases: information_schema, mysql, performance_schema, sys
🏋️ Exercise 2: Explore phpMyAdmin (if installed)
- Open http://localhost/phpmyadmin and log in
- Click on any system database in the left sidebar (try
mysql) - Click the SQL tab at the top
- Type
SELECT VERSION();and click Go - Observe the result — you just ran your first query in phpMyAdmin!
✅ What You Should See
The SQL tab should show a text area where you can type queries. After clicking Go, the result appears below with your MySQL version number in a table format. This is the same query you ran from the command line — phpMyAdmin is just a visual wrapper around the MySQL CLI.
🎯 Quick Quiz
Question 1: What does the "M" in LAMP stand for?
Question 2: Which command logs you into MySQL from the command line?
Question 3: What SQL command shows all databases on the server?
Summary
🎉 Key Takeaways
- The LAMP stack (Linux, Apache, MySQL, PHP) is the classic web development environment
- Windows users install MySQL through WSL (Ubuntu) for a real Linux environment
- macOS users install MySQL through Homebrew with
brew install mysql - Linux users install directly with
sudo apt install mysql-server - Always run
mysql_secure_installationto set a root password and remove defaults - Switch root from socket auth to password auth with
ALTER USERso phpMyAdmin can connect - phpMyAdmin provides a visual, browser-based interface for managing databases
- Use
mysql -u root -pto log in from the command line
🚀 What's Next?
Your database server is up and running. In the next lesson, you'll learn to navigate both the MySQL command-line interface (CLI) and phpMyAdmin — running queries, exploring databases, and getting comfortable with the tools you'll use throughout this course.
🎉 Congratulations!
You've installed MySQL and set up your development environment. The hardest part of any course is getting the tools working — and you've done it. Let's put them to use!