How to Deploy MySQL 5.7 with Docker: A Complete Step‑by‑Step Guide
This article walks you through downloading the MySQL 5.7 Docker image, configuring IPv4 networking, launching the container with proper volume mounts, troubleshooting common startup errors, verifying the setup, and discussing production‑grade considerations such as data persistence, memory isolation, and scaling.
1. Download MySQL Image
Visit Docker Hub, locate the mysql:5.7.42‑oracle tag and pull it with docker pull mysql:5.7.42‑oracle. The pull output shows each layer being downloaded and the final image digest.
docker pull mysql:5.7.42-oracle2. Configure IPv4 (Optional)
On a virtual machine you may need to enable IPv4 forwarding. Add net.ipv4_forward=1 to /usr/lib/sysctl.d/00-system.conf and restart networking and Docker services.
net.ipv4_forward=1 systemctl restart network
systemctl restart docker2.1 Verify
Check the sysctl setting and ensure the services restart without errors.
3. Start MySQL Container
Map host port 3306 to container port 3306, give the container a name, and mount host directories for logs, data, and configuration files. Set the root password via the MYSQL_ROOT_PASSWORD environment variable and enforce UTF‑8 encoding.
docker run -p 3306:3306 \
--name mysql \
-v /home/mysql/log:/var/log/mysql \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.42-oracle \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ciThe initial run may fail because the mounted /etc/mysql directory does not exist. Remove that volume mapping and retry.
# Remove the problematic volume
docker rm mysql
# Re‑run without the /etc/mysql mount
docker run -p 3306:3306 \
--name mysql \
-v /home/mysql/log:/var/log/mysql \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.42-oracle \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ciAfter the container starts, verify it is running:
docker ps4. Verify File Mounts
Enter the container and list the mounted directories to confirm they were created correctly.
docker exec -it mysql bash
pwd
ls -l5. Monitor Container Stats
Use docker stats to watch CPU, memory, network, and I/O usage in real time.
docker stats mysql6. Connect with a Local Client
Use any MySQL client (e.g., MySQL Workbench, CLI) to connect to localhost:3306 with the root credentials.
7. Configure MySQL Inside the Container
Change the root authentication method, make the password never expire, and allow remote connections.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
ALTER USER 'root'@'%' IDENTIFIED BY 'root' PASSWORD EXPIRE NEVER;
UPDATE mysql.user SET host='%' WHERE user='root';
FLUSH PRIVILEGES;8. Production‑grade Considerations
8.1 Scaling MySQL
In large‑scale environments MySQL is usually deployed as a cluster (e.g., Galera, InnoDB Cluster). Ad‑hoc container scaling is rarely used for production databases.
8.2 Data Persistence
Persist data by mounting host directories or using dedicated volume drivers. Deleting a container does not delete the data stored on the host.
Containers are isolated runtime instances; images are templates. Removing a container leaves the host‑mounted data untouched.
8.3 Memory Isolation
Other services (cache, MQ) share the same host memory. Limit each service’s memory usage via Docker’s --memory flag or cgroup settings to avoid contention.
8.4 Avoid Single‑Point‑of‑Failure
Run MySQL in its own container or VM, and consider managed cloud databases for higher availability.
9. Summary
Deploying MySQL with Docker provides portability, isolation, and easier management. By pulling the official image, configuring proper volume mounts, handling networking, and applying basic security tweaks, you can run a reliable MySQL instance for development or small‑scale production workloads.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
