MySQL 二进制日志binlog介绍

测试环境:
centos 6.4
MySQL 5.6.37

文章目录

  • 一.MySQl binlog介绍
    • 1.1 binlog的作用
    • 1.2 binlog的位置
    • 1.3 binlog相关参数
    • 1.4 binlog格式
    • 1.5 清理binlog
    • 1.6 binlog和redo的区别
  • 二.MySQl binlog恢复
    • 2.1 语句级别的恢复
    • 2.2 row格式的恢复
  • 参考文献:

一.MySQl binlog介绍

1.1 binlog的作用

逻辑日志,记录的是数据库内部的所有变动(sql语句 行的改变)
server层日志,binlog不仅仅记录innodb的变动,也记录myisam存储引擎的变动。
innodb redo 是存储引擎层,和binlog不是一层,redo只记录innodb的变化。
binlog不记录 select show 等查询语句,记录dml ddl等数据库变动的语句.

可以查看mysql变更
mysql的复制架构(主从)
mysql备份恢复(可以通过binlog进行数据的补齐或者回滚)

1.2 binlog的位置

mysql> show variables like '%log_bin%';
+---------------------------------+-------------------------------------+
| Variable_name                   | Value                               |
+---------------------------------+-------------------------------------+
| log_bin                         | ON                                  |
| log_bin_basename                | /u01/my3306/log/binlog/binlog       |
| log_bin_index                   | /u01/my3306/log/binlog/binlog.index |
| log_bin_trust_function_creators | OFF                                 |
| log_bin_use_v1_row_events       | OFF                                 |
| sql_log_bin                     | ON                                  |
+---------------------------------+-------------------------------------+
6 rows in set (0.00 sec)

mysql> 

log_bin为on代表开启了binlog
log_bin_basename的上一层是binog日志的位置/u01/my3306/log/binlog

1.3 binlog相关参数

参数 参数用途
log-bin 决定了msyql 的binlog的名字,生成的binlog名字为mysql-bin.000001
binlog_format 规定binlog的格式,binlog有三种格式statement,row以及mixed,默认使用默认使用statement,建议使用row格式
expire_logs_days 过期时间 建议大家根据业务设置7D-30D
binlog_do_db 此参数表示只记录指定数据库的二进制日志
binlog_ignore_db 此参数表示不记录指定的数据库的二进制日志
sync_binlog 0—每一秒刷一次磁盘
1—每次事务提交都刷一次磁盘
n—(100 200 500)每n次提交落盘一次
innodb_flush_log_at_trx_commit =1
sync_binlog=1
双1配置,数据库的安全性是最高的,不会丢事务。
binlog_checksum 为 {CRC32 or NONE}
写binlog时,会将内容生成校验位,之后存储在binlog中。
默认情况下,服务器记录事件的长度以及事件本身,并使用它来验证事件是否正确写入。 也可以通过设置
binlog_checksum系统变量来使服务器为事件写入校验和。
max_binlog_size binlog文件的最大值,默认和最大是1GB,并不能严格限定二进制文件的大小
max_binlog_cache_size 表示的是binlog 能够使用的最大cache 内存大小
当我们执行多语句事务的时候 所有session的使用的内存超过max_binlog_cache_size的值时
就会报错:“Multi-statement transaction required more than ‘max_binlog_cache_size’ bytes ofstorage”
binlog_cache_size binlog日志的缓存,默认4M,不宜过大

1.4 binlog格式

binlog有三种格式statement,row以及mixed,默认使用默认使用statement,建议使用row格式

参数值 描述
statement 语句级别,记录执行的DDL及DML语句
优点: 性能最优,日志量少
缺点 : 主从复制情况下可能会导致主备不一致
row 行级别,用行的形式记录执行的DDL及DML
优点:数据一致性高
缺点:性能略低于语句级别
mixed 混合级别,MySQL自动判断是记录行还是记录语句,官方推荐的模式

1.5 清理binlog

reset master; //删除master的binlog,线上不要使用
purge master logs before ‘2012-03-30 17:20:00’; //删除指定日期以前的日志索引中binlog日志文件
purge master logs to ‘binlog.000002’; //删除指定日志文件的日志索引中binlog日志文件

如果想直接删除binlog
rm binlog.000002
(不要删除当前正在使用的binlog,也就是编号最大的binlog)
[root@namenode mysql3306]# cat mysql-bin.index 不会自动更新
/home/mysql3306/mysql3306/mysql-bin.000001
/home/mysql3306/mysql3306/mysql-bin.000002
主从复制如果删掉了,容易从库报错(保证从库不再同步该binlog的数据)

1.6 binlog和redo的区别

  • redo 物理逻辑日志 物理:数据页 逻辑: 数据页的改变
    binlog 是逻辑日志 记录的SQL或者是行改变
  • redo是innodb存储引擎层
    binlog是server层
  • redo时时刻刻都在写入文件
    binlog是 提交事务的写入(具体写入策略跟具体参数有关)

二.MySQl binlog恢复

2.1 语句级别的恢复

show master status;
flush logs;
show master status;
create table t2(id int,name varchar(100));
insert into t2 values (1,'a');
insert into t2 values (2,'b');
insert into t2 values (3,'c');
flush logs;

执行记录

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000008 |      120 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.21 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000009 |      120 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> create table t2(id int,name varchar(100));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into t2 values (1,'a');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (2,'b');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (3,'c');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t2;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | b    |
|    3 | c    |
+------+------+
3 rows in set (0.00 sec)

mysql> 
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

通过mysqlbinlog查看 可以看到相关的sql语句都被打印出来

mysqlbinlog /u01/my3306/log/binlog/binlog.000009
[mysql@monitor binlog]$ mysqlbinlog /u01/my3306/log/binlog/binlog.000009
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200816 21:55:18 server id 1  end_log_pos 120 CRC32 0x28e9c60f  Start: binlog v 4, server v 5.6.37-log created 200816 21:55:18
BINLOG '
xjo5Xw8BAAAAdAAAAHgAAAAAAAQANS42LjM3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAQ/G
6Sg=
'/*!*/;
# at 120
#200816 21:55:41 server id 1  end_log_pos 235 CRC32 0x98ce9bc5  Query   thread_id=1     exec_time=0     error_code=0
use `test`/*!*/;
SET TIMESTAMP=1597586141/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table t2(id int,name varchar(100))
/*!*/;
# at 235
#200816 21:55:52 server id 1  end_log_pos 314 CRC32 0x3f15ad19  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586152/*!*/;
BEGIN
/*!*/;
# at 314
#200816 21:55:52 server id 1  end_log_pos 417 CRC32 0xfeaa465a  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586152/*!*/;
insert into t2 values (1,'a')
/*!*/;
# at 417
#200816 21:55:52 server id 1  end_log_pos 448 CRC32 0x55d2ec50  Xid = 45
COMMIT/*!*/;
# at 448
#200816 21:55:56 server id 1  end_log_pos 527 CRC32 0xe8ee6ecd  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586156/*!*/;
BEGIN
/*!*/;
# at 527
#200816 21:55:56 server id 1  end_log_pos 630 CRC32 0xf729214c  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586156/*!*/;
insert into t2 values (2,'b')
/*!*/;
# at 630
#200816 21:55:56 server id 1  end_log_pos 661 CRC32 0xa47e49c8  Xid = 46
COMMIT/*!*/;
# at 661
#200816 21:56:00 server id 1  end_log_pos 740 CRC32 0xac7fdd47  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586160/*!*/;
BEGIN
/*!*/;
# at 740
#200816 21:56:00 server id 1  end_log_pos 843 CRC32 0x7164118b  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586160/*!*/;
insert into t2 values (3,'c')
/*!*/;
# at 843
#200816 21:56:00 server id 1  end_log_pos 874 CRC32 0x777d6e18  Xid = 47
COMMIT/*!*/;
# at 874
#200816 21:56:12 server id 1  end_log_pos 918 CRC32 0x79221882  Rotate to binlog.000010  pos: 4
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

通过–start-position、–stop-position限定时间段

mysqlbinlog --start-position=120 --stop-position=235  /u01/my3306/log/binlog/binlog.000009

测试记录:

[mysql@monitor binlog]$ mysqlbinlog --start-position=120 --stop-position=235  /u01/my3306/log/binlog/binlog.000009
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200816 21:55:18 server id 1  end_log_pos 120 CRC32 0x28e9c60f  Start: binlog v 4, server v 5.6.37-log created 200816 21:55:18
BINLOG '
xjo5Xw8BAAAAdAAAAHgAAAAAAAQANS42LjM3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAQ/G
6Sg=
'/*!*/;
# at 120
#200816 21:55:41 server id 1  end_log_pos 235 CRC32 0x98ce9bc5  Query   thread_id=1     exec_time=0     error_code=0
use `test`/*!*/;
SET TIMESTAMP=1597586141/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table t2(id int,name varchar(100))
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[mysql@monitor binlog]$ 

通过–start-datetime、–stop-datetime参数来指定时间段

mysqlbinlog --start-datetime='2020-08-16 21:55:41' --stop-datetime='2020-08-16 21:55:56'  /u01/my3306/log/binlog/binlog.000009
[mysql@monitor binlog]$ mysqlbinlog --start-datetime='2020-08-16 21:55:41' --stop-datetime='2020-08-16 21:55:56'  /u01/my3306/log/binlog/binlog.000009
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200816 21:55:18 server id 1  end_log_pos 120 CRC32 0x28e9c60f  Start: binlog v 4, server v 5.6.37-log created 200816 21:55:18
BINLOG '
xjo5Xw8BAAAAdAAAAHgAAAAAAAQANS42LjM3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAQ/G
6Sg=
'/*!*/;
# at 120
#200816 21:55:41 server id 1  end_log_pos 235 CRC32 0x98ce9bc5  Query   thread_id=1     exec_time=0     error_code=0
use `test`/*!*/;
SET TIMESTAMP=1597586141/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table t2(id int,name varchar(100))
/*!*/;
# at 235
#200816 21:55:52 server id 1  end_log_pos 314 CRC32 0x3f15ad19  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586152/*!*/;
BEGIN
/*!*/;
# at 314
#200816 21:55:52 server id 1  end_log_pos 417 CRC32 0xfeaa465a  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597586152/*!*/;
insert into t2 values (1,'a')
/*!*/;
# at 417
#200816 21:55:52 server id 1  end_log_pos 448 CRC32 0x55d2ec50  Xid = 45
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

另外,还有–database=ops指定只恢复ops数据库

2.2 row格式的恢复

修改日志格式

mysql> show variables like '%binlog_format%';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

mysql> set binlog_format = row;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)
-- 这个参数要为on,不然sql语句会被注释
mysql> show variables like 'binlog_rows_query_log_events';
+------------------------------+-------+
| Variable_name                | Value |
+------------------------------+-------+
| binlog_rows_query_log_events | ON    |
+------------------------------+-------+
1 row in set (0.00 sec)

测试sql

flush logs;
show master status;
create table t3(id int,name varchar(100));
insert into t3 values (1,'aaa');
insert into t3 values (2,'bbb');
insert into t3 values (3,'ccc');
update t3 set name='ddd' where id = 2;
select * from t3;
flush logs;

测试语句执行记录

mysql> flush logs;
Query OK, 0 rows affected (0.21 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000011 |      120 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> create table t3(id int,name varchar(100));
Query OK, 0 rows affected (0.22 sec)

mysql> insert into t3 values (1,'aaa');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t3 values (2,'bbb');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t3 values (3,'ccc');
Query OK, 1 row affected (0.01 sec)

mysql> update t3 set name='ddd' where id = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t3;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    2 | ddd  |
|    3 | ccc  |
+------+------+
3 rows in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)

通过mysqlbinlog查看执行的日志

mysqlbinlog  --base64-output=DECODE-ROWS -v -v /u01/my3306/log/binlog/binlog.000011
[mysql@monitor binlog]$ mysqlbinlog  --base64-output=DECODE-ROWS -v -v /u01/my3306/log/binlog/binlog.000011
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200816 22:30:45 server id 1  end_log_pos 120 CRC32 0xd552697f  Start: binlog v 4, server v 5.6.37-log created 200816 22:30:45
# at 120
#200816 22:32:39 server id 1  end_log_pos 235 CRC32 0x8cc6dae3  Query   thread_id=1     exec_time=0     error_code=0
use `test`/*!*/;
SET TIMESTAMP=1597588359/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create table t3(id int,name varchar(100))
/*!*/;
# at 235
#200816 22:32:49 server id 1  end_log_pos 307 CRC32 0x6e7bdf26  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597588369/*!*/;
BEGIN
/*!*/;
# at 307
#200816 22:32:49 server id 1  end_log_pos 362 CRC32 0xcfd834d7  Rows_query
# insert into t3 values (1,'aaa')
# at 362
#200816 22:32:49 server id 1  end_log_pos 410 CRC32 0xe0be9170  Table_map: `test`.`t3` mapped to number 74
# at 410
#200816 22:32:49 server id 1  end_log_pos 455 CRC32 0x2f857fed  Write_rows: table id 74 flags: STMT_END_F
### INSERT INTO `test`.`t3`
### SET
###   @1=1 /* INT meta=0 nullable=1 is_null=0 */
###   @2='aaa' /* VARSTRING(300) meta=300 nullable=1 is_null=0 */
# at 455
#200816 22:32:49 server id 1  end_log_pos 486 CRC32 0xeb767c91  Xid = 56
COMMIT/*!*/;
# at 486
#200816 22:32:55 server id 1  end_log_pos 558 CRC32 0xc5a0a923  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597588375/*!*/;
BEGIN
/*!*/;
# at 558
#200816 22:32:55 server id 1  end_log_pos 613 CRC32 0x3c494320  Rows_query
# insert into t3 values (2,'bbb')
# at 613
#200816 22:32:55 server id 1  end_log_pos 661 CRC32 0x0767f99b  Table_map: `test`.`t3` mapped to number 74
# at 661
#200816 22:32:55 server id 1  end_log_pos 706 CRC32 0x0f7eb2a7  Write_rows: table id 74 flags: STMT_END_F
### INSERT INTO `test`.`t3`
### SET
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */
###   @2='bbb' /* VARSTRING(300) meta=300 nullable=1 is_null=0 */
# at 706
#200816 22:32:55 server id 1  end_log_pos 737 CRC32 0x4382e7c6  Xid = 57
COMMIT/*!*/;
# at 737
#200816 22:33:00 server id 1  end_log_pos 809 CRC32 0x1a477c11  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597588380/*!*/;
BEGIN
/*!*/;
# at 809
#200816 22:33:00 server id 1  end_log_pos 864 CRC32 0x4045e1c3  Rows_query
# insert into t3 values (3,'ccc')
# at 864
#200816 22:33:00 server id 1  end_log_pos 912 CRC32 0xfa37dcf1  Table_map: `test`.`t3` mapped to number 74
# at 912
#200816 22:33:00 server id 1  end_log_pos 957 CRC32 0x9e5fd360  Write_rows: table id 74 flags: STMT_END_F
### INSERT INTO `test`.`t3`
### SET
###   @1=3 /* INT meta=0 nullable=1 is_null=0 */
###   @2='ccc' /* VARSTRING(300) meta=300 nullable=1 is_null=0 */
# at 957
#200816 22:33:00 server id 1  end_log_pos 988 CRC32 0x7da1f606  Xid = 58
COMMIT/*!*/;
# at 988
#200816 22:35:03 server id 1  end_log_pos 1060 CRC32 0x1d91e750         Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1597588503/*!*/;
BEGIN
/*!*/;
# at 1060
#200816 22:35:03 server id 1  end_log_pos 1121 CRC32 0x99eddaa3         Rows_query
# update t3 set name='ddd' where id = 2
# at 1121
#200816 22:35:03 server id 1  end_log_pos 1169 CRC32 0x7581ae12         Table_map: `test`.`t3` mapped to number 74
# at 1169
#200816 22:35:03 server id 1  end_log_pos 1225 CRC32 0x8573abd1         Update_rows: table id 74 flags: STMT_END_F
### UPDATE `test`.`t3`
### WHERE
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */
###   @2='bbb' /* VARSTRING(300) meta=300 nullable=1 is_null=0 */
### SET
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */
###   @2='ddd' /* VARSTRING(300) meta=300 nullable=1 is_null=0 */
# at 1225
#200816 22:35:03 server id 1  end_log_pos 1256 CRC32 0x49e1eb6b         Xid = 59
COMMIT/*!*/;
# at 1256
#200816 22:35:19 server id 1  end_log_pos 1300 CRC32 0x1febd0af         Rotate to binlog.000012  pos: 4
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

测试一下恢复

-- drop表
drop table t3;
-- 通过mysqlbinlog进行恢复
mysqlbinlog  --base64-output=DECODE-ROWS -v -v /u01/my3306/log/binlog/binlog.000011 | mysql -uroot -pabc123 -v
-- 查看得知,建表语句是恢复了,但是dml没有恢复,应该是sql语句被注释了的原因
[mysql@monitor binlog]$ mysqlbinlog  --base64-output=DECODE-ROWS -v -v /u01/my3306/log/binlog/binlog.000011 | mysql -uroot -pabc123 -v
Warning: Using a password on the command line interface can be insecure.
--------------
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/
--------------

--------------
/*!40019 SET @@session.max_insert_delayed_threads=0*/
--------------

--------------
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/
--------------

--------------
SET TIMESTAMP=1597588359
--------------

--------------
SET @@session.pseudo_thread_id=1
--------------

--------------
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1
--------------

--------------
SET @@session.sql_mode=1075838976
--------------

--------------
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1
--------------

--------------
/*!*/
--------------

--------------
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=33
--------------

--------------
SET @@session.lc_time_names=0
--------------

--------------
SET @@session.collation_database=DEFAULT
--------------

--------------
create table t3(id int,name varchar(100))
--------------

--------------
SET TIMESTAMP=1597588369
--------------

--------------
BEGIN
--------------

--------------
COMMIT
--------------

--------------
SET TIMESTAMP=1597588375
--------------

--------------
BEGIN
--------------

--------------
COMMIT
--------------

--------------
SET TIMESTAMP=1597588380
--------------

--------------
BEGIN
--------------

--------------
COMMIT
--------------

--------------
SET TIMESTAMP=1597588503
--------------

--------------
BEGIN
--------------

--------------
COMMIT
--------------

--------------
ROLLBACK
--------------

--------------
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/
--------------

--------------
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/
--------------

其实对于恢复而言,直接执行即可,不用查看到具体的sql语句

-- mysqlbinlog后面可以带多个binlog文件
mysqlbinlog /u01/my3306/log/binlog/binlog.000011 | mysql -uroot -p
[mysql@monitor binlog]$ mysqlbinlog /u01/my3306/log/binlog/binlog.000011 | mysql -uroot -p
Enter password: 
[mysql@monitor binlog]$ 

mysql> select * from t3;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    2 | ddd  |
|    3 | ccc  |
+------+------+
3 rows in set (0.00 sec)

参考文献:

1.http://blog.itpub.net/15412087/viewspace-2152202/
2.http://blog.itpub.net/28413242/viewspace-2650501/
3.https://www.cnblogs.com/Presley-lpc/p/9619571.html
4.https://www.cnblogs.com/kerrycode/p/9315949.html

你可能感兴趣的:(MySQL运维,数据库,mysql)