----------------------------------------恢复到某个时间点
全备
[root@zqr-cs-1 binlog]# mysqldump -uroot -predhat --default-character-set=utf8 --max-allowed-packet=128M --flush-logs --set-gtid-purged=OFF --hex-blob --triggers --routines --events --master-data --single-transaction --databases test > /backup/test_20200611.sql
全备之后,数据库发生了dml操作
mysql> insert into a values ('test1',1), ('test2',2), ('test3',3);
Query OK, 3 rows affected (0.01 sec)
突发:删除数据库test!!!
查看全备到删除数据库这个时间段的操作!!
[root@zqr-cs-1 binlog]# mysqlbinlog --no-defaults -v --base64-output=DECODE-ROWS --start-datetime="2020-06-11 16:44:00" --stop-datetime="2020-06-11 16:52:00" mysql-bin.000018
利用binlog进行数据库恢复。
[root@zqr-cs-1 binlog]# mysqlbinlog --no-defaults --start-datetime="2020-06-11 16:44:00" --stop-datetime="2020-06-11 16:52:00" mysql-bin.000018 | mysql -uroot -predhat
完成恢复!!
---------------------------------------------恢复到某个position
mysql>show binlog events in 'mysql-bin.000004';
mysqlbinlog -d db --start-position=219--stop-position=907 /usr/local/mysql/binlog/mysql-bin.000004>/tmp/recover.sql
set sql_log_bin=0;
source /tmp/recover.sql;
-----------------------------------误删数据后,利用binlog,恢复数据
flush logs; 开启新的binlog
定位误删sql语句的位置!
sudo mysqlbinlog --base64-output=DECODE-ROWS -v -d dbname mysql-bin.000001 | grep --ignore-case -A3 -B4 '错误的sql语句'
恢复数据时,可能会有重复数据的报错,建议用-f参数忽略。
# mysql -uroot -p -f dbname < data.sql
如果要恢复表级别的数据,导出成sql后再进行过滤grep即可。
# more data.sql | grep --ignore-case -E 'insert|update|delete' | grep table
NOTE! 推荐使用postion位置点进行数据库恢复。
Only use the --start-datetime and --stop-datetime options to help you find the actual event positions of interest. Using the two options to specify the range of binary log segment to apply is not recommended: there is a higher risk of missing binary log events when using the options. Use --start-position and --stop-position instead.