参数binlog_rows_query_log_events和binlog_row_image 与用 binlog恢复数据
binlog_rows_query_log_events和binlog_row_image 參數作用
(mysql 5.6.2 引入)
當binlog使用row格式時
binlog_rows_query_log_events =1
在row模式下開啟該參數,將把sql語句打印到binlog日志里面.默認是0(off);
雖然將語句放入了binlog,但不會執行這個sql,就相當于注釋一樣.但對于dba來說,在查看binlog的時候,很有用處.
binlog_row_image=‘minimal’
默認為full,在binlog為row格式下,full將記錄update前后所有字段的值,
minimal時,只記錄更改字段的值和where字段的值,
noblob時,記錄除了blob和text的所有字段的值,如果update的blob或text字段,也只記錄該字段更改后的值,更改前的不記錄;
row格式下的binlog增長速度太快,對存儲空間,主從傳輸都是一個不小的壓力.不管數據是否有改動都會記錄.
binlog_row_image=‘minimal’ 時大大減少了binlog記錄的數據.
節省了硬盤空間開銷,減小I/O,減少了主從傳輸壓力;
有利必有弊。在 binlog_row_image=‘minimal’ 時,因為binlog 中不再記錄全字段修改/刪除前后的值,如果需要用binlog
來做數據恢復,那就行不通了。
下面示例一下把誤刪除的數據,怎樣使用binlog 日志進行恢復回來。
mysql> show variables like 'binlog_row%'; +------------------------------+-------+ | Variable_name | Value | +------------------------------+-------+ | binlog_row_image | FULL | | binlog_rows_query_log_events | ON | +------------------------------+-------+ 2 rows in set (0.00 sec)#添加表數據 mysql> create tbl1(id int not null auto_increment,-> name varchar(10),-> create_time timestamp default CURRENT_TIMESTAMP,-> update_time timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,-> primary key (id)); mysql> insert into tbl1(name) values('test1'); Query OK, 1 row affected (0.02 sec) mysql> insert into tbl1(name) values('test2'); Query OK, 1 row affected (0.00 sec)mysql> insert into tbl1(name) values('test3'); Query OK, 1 row affected (0.00 sec)mysql> delete from tbl1; Query OK, 3 rows affected (0.09 sec)我們來查看一下binlog 日志
[root@m10 mysql]# mysqlbinlog mysql-bin.000011 -vv --base64-output=DECODE-ROWS >/tmp/mysql-bin.11.log # at 604 #190529 9:43:16 server id 101 end_log_pos 666 CRC32 0x29a3aebd Rows_query # insert into tbl1(name) values('test1') #只有在binlog_rows_query_log_events =1 時才會打印此內容 # at 666 #190529 9:43:16 server id 101 end_log_pos 720 CRC32 0x7b1a3e0e Table_map: `test`.`tbl1` mapped to number 70 # at 720 #190529 9:43:16 server id 101 end_log_pos 774 CRC32 0x8f165079 Write_rows: table id 70 flags: STMT_END_F ### INSERT INTO `test`.`tbl1` ### SET ### @1=1 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test1' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094196 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094196 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ # at 805 #190529 9:51:37 server id 101 end_log_pos 853 CRC32 0xeb1fde0c GTID [commit=yes] SET @@SESSION.GTID_NEXT= '450009fd-3332-11e8-ab5d-005056257272:3'/*!*/; # at 853 #190529 9:51:37 server id 101 end_log_pos 925 CRC32 0x4b540350 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1559094697/*!*/; BEGIN /*!*/; # at 925 #190529 9:51:37 server id 101 end_log_pos 987 CRC32 0xe0c0cdd9 Rows_query # insert into tbl1(name) values('test2') # at 987 #190529 9:51:37 server id 101 end_log_pos 1041 CRC32 0x7e7cb17f Table_map: `test`.`tbl1` mapped to number 70 # at 1041 #190529 9:51:37 server id 101 end_log_pos 1095 CRC32 0x107e7821 Write_rows: table id 70 flags: STMT_END_F ### INSERT INTO `test`.`tbl1` ### SET ### @1=2 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test2' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094697 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094697 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ # at 1095 #190529 9:51:37 server id 101 end_log_pos 1126 CRC32 0x27ccc8e1 Xid = 33 COMMIT/*!*/; # at 1126 #190529 9:51:39 server id 101 end_log_pos 1174 CRC32 0x02a87edc GTID [commit=yes] SET @@SESSION.GTID_NEXT= '450009fd-3332-11e8-ab5d-005056257272:4'/*!*/; # at 1174 #190529 9:51:39 server id 101 end_log_pos 1246 CRC32 0x0e82536b Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1559094699/*!*/; BEGIN /*!*/; # at 1246 #190529 9:51:39 server id 101 end_log_pos 1308 CRC32 0xa65a6deb Rows_query # insert into tbl1(name) values('test3') # at 1308 #190529 9:51:39 server id 101 end_log_pos 1362 CRC32 0x31fe1b9c Table_map: `test`.`tbl1` mapped to number 70 # at 1362 #190529 9:51:39 server id 101 end_log_pos 1416 CRC32 0x26ee5087 Write_rows: table id 70 flags: STMT_END_F ### INSERT INTO `test`.`tbl1` ### SET ### @1=3 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test3' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094699 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094699 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ # at 1416 #190529 9:51:39 server id 101 end_log_pos 1447 CRC32 0xf608d960 Xid = 34 COMMIT/*!*/; # at 1447 #190529 9:51:49 server id 101 end_log_pos 1495 CRC32 0x4a4d95a7 GTID [commit=yes] SET @@SESSION.GTID_NEXT= '450009fd-3332-11e8-ab5d-005056257272:5'/*!*/; # at 1495 #190529 9:51:49 server id 101 end_log_pos 1567 CRC32 0x3d5788fb Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1559094709/*!*/; BEGIN /*!*/; # at 1567 #190529 9:51:49 server id 101 end_log_pos 1607 CRC32 0x844332be Rows_query # delete from tbl1 # at 1607 #190529 9:51:49 server id 101 end_log_pos 1661 CRC32 0x03342d4e Table_map: `test`.`tbl1` mapped to number 70 # at 1661 #190529 9:51:49 server id 101 end_log_pos 1753 CRC32 0x3fcb94e0 Delete_rows: table id 70 flags: STMT_END_F ### DELETE FROM `test`.`tbl1` ### WHERE ### @1=1 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test1' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094196 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094196 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### DELETE FROM `test`.`tbl1` ### WHERE ### @1=2 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test2' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094697 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094697 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### DELETE FROM `test`.`tbl1` ### WHERE ### @1=3 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test3' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094699 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094699 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ # at 1753 #190529 9:51:49 server id 101 end_log_pos 1784 CRC32 0xd658ea43 Xid = 35 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog *//*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;把刪除命令的 binlog 讀出來后面進行處理
[root@m10 mysql]# cat /tmp/mysql-bin.11.log |sed -n '/### DELETE FROM `test`.`tbl1`/,/COMMIT/p' >/tmp/mysql-bin.del.log### DELETE FROM `test`.`tbl1` ### WHERE ### @1=1 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test1' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094196 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094196 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### DELETE FROM `test`.`tbl1` ### WHERE ### @1=2 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test2' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094697 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094697 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### DELETE FROM `test`.`tbl1` ### WHERE ### @1=3 /* INT meta=0 nullable=0 is_null=0 */ ### @2='test3' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ ### @3=1559094699 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ ### @4=1559094699 /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */ # at 1753 #190529 9:51:49 server id 101 end_log_pos 1784 CRC32 0xd658ea43 Xid = 35 COMMIT/*!*/;命令如下:
cat /tmp/mysql-bin.del.log |sed -n '/###/p'| sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;'|sed -r 's/(@4.*),/\1;/g'|sed 's/@[1-2]=//g' |sed 's/@[3-4]=/=/g' |sed 's/=[0-9]\+/from_unixtime(&)/g' >/tmp/insert.sql#經過多次替換:
#insert.sql 內容如下:
INSERT INTO `test`.`tbl1` SELECT1 ,'test1' ,from_unixtime(=1559094196) ,from_unixtime(=1559094196) ; INSERT INTO `test`.`tbl1` SELECT2 ,'test2' ,from_unixtime(=1559094697) ,from_unixtime(=1559094697) ; INSERT INTO `test`.`tbl1` SELECT3 ,'test3' ,from_unixtime(=1559094699) ,from_unixtime(=1559094699) ;后面可以用 insert.sql 恢復 數據。
source /tmp/insert.sql
mysql> mysql> source /tmp/insert.sql; Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0mysql> select * from tbl1; +----+-------+---------------------+---------------------+ | id | name | create_time | update_time | +----+-------+---------------------+---------------------+ | 1 | test1 | 2019-05-29 14:52:36 | 2019-05-29 14:52:36 | | 2 | test2 | 2019-05-29 14:52:36 | 2019-05-29 14:52:36 | | 3 | test3 | 2019-05-29 14:52:36 | 2019-05-29 14:52:36 | +----+-------+---------------------+---------------------+ 3 rows in set (0.01 sec)總結
以上是生活随笔為你收集整理的参数binlog_rows_query_log_events和binlog_row_image 与用 binlog恢复数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 访问 zookeeper
- 下一篇: elasticsearch7.9.2 安