mysql 利用relay log 使用 start slave until 恢复方法

1结构

 

 注意: server-id 必须不相同

 

2. slave2 使用 screen 备份主库 binlog 到/data/backup/目录下, 实时备份mysqlbinlog

(1).  安装 screen
yum  -y install screen
screen -S  mysqlbinlogsbackup

cd /data/backup/; mysqlbinlog --raw --read-from-remote-server --stop-never --host 172.17.0.2 --port 3306 -u replication -p123456 mybinlog.000003


ctrl+a d
暂时离开当前session,将目前的 screen session 丢到后台.

 


 

screen -S  screenname -> 新建一个session
screen -ls         ->     列出所有的session
screen -r  screenname -> 回到screenname 这个session
screen -d  screenname -> 远程detach某个session
screen -d -r screenname -> 结束当前session并回到screenname 这个session

 

3. mysql master 上 创建数据库, sysbench 写一些数据

create database  T100

  sysbench /usr/local/share/sysbench/oltp_read_write.lua --mysql-host=172.17.0.2 --mysql-port=3306 --mysql-user=fengjian --mysql-password=123456 --mysql-db=T100 --tables=10 --table_size=10000 --mysql_storage_engine=Innodb prepare

 

4. 使用innobackup 进行全备

innobackupex --defaults-file=/etc/my.cnf -S /data/mysql/mysql3306/mysql.sock  -u fengjian -p123456 /data/backup/

 

5. 删除master T100库的表, 模拟误删除操作

mysql -u root -p 
use T100;
drop table  sbtest1 ;

#发生误操作,切换binlogs
flush logs;

查看binlog
show binary logs;

+-----------------+-----------+
| Log_name | File_size |
+-----------------+-----------+
| mybinlog.000001 | 177 |
| mybinlog.000002 | 177 |
| mybinlog.000003 | 200 |
| mybinlog.000004 | 200 |
| mybinlog.000005 | 200 |
| mybinlog.000006 | 200 |
| mybinlog.000007 | 200 |
| mybinlog.000008 | 19097126 |
| mybinlog.000009 | 19097508 |
| mybinlog.000010 | 194 |
+-----------------+-----------+

 

6. 打包全备数据 到 slave 从库上

cd /data/backup
tar  -zcvf  2020.tar.gz  2020-02-29_05-36-06  

scp  172.17.0.2:/data/backup/2020.tar.gz  root@172.17.0.4:/data/backup/

 

7. slave2 上解压缩,恢复数据库

cd /data/backup/
tar -zxvf 2020.tar.gz 
cd  2020-02-29_05-36-06
# innobackup 应用日志 innobackupex --apply-log -u root -p123456 /data/backup/2020-02-29_05-36-06

#slave2 上新建的mysql 存储目录不需要初始化
mkdir -R /data/mysqld/mysql3306/{data,logs,tmp,undolog}
chown -R mysql.mysql /data/mysqld

#拷贝 /data/backup/2020-02-29_05-36-06/ 备份文件到 mysql 存储目录
mv /data/backup/2020-02-29_05-36-06/undo* /data/mysql/mysql3306/undolog/
mv /data/backup/2020-02-29_05-36-06/* /data/mysql/mysql/mysql3306/data/

chown -R mysql.mysql /data/mysql

# 启动mysql
mysqld --defaults-file=/etc/mysql.cnf &

 

由于mysql master 和 slave2 都是开启gtid的,所以恢复全量备份数据到slave2实例上,如果启动slave2实例 Gtid和 master xtrabackup全量备份结束的Gtid号是不一致
登录slave2 实例,reset master  清空 Gtid 信息, 然后再set global gtid_purged='bde7b592-b966-11e9-8c64-000c294f3e61:1-10296'; 让slave2 实例的Gtid号执行到全量备份结束时的这个Gtid号

查看 xtrabackup_binlog_info 信息

mybinlog.000009    19097277    0e21dacd-5aa5-11ea-a4db-0242ac110002:1-404,
3f6088fc-5ab6-11ea-b88f-0242ac110002:1-605,
b9d7ea3f-62b0-11ea-b3d0-0242ac110002:1-123

 

slave2 设置 gtid purged值

reset master;
# 设置gtid purged
set global  gtid_purged='0e21dacd-5aa5-11ea-a4db-0242ac110002:1-404,3f6088fc-5ab6-11ea-b88f-0242ac110002:1-605,b9d7ea3f-62b0-11ea-b3d0-0242ac110002:1-123'
#查看gtid信息
show global variables like '%gtid%';

+----------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+----------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 0e21dacd-5aa5-11ea-a4db-0242ac110002:1-404,3f6088fc-5ab6-11ea-b88f-0242ac110002:1-605,b9d7ea3f-62b0-11ea-b3d0-0242ac110002:1-61 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | 0e21dacd-5aa5-11ea-a4db-0242ac110002:1-404,3f6088fc-5ab6-11ea-b88f-0242ac110002:1-605,b9d7ea3f-62b0-11ea-b3d0-0242ac110002:1-61 |
| session_track_gtids | OFF |
+----------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+

 

 

 

8. 开始MySQL relaylog + SQL_Thread 增量恢复binlog演示

change命令,是为了告诉MySQL自己为一个slave实例:通过change命令,是为了告诉MySQL自己为一个slave实例,因为无需用到IO_Thread,故host,password,user等可以随意填写。
并且通过该步骤,生成relay.info文件。

root@localhost 08:06:  [(none)]> change master to master_host='172.17.0.2';

查看 relay log 的名字为,slave2-relay-bin 开头

root@localhost 08:06: [(none)]> show global variables like '%relay%';
+---------------------------+---------------------------------------------------+
| Variable_name | Value |
+---------------------------+---------------------------------------------------+
| max_relay_log_size        | 0 |
| relay_log             | |
| relay_log_basename        | /data/mysql/mysql3306/data/slave2-relay-bin |
| relay_log_index          | /data/mysql/mysql3306/data/slave2-relay-bin.index |
| relay_log_info_file       | relay-log.info |
| relay_log_info_repository    | TABLE |
| relay_log_purge          | ON |
| relay_log_recovery        | ON |
| relay_log_space_limit      | 0 |
| sync_relay_log           | 10000 |
| sync_relay_log_info       | 10000 |
+---------------------------+---------------------------------------------------+


 

关闭3308实例,将需要增量的binlog文件伪装成relay log:

mysqladmin  -u root -p shutdown
cd /data/mysql/mysql3306/data/

# 查看目录里生成了 relay log日志 和索引   slave2-relay-bin.000001  slave2-relay-bin.index

ls 

 



# 拷贝mysqlbinlog  server备份的 binlog到 data目录。
cp /data/backup/mysqlbin*  /data/mysql/mysql3306/data/

# 删除原有 relay log 和 index 文件,使用binlog 生成

rm slave2-relay-bin.*  -rf
for i in `ls mybinlog.00*` ; do lastnum=`echo $i |cut -d '.' -f2`; mv mybinlog.$lastnum slave2-relay-bin.$lastnum ; done
ls ./slave2-relay-bin.0000* > slave2-relay-bin.index

 

启动slave2 mysql

mysqld  --defaults-file=/etc/my.cnf &

 

使用 change master to语句, 让 slave2 的sql_thread 执行  relay log文件到 xtrabackup_binlog_info 记录的位置

[root@slave2 2020-03-11_06-02-09]# cat xtrabackup_binlog_info 
mybinlog.000009    19097277    0e21dacd-5aa5-11ea-a4db-0242ac110002:1-404, 3f6088fc-5ab6-11ea-b88f-0242ac110002:1-605, b9d7ea3f-62b0-11ea-b3d0-0242ac110002:1-123

 

slave2 上执行 change master to,  注意 replay_log_file 文件变成了 slave2-relay-bin.000009

change master to RELAY_LOG_FILE='slave2-relay-bin.000009', RELAY_LOG_POS=19097277;

 

解析mysqlbinlogs 查看删除表的 gtid位置

 

 

 

 

使用 start slave until  恢复到删除的 gtid 或者 

START SLAVE   SQL_THREAD    UNTIL    MASTER_LOG_FILE = 'slave2-relay-bin.000009', MASTER_LOG_POS =19097277 ; ##此处的Gtid是drop table test1_event 前的最近的一个binlog的文件的pos位置点
 或者是:
 START SLAVE   SQL_THREAD  UNTIL   SQL_BEFORE_GTIDS='b9d7ea3f-62b0-11ea-b3d0-0242ac110002:124'  ##此处的Gtid是drop table test1_event 前的最近的一个Gtid


利用SQL_thread快速恢复增量过程总结:

1.不能使用master_auto_position=1
2.先要让mysql知道他是一个Slave
3.关掉mysql,构建relay-log
4.利用change master to relay_log_file=... ,
relay_log_pos=...;
5.START SLAVE SQL_THREAD UNTIL MASTER_LOG_FILE='xxx',MASTER_LOG_POS=xxxxx
或者START SLAVE SQL_THREAD UNTIL SQL_BEFORE_GTIDS='xxx--xx-x';

 

 

参考:

https://blog.csdn.net/baping8015/article/details/100952782

 

posted @ 2020-03-11 16:55  fengjian1585  阅读(1503)  评论(0编辑  收藏  举报