This article mainly introduces how to back up and restore the docker MySQL database, and how to implement automatic backup every day. I hope it will be helpful to friends in need!
Docker MySQL database backup and restore, as well as daily scheduled automatic backup
1: Backup
View docker mysql
ubuntu@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a30ead8a26fc mysql:8.0.27 "docker-entrypoint.s…" 10 seconds ago Up 7 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
Method 1:
Step by step, perform regular backup, enter the container, and use mysqldump to back up. [Recommended learning: docker video tutorial]
sudo docker exec -it mysql /bin/bash #进入mysql容器 mysqldump -uroot -p123456 --all-databases > emp_`date +%F`.sql #在容器中备份所有数据库,并以日期命名 exit #退出容器 sudo docker cp mysql:emp_2022-03-17.sql /var/backup/ #将容器中的备份文件,复制到本地备份文件夹中
is displayed as follows:
ubuntu@ubuntu:~$ sudo docker exec -it mysql /bin/bash root@a30ead8a26fc:/# mysqldump -uroot -p123456 --all-databases > emp_`date +%F`.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. root@a30ead8a26fc:/# exit exit ubuntu@ubuntu:~$ sudo docker cp mysql:emp_2022-03-17.sql /var/backup/ ubuntu@ubuntu:~$ ll /var/backup/ total 11492 drwxrwxrwx 2 root root 4096 Mar 17 15:45 ./ drwxr-xr-x 14 root root 4096 Mar 14 17:49 ../ -rw-r--r-- 1 root root 3915599 Mar 17 15:42 emp_2022-03-17.sql
Method 2:
One step, there are two possible commands here
1): (Recommended, there is no problem with restore)
sudo docker exec mysql(容器名) sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +%F`.sql
2): (Not recommended, there is a problem with restore)
Most of the online backup methods are this, but I There is a problem with the restore. You can try version 5.7.
sudo docker exec -it mysql(容器名) mysqldump -uroot -p123456 --all-databases > /var/backup/music_`date +%F`.sql
Note: It may be due to the version (my version is 8.0). There is no problem with this kind of backup, but there will be problems with the restore. The error is reported as follows:
ubuntu@ubuntu:~$ sudo mysql -uroot -p < emp_2022-03-15.sql Enter password: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump: [Warning] Using a password on the command line interface can be insec' at line 1
You can also enter the database and use the command source to restore, but the error will still appear, but will be ignored, resulting in incomplete data recovery in the end. All this is not recommended.
2: Restore
1): Restore in non-container
1: Direct command to restore
mysql -uroot -p123456 < /var/backup/emp_2022-03-15.sql
2: Source restore after entering mysql
mysql -uroot -p source /var/backup/emp_2022-03-15.sql
2): Container mysql restore
needs to be restored in the mysql container.
#复制备份文件到容器中的var目录 sudo docker cp /var/backup/emp_2022-03-17.sql mysql:/var #进入容器 sudo docker exec -it mysql bin/bash #进入mysql mysql -uroot -p #使用备份sql恢复数据库 source /var/backup/emp_2022-03-15.sql #或者不进入mysql直接容器中还原 mysql -uroot -p123456 < /var/backup/emp_2022-03-15.sql
Three: Scheduled tasks
1): Set scheduled tasks
crontab -e
The content is as follows:
0 2 * * * find /backup/ -mtime +1 -name "*.sql" -delete && sudo docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +%F`.sql
Command description:
Every day Executed at 2 o'clock in the morning
find /backup/ -mtime +1 -name "*.sql" -delete #删除2天前的备份任务,即保留3个版本。根据需要自己选择。
2): The scheduled task is not executed
Update: The next day it was found that there was no backup file, check the cron log. If not, please enable cron log. The method is as follows:
#编辑系统日志文件 vim /etc/rsyslog.d/50-default.conf #去掉前面的#注释符号 cron.* /var/log/cron.log #重启日志服务 systemctl restart rsyslog.service
View the scheduled task display
cat /var/log/cron.log .... CRON[2015636]: (root) CMD (find /backup/ -mtime +1 -name "*.sql" -delete && sudo docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +) ....
It is found that %F`.sql after the command is not displayed. The reason is that % is a variable in it. Add the \ escape character, so that Keep the original characters.
0 2 * * * find /backup/ -mtime +1 -name "*.sql" -delete && sudo docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p123456 --all-databases' > /var/backup/music_`date +\%F`.sql
Save and the test will take effect.
The above is the detailed content of docker MySQL realizes automatic backup every day!. For more information, please follow other related articles on the PHP Chinese website!

Docker simplifies application deployment and management on Linux. 1) Docker is a containerized platform that packages applications and their dependencies into lightweight and portable containers. 2) On Linux, Docker uses cgroups and namespaces to implement container isolation and resource management. 3) Basic usages include pulling images and running containers. Advanced usages such as DockerCompose can define multi-container applications. 4) Debug commonly used dockerlogs and dockerexec commands. 5) Performance optimization can reduce the image size through multi-stage construction, and keeping the Dockerfile simple is the best practice.

Docker is a Linux container technology-based tool used to package, distribute and run applications to improve application portability and scalability. 1) Dockerbuild and dockerrun commands can be used to build and run Docker containers. 2) DockerCompose is used to define and run multi-container Docker applications to simplify microservice management. 3) Using multi-stage construction can optimize the image size and improve the application startup speed. 4) Viewing container logs is an effective way to debug container problems.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools