mysql如何利用binlog进行数据恢复详解

本文详细介绍了MySQL的Binary Log(binlog)如何用于数据恢复,包括binlog的基本配置、三种格式(ROW、STATEMENT、MIX)的特点,以及如何利用mysqlbinlog进行数据操作的查看和恢复。此外,还提到了binlog2sql工具帮助解析binlog。

 

前言:

MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个:

* 数据回复

* 主从数据库。用于slave端执行增删改,保持与master同步

如果线上误操作了一个数据或者被恶意攻击了,由于是直接修改的数据库,所有唯一的恢复方式就在mysql的binlog。binlog使用的是ROW模式,即受影响的每条记录都会生成一个sql。同时利用了binlog2sql项目。

mysqlbinlog 官方操作文档:https://dev.mysql.com/doc/refman/5.6/en/mysqlbinlog.html

注意:

数据库最好要作定时快照备份,避免需要增量更新的数量过多

 

binlog基本配置和格式

binlog基本配置

binlog需要在mysql的配置文件的mysqld节点中进行配置:

# 日志中的Serverid
server-id = 1
# 日志路径
log_bin  = /var/log/mysql/mysql-bin.log
# 保存几天的日志
expire_logs_days = 10
# 每个binlog的大小
max_binlog_size = 1000M
#binlgo模式
binlog_format=ROW
# 默认是所有记录,可以配置哪些需要记录,哪些不记录
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name

查看binlog状态

  • SHOW BINARY LOGS; 查看binlog文件
  • SHOW VARIABLES LIKE '%log_bin%' 查看日志状态
  • SHOW MASTER STATUS 查看日志文件位置

binlog的三种格式

1.ROW

针对行记录日志,每行修改产生一条记录(即每一行数据操作都会产生独立的sql记录)。

优点:上下文信息比较全,恢复某条误操作时可以直接在日志中查找到原文信息,对于主从复制支持好。

缺点:输出非常大,如果是Alter语句将产生大量的记录

2.STATEMENT

针对sql语句的,每条语句产生一条记录

优点:产生的日志量比较小,主从版本可以不一致

缺点:主从有些语句不能支持,像自增主键和UUID这种类型的

3.MIX

结合了两种的优点,一般情况下都采用STATEMENT模式,对于不支持的语句采用ROW模式

常用操作语法:(要找到能允许mysqlbinlog 执行的位置运行)

数据操作的查看与恢复:

mysql自带的mysqlbinlog

由于binlog是二进制的,所以需要先转换成文本文件,一般可以采用Mysql自带的mysqlbinlog转换成文本。

mysqlbinlog --no-defaults --base64-output='decode-rows' -d room -v mysql-bin.011012 > /root/binlog_2020-04-29

参数说明

  • --no-defaults 为了防止报错:mysqlbinlog: unknown variable 'default_character_set=utf8mb4'
  • --base64-output='decode-rows' 和-v一起使用, 进行base64解码
    其他有很多用来限定范围的参数,比如数据库,起始时间,起始位置等等。这些参数在查找误操作的时候非常有用。
  • 操作选项:

    Option NameDescriptionIntroduced
    --base64-outputPrint binary log entries using base-64 encoding 
    --bind-addressUse specified network interface to connect to MySQL Server 
    --binlog-row-event-max-sizeBinary log max event size 
    --character-sets-dirDirectory where character sets are installed 
    --connection-server-idUsed for testing and debugging. See text for applicable default values and other particulars5.6.20
    --databaseList entries for just this database 
    --debugWrite debugging log 
    --debug-checkPrint debugging information when program exits 
    --debug-infoPrint debugging information, memory, and CPU statistics when program exits 
    --default-authAuthentication plugin to use 
    --defaults-extra-fileRead named option file in addition to usual option files 
    --defaults-fileRead only named option file 
    --defaults-group-suffixOption group suffix value 
    --disable-log-binDisable binary logging 
    --exclude-gtidsDo not show any of the groups in the GTID set provided 
    --force-if-openRead binary log files even if open or not closed properly 
    --force-readIf mysqlbinlog reads a binary log event that it does not recognize, it prints a warning 
    --helpDisplay help message and exit 
    --hexdumpDisplay a hex dump of the log in comments 
    --hostHost on which MySQL server is located 
    --include-gtidsShow only the groups in the GTID set provided 
    --local-loadPrepare local temporary files for LOAD DATA in the specified directory 
    --login-pathRead login path options from .mylogin.cnf 
    --no-defaultsRead no option files 
    --offsetSkip the first N entries in the log 
    open_files_limitSpecify the number of open file descriptors to reserve 
    --passwordPassword to use when connecting to server 
    --plugin-dirDirectory where plugins are installed 
    --portTCP/IP port number for connection 
    --print-defaultsPrint default options 
    --protocolConnection protocol to use 
    --rawWrite events in raw (binary) format to output files 
    --read-from-remote-masterRead the binary log from a MySQL master rather than reading a local log file 
    --read-from-remote-serverRead binary log from MySQL server rather than local log file 
    --result-fileDirect output to named file 
    --secure-authDo not send passwords to server in old (pre-4.1) format5.6.17
    --server-idExtract only those events created by the server having the given server ID 
    --server-id-bitsTell mysqlbinlog how to interpret server IDs in binary log when log was written by a mysqld having its server-id-bits set to less than the maximum; supported only by MySQL Cluster version of mysqlbinlog 
    --set-charsetAdd a SET NAMES charset_name statement to the output 
    --shared-memory-base-nameName of shared memory to use for shared-memory connections 
    --short-formDisplay only the statements contained in the log 
    --skip-gtidsDo not print any GTIDs; use this when writing a dump file from binary logs containing GTIDs 
    --socketUnix socket file or Windows named pipe to use 
    --ssl-crlFile that contains certificate revocation lists 
    --ssl-crlpathDirectory that contains certificate revocation-list files 
    --ssl-modeDesired security state of connection to server5.6.30
    --start-datetimeRead binary log from first event with timestamp equal to or later than datetime argument 
    --start-positionDecode binary log from first event with position equal to or greater than argument 
    --stop-datetimeStop reading binary log at first event with timestamp equal to or greater than datetime argument 
    --stop-neverStay connected to server after reading last binary log file 
    --stop-never-slave-server-idSlave server ID to report when connecting to server 
    --stop-positionStop decoding binary log at first event with position equal to or greater than argument 
    --to-last-logDo not stop at the end of requested binary log from a MySQL server, but rather continue printing to end of last binary log 
    --userMySQL user name to use when connecting to server 
    --verboseReconstruct row events as SQL statements 
    --verify-binlog-checksumVerify checksums in binary log 
    --versionDisplay version information and exit 

binlog的基本块如下:

# at 642
#200429  9:43:32 server id 3347870753  end_log_pos 716 CRC32 0x6910cffb 	Query	thread_id=176224	exec_time=0	error_code=0

SET TIMESTAMP=1588124612/*!*/;
BEGIN
/*!*/;
# at 716
# at 895
# at 1895
#200429  9:43:32 server id 3347870753  end_log_pos 1926 CRC32 0x48ba5b4a 	Xid = 512601748
COMMIT/*!*/;

1、# at 642

指明的当前位置相对文件开始的偏移位置,这个在mysqlbinlog命令中可以作为--start-position的参数

2、#181007 1:50:38 server id 1630000 end_log_pos 417844 CRC32 0x9fc3e3cd Query thread_id=440109962 exec_time=0 error_code=0

3、SET TIMESTAMP=1538877038/!/;

BEGIN

 

具体执行的语句

SET TIMESTAMP=1588124612/*!*/;
BEGIN
/*!*/;
# at 716
# at 895
# at 1895
#200429  9:43:32 server id 3347870753  end_log_pos 1926 CRC32 0x48ba5b4a 	Xid = 512601748
COMMIT/*!*/;

一行记录产生的日志如上所示。以SET TIMESTAMP=1539136238/*!*/;开始,以COMMIT/*!*/;结尾。我们可以根据两个at指明的位置来限定范围。

注意一条记录开始的SET TIMESTAMP之前的# at $position 和结尾的COMMIT之后的# at $position

根据binlog文件进行数据的恢复

格式:mysqlbinlog 日志文件 参数 | mysql -u用户名 -p密码

mysqlbinlog liangck.000001 | mysql -uroot –p123456  表示 全部恢复
 
mysqlbinlog liangck.000002 --start-pos=417 --stop-pos=773 | mysql -uroot -p123456 表示 从417至773位置
 
mysqlbinlog liangck.000002 --stop-pos=773 | mysql -uroot -p123456  表示 从头至773位置
 
mysqlbinlog liangck.000002 --start-pos=417 | mysql -uroot -p123456 表示 从417至尾 位置 
 
mysqlbinlog liangck.000002 --start-datetime="2004-12-25 11:25:56"  | mysql -uroot -p123456 日期参数和pos同理

 

还有一些好用的工具例如

利用binlog2sqlbinlog2sql官网介绍:从MySQL binlog解析出你要的SQL。根据不同选项,你可以得到原始SQL、回滚SQL、去除主键的INSERT SQL等。

下一章我们会讲到

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值