1. Overview of MySQL Containerization
1.1 Advantages of containerized deployment
- Environmental consistency: Eliminate the problem of "can run on my machine" and ensure that the development, testing and production environments are completely consistent
- Rapid deployment: Start MySQL instance in seconds, it takes more than 10 minutes to install it in traditional way
- Resource Isolation: Each container instance has an independent running environment to avoid resource conflicts
- Version Management: Easily switch different MySQL versions (5.7/8.0, etc.)
- Portability: Mirrors can run across platforms and support x86/ARM architecture
1.2 Official mirror features
MySQL official Docker image provides:
- Multiple version selection (latest version/specific version)
- Two images based on Alpine and Debian
- Automatic initialization mechanism
- Environment variable configuration support
- Default security configuration (non-root running)
2. Basic deployment practice
2.1 Pull the official image
Get the latest MySQL 8.0 image:
docker pull mysql:8.0
Verify the mirror:
docker image inspect mysql:8.0 | jq '.[0].'
2.2 Minimize the startup command
docker run -d \ --name mysql-dev \ -e MYSQL_ROOT_PASSWORD=my-secret-pw \ -p 3306:3306 \ mysql:8.0
Parameter description:
-
-d
: Backend running -
--name
: Container naming -
-e
: Set environment variables (required to set root password) -
-p
: Port mapping (host port: container port)
2.3 Connection Verification
Connect using MySQL client:
mysql -h 127.0.0.1 -P 3306 -u root -p # Enter the password my-secret-pw
Connection within container:
docker exec -it mysql-dev mysql -u root -p
3. Data persistence solution
3.1 Mounting the data directory
Create a data directory:
mkdir -p ~/mysql-data/{data,,initdb}
Start the container:
docker run -d \ --name mysql-prod \ -v ~/mysql-data/data:/var/lib/mysql \ -v ~/mysql-data/:/etc/mysql/ \ -v ~/mysql-data/initdb:/ \ -e MYSQL_ROOT_PASSWORD=strongpassword \ mysql:8.0
Directory function:
-
/var/lib/mysql
: MySQL data file -
/etc/mysql/
: Custom configuration file -
/
: Initialize SQL scripts
3.2 Custom configuration files
Example:
[mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci max_connections=200 innodb_buffer_pool_size=512M
3.3 Database initialization
Place SQL files in the initdb directory:
echo "CREATE DATABASE appdb;" > ~/mysql-data/initdb/ echo "CREATE USER 'appuser'@'%' IDENTIFIED BY 'userpass';" >> ~/mysql-data/initdb/ echo "GRANT ALL ON appdb.* TO 'appuser'@'%';" >> ~/mysql-data/initdb/
4. Safety reinforcement configuration
4.1 Non-root run
docker run -d \ --user mysql \ --read-only \ --cap-drop ALL \ mysql:8.0
4.2 Password encryption policy
docker run -d \ -e MYSQL_ROOT_PASSWORD=securepass \ -e MYSQL_SECURE_TRANSPORT=ON \ -e MYSQL_SSL=ON \ mysql:8.0
4.3 Network Isolation
Create a dedicated network:
docker network create mysql-net docker run -d \ --network mysql-net \ --name mysql-secure \ -p 3306:3306 \ mysql:8.0
5. Advanced management skills
5.1 Performance Tuning Parameters
docker run -d \ --name mysql-tuned \ -e MYSQL_ROOT_PASSWORD=pass123 \ -e MYSQL_INNODB_BUFFER_POOL_SIZE=1G \ -e MYSQL_INNODB_LOG_FILE_SIZE=256M \ -e MYSQL_MAX_CONNECTIONS=500 \ mysql:8.0
5.2 Master-slave replication configuration
Main library:
docker run -d \ --name mysql-master \ -e MYSQL_ROOT_PASSWORD=masterpass \ -e MYSQL_REPLICATION_USER=repl \ -e MYSQL_REPLICATION_PASSWORD=replpass \ -e MYSQL_MASTER_PORT=3306 \ mysql:8.0 \ --server-id=1 \ --log-bin=mysql-bin \ --binlog-format=ROW
From the library:
docker run -d \ --name mysql-slave \ --link mysql-master:master \ -e MYSQL_ROOT_PASSWORD=slavepass \ -e MYSQL_MASTER_HOST=master \ -e MYSQL_MASTER_USER=repl \ -e MYSQL_MASTER_PASSWORD=replpass \ -e MYSQL_MASTER_PORT=3306 \ mysql:8.0 \ --server-id=2
5.3 Backup Recovery Plan
Perform a backup:
docker exec mysql-prod sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' >
Timed backup script:
#!/bin/bash BACKUP_DIR=/path/to/backups DATE=$(date +%Y%m%d_%H%M%S) docker exec mysql-prod mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} --all-databases | gzip > ${BACKUP_DIR}/backup_${DATE}. find ${BACKUP_DIR} -name "*." -mtime +7 -delete
6. Production environment deployment suggestions
6.1 Resource Limitations
docker run -d \ --name mysql-production \ --memory="4g" \ --memory-swap="6g" \ --cpus="2" \ --blkio-weight=500 \ mysql:8.0
6.2 Monitoring configuration
Enable Performance Mode:
docker run -d \ -e MYSQL_PERFORMANCE_SCHEMA=ON \ mysql:8.0
Integration Prometheus:
docker run -d \ -e MYSQL_EXPORTER=ON \ -p 9104:9104 \ mysql:8.0
6.3 High availability scheme
Use a health check:
docker run -d \ --health-cmd="mysqladmin ping -uroot -p$$MYSQL_ROOT_PASSWORD" \ --health-interval=10s \ --health-timeout=3s \ --health-retries=3 \ mysql:8.0
7. Troubleshooting of FAQs
7.1 Startup failed diagnosis
View the log:
docker logs mysql-prod
Common errors:
- Password not set:
MYSQL_ROOT_PASSWORD
Missing environment variables - Port conflict: 3306 Port has been occupied
- Permissions issue: Data directory cannot be written
7.2 Performance problem analysis
Check the running status:
docker exec -it mysql-prod mysqladmin -uroot -p status
Slow query log:
docker exec mysql-prod sh -c 'cat /var/log/mysql/'
Version 7.3 Upgrade Process
- Back up all data
- Start a new version of the container:
docker run -d \ --name mysql-new \ -v ~/mysql-data/data:/var/lib/mysql \ mysql:8.1
- Run mysql_upgrade
- Switch traffic after verification
8. Summary of best practices
- Data persistence: The /var/lib/mysql directory must be mounted
- Password security: Use strong passwords and change them regularly
- Resource limitations: Reasonably allocate CPU/memory according to server configuration
- Regular backup: Implement automated backup strategy
- Monitoring alarm: Configure performance monitoring and abnormal alarms
- Version control: Clearly record the MySQL version used
This is the article about the process steps of Docker using run command to deploy MySQL. For more related content on Docker running deployment MySQL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!