概述
目前,远程备份只能使用 mysqldump 或 MySQL 8.0 提供的 Clone 功能;MySQL中增量备份必须借助 binlog ,所以我们在远程执行 mysqldump 做备份之后,需要远程备份 binlog 日志。
我们以搭建binlog服务器的模式进行MySQL远程增量备份。
原理
mysqlbinlog 是 MySQL binlog 解析工具,可以对mysqlbinlog进行解析,将其转化为SQL文本;也可以模拟从服务器,按照复制协议向主库获取 binlog 日志,下面增量备份将以这个技术实现。
实战
数据库IP:10.106.34.221
以下操作全部在远程端,并不会在被纳管库执行
1、远程登录库,造一些数据
mysql -h10.106.34.221 -uroot -pQwer1234 -e "
drop database if exists remote_inc_backup;
create database remote_inc_backup;
use remote_inc_backup;
create table t (id int);
insert into t values(1);
select * from t ;
"
2、远程全备
MySQL 8.0.26 需要使用 --source-data 否则导入将被错
MySQL 8.0.26 以下版本可以使用 --master-data=2
mysqldump -h10.106.34.221 -uroot -pQwer1234 --single-transaction --source-data=2 --set-gtid-purged=OFF -R -E remote_inc_backup> remote_inc_backup.sql
3、查看binlog文件以及position信息
Sangfor:DBVM/host-fefcfe5eb075 ~ # grep CHANGE remote_inc_backup.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000060', MASTER_LOG_POS=3028;
binlog 文件名为 mysql-bin.000060,position 为3028
4、建立实时增量复制链路
mysqlbinlog --read-from-remote-server --raw --host=10.106.34.221 --port=3306 --user=root --password='Qwer1234' --start-position=3028 --stop-never mysql-bin.000060
5、另开一个窗口,登陆被纳管库,继续造一些数据
mysql -h10.106.34.221 -uroot -pQwer1234 -e "
use remote_inc_backup;
insert into t values(2);
insert into t values(3);
insert into t values(4);
select * from t ;
"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
6、Ctrl+C 停止增量复制,并进行全量+增量还原
mysql -uroot -pQwer1234 -h127.0.0.1 -e "create database remote_inc_backup;"
mysql -uroot -pQwer1234 -h127.0.0.1 remote_inc_backup < remote_inc_backup.sql
mysqlbinlog mysql-bin.000060 | mysql -uroot -pQwer1234 -h127.0.0.1
Sangfor:DBVM/host-fefcfe5eb075 ~ # mysql -uroot -pQwer1234 -h127.0.0.1 -e "select * from remote_inc_backup.t;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
增量数据已被还原。
参考文献: