Installing MySQL with Docker
Introduction
Docker runs MySQL in an isolated container with one command—same on Windows, macOS, and Linux. This chapter pulls the official mysql:8 image, starts a container with persistent data, connects from the host, and stops or resets cleanly without touching system services.
Prerequisites
- What Is MySQL
- Docker Introduction or Docker Desktop installed
- ~1 GB disk for image layers
Why Docker for Learning
| Benefit | Detail |
|---|---|
| Clean host | No system MySQL service conflict if you choose different port |
| Reproducible | Same env vars → same database every time |
| Reset fast | Remove container and volume to start over |
| Version pinning | mysql:8.0.36 for exact matches |
Pull the Image
docker pull mysql:8
docker images | grep mysqlRun a MySQL Container
docker run -d \
--name mysql-learn \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=learnpass \
-e MYSQL_DATABASE=learn_db \
-e MYSQL_USER=learn_user \
-e MYSQL_PASSWORD=learnpass \
mysql:8| Flag | Meaning |
|---|---|
-d | Background (detached) |
--name mysql-learn | Friendly container name |
-p 3306:3306 | Host port → container 3306 |
MYSQL_ROOT_PASSWORD | Required root password |
MYSQL_DATABASE | Auto-create database on first start |
MYSQL_USER / MYSQL_PASSWORD | Extra non-root user with grants on MYSQL_DATABASE |
Code explanation:
- Environment variables run only on first container initialization with empty data volume
Connect from Host
mysql -h 127.0.0.1 -P 3306 -u learn_user -plearnpass learn_dbOr root:
mysql -h 127.0.0.1 -u root -plearnpassSELECT DATABASE();
SHOW TABLES;
EXIT;Warning
Password on Command Line
-plearnpass exposes password in shell history—fine for local learning; use config file or prompt in production scripts.
Persist Data with a Volume
Without a volume, removing container deletes data:
docker run -d \
--name mysql-learn \
-p 3306:3306 \
-v mysql_learn_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=learnpass \
-e MYSQL_DATABASE=learn_db \
mysql:8Code explanation:
mysql_learn_datanamed volume survives container delete
Daily Commands
docker ps
docker logs mysql-learn
docker stop mysql-learn
docker start mysql-learnRemove container (keep volume):
docker rm -f mysql-learnRemove container and volume (wipe data):
docker rm -f mysql-learn
docker volume rm mysql_learn_dataPort Conflict with Native MySQL
If local MySQL uses 3306, map host port 3307:
-p 3307:3306Connect with -P 3307.
docker compose (Optional)
compose.yml snippet:
Run: docker compose up -d
FAQ
Container exits immediately?
Check docker logs mysql-learn—often missing MYSQL_ROOT_PASSWORD.
Cannot connect from app in another container?
Use service name db as host on same Docker network—not 127.0.0.1.
mysql client not on host?
Install client only, or: docker exec -it mysql-learn mysql -u root -p
ARM Mac image?
Official mysql:8 supports multi-arch.
JDBC connection string?
jdbc:mysql://127.0.0.1:3306/learn_db — see JDBC Docker chapter.
Production on Docker?
Valid with backups and resource limits—beyond this intro chapter.