1.下载备份工具: wget https://downloads.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0.22-15/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.22-15.1.el7.x86_64.rpm 注意xtrabackup8不再有innodbackupex
2.docker 启动数据库(mysql 的docker启用默认会读取/etc/mysql/my.cnf,所以要把配置文件放到宿主的一个目录-v进去) docker run --restart=always --privileged=true -d -v /usr/mysql/conf/:/etc/mysql/ -v /usr/mysql/data:/var/lib/mysql -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.21 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 配置文件: vim /usr/mysql/conf/my.cnf [client]
#socket = /usr/mysql/mysqld.sock
default-character-set = utf8mb4
[mysqld]
#pid-file = /var/run/mysqld/mysqld.pid
#socket = /var/run/mysqld/mysqld.sock
#datadir = /var/lib/mysql
#socket = /usr/mysql/mysqld.sock
#pid-file = /usr/mysql/mysqld.pid
datadir = /var/lib/mysql
character_set_server = utf8mb4
collation_server = utf8mb4_bin
secure-file-priv= NULL
Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
Custom config should go here
#!includedir /etc/mysql/conf.d/
3.备份方法: 第一步:构造一个自己备份的my.cnf.bak /etc/my.cnf.bak 里面写如下内容: [mysqld] datadir=/usr/mysql/data log-bin=/usr/mysql/data/binlog log-bin-index=/usr/mysql/data/binlog.index #socket=/var/lib/mysql/mysql.sock
Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
Settings user and group are ignored when systemd is used.
If you need to run mysqld under a different user or group,
customize your systemd unit file for mariadb according to the
instructions in http://fedoraproject.org/wiki/Systemd
#[mysqld_safe] #log-error=/var/log/mariadb/mariadb.log #pid-file=/var/run/mariadb/mariadb.pid
include all files from the config directory
#!includedir /etc/my.cnf.d
第二步:执行备份脚本
xtrabackup --defaults-file=/etc/my.cnf.bak --datadir=/usr/mysql/data --host='10.25.15.86' --user='root' --password='123456' --port=3306 --backup --target-dir=/home/mysqlbackup/FULL-253database-date +%Y-%m-%d-%H-%M-%S
4.模拟删掉数据文件,进行恢复 模拟故障: docker rm -v mysql -f 再另外一台机器启动: docker run --restart=always --privileged=true -d -v /usr/mysql/conf/:/etc/mysql/ -v /usr/mysql/data:/var/lib/mysql -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.21 删除数据目录 docker stop mysql cd /usr/mysql/data rm -rf ./*
注意这里指定的配置文件时docker容器mysql的配置文件 xtrabackup --defaults-file=/usr/mysql/conf/my.cnf --prepare --target-dir=/home/mysqlbackup/FULL-253database-2021-01-22-23-34-04/ /usr/mysql/conf/my.cnf修改datadir=/usr/mysql/data xtrabackup --defaults-file=/usr/mysql/conf/my.cnf --copy-back --target-dir=/home/mysqlbackup/FULL-253database-2021-01-22-23-34-04/ 然后/usr/mysql/conf/my.cnf再修改 datadir=/var/lib/mysql docker start mysql