Running MySQL in Docker: Installation, Configuration, and Basic Operations
This article explains how to install Docker, pull a MySQL image, configure container volumes, run the MySQL service, manage containers, and perform backups, providing a practical guide for deploying MySQL in a Docker environment.
1. Docker Introduction : Docker is a Linux container technology that isolates applications, offering fast creation, low resource consumption, and benefits such as efficient building, rapid deployment, high resource utilization, easy migration, and simple update management.
2. Installing Docker : Prerequisites include a 64‑bit CPU, kernel ≥3.8 (CentOS ≥3.10), a supported storage driver (Device Mapper, AUFS, vfs, btrfs), and enabled cgroup/namespace features. Commands to check the kernel ( uname -r), verify Device Mapper, install Docker via yum -y install docker.io or the official script ( curl -s https://get.docker.com | sh), and start the service ( service docker start or systemctl start docker.service) are provided.
3. MySQL Deployment in Docker : Pull the MySQL 5.7.30 image with docker pull mysql:5.7.30, verify it using docker images, and run a container with port mapping, environment variable for the root password, and volume mounts for configuration, logs, and data directories:
#docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7.304. Container Management : Use docker ps or docker container ls to list containers, docker start / docker stop to control them, and docker exec -it <container_id> /bin/bash to enter the container. Inside, access MySQL with mysql -uroot -p123456.
5. Configuration and Persistence : An example my.cnf is provided with settings for server ID, character set, port, transaction isolation, max connections, data directory, socket, PID file, and error log. Host directories are created and mounted using the -v option to ensure data persists across container restarts.
6. Backup : Perform a full backup using mysqldump inside the container:
docker exec <container_id> sh -c 'exec mysqldump -uroot -p123456 -P3309 --single-transaction --master-data --all-databases' > /opt/all-databases.sql7. Summary : Deploying MySQL in Docker is straightforward with minimal performance overhead (≈10%). Further exploration can cover Docker data persistence (volumes vs bind mounts), namespace and cgroup isolation, orchestration tools such as Mesos, Swarm, Kubernetes, and network configuration options.
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.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.
