Docker中对PXC集群进行数据备份

准备环境:

  1.5台PXC容器(部署可参考另一篇博客:Docker搭建MySQL集群

 

操作步骤:

1.停掉并删除node1节点,重新映射v1卷和backup卷,并以加入node2节点的方式启动容器

docker volume create backup
docker stop node1
docker rm node1
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123qqq...A -e CLUSTER_NAME=PXC -e CLUSTER_JOIN=node2 -e XTRABACKUP_PASSWORD=123qqq...A -v v1:/var/lib/mysql -v backup:/data --privileged --name=node1 --net=net1 --ip 172.20.0.2 pxc

2.进入容器,安装xtrabackup,备份数据

# 进入容器后,系统是root用户
docker exec -it --user root node1 bash
# 修改国内镜像,更新,然后安装
mv /etc/apt/sources.list apt-sources-list.ba
cat << EOF > /etc/apt/sources.list
> deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
> deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
> deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse12345678910
> EOF
apt-get update
apt-get install percona-xtrabackup

#备份数据(如果数据库没有数据,添加一些数据后在进行备份)
innobackupex --user=root --password=123qqq...A /data/backup/full

3.进行全量恢复

  注意:PXC集群数据库可以热备份,但是不能热还原。为了避免恢复过程中的数据同步,我们采用空白的MySQL还原数据,然后再建立PXC集群

  我们要先解除PXC集群,删除所有节点,然后创建一个新容器进行还原后,再建立PXC集群

# 停止所有容器
docker stop node1 node2 node3 node4 node5
# 删除容器
docker rm node1 node2 node3 node4 node5
# 删除存储卷
docker volume rm v1 v2 v3 v4 v5
# 创建新卷,并启动容器
docker volume create v1
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123qqq...A -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123qqq...A -v v1:/var/lib/mysql -v backup:/data --privileged --name=node1 --net=net1 --ip 172.20.0.2 pxc

# 进入容器
docker exec -it node1 bash
# 清空mysql数据
rm -rf /var/lib/mysql/*
# 未提交事务回滚
innobackupex --user=root --password=123qqq...A --apply-back /data/backup/full/2019-05-17_09-30-12/
# 执行冷还原
innobackupex --user=root --password=123qqq...A --copy-back /data/backup/full/2019-05-17_09-30-12/

#退出容器,重启node1节点
docker stop node1
docker start node1

4.进入数据库,查看后,就可以重新建立PXC集群

posted @ 2019-05-17 11:28  落魄山陈平安  阅读(760)  评论(0编辑  收藏  举报