binlog_format不一樣模式下,對mysqlbinlog恢復的影響

 
binlog_format='mixed'
(root@g1-db-test-v01:3306)[(none)]>use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
(root@g1-db-test-v01:3306)[test]>update db1.t2 set name='haha' where id=105;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

(root@g1-db-test-v01:3306)[test]>use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
(root@g1-db-test-v01:3306)[db1]>delete from test.t1 where id=11;
Query OK, 1 row affected (0.02 sec)
 
假如使用mysqlbinlog恢復db1的數據,命令加上參加 -d :
mysqlbinlog -d db1 test-mysql-bin.000115 > 115d.sql
 
/*!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
#170809 15:38:35 server id 1882073306  end_log_pos 120 CRC32 0x9c11ffa3         Start: binlog v 4, server v 5.6.23-72.1-log created 170809 15:38:35
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
+7uKWQ/aKC5wdAAAAHgAAAABAAQANS42LjIzLTcyLjEtbG9nAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAaP/
EZw=
'/*!*/;
# at 120
#170809 15:39:20 server id 1882073306  end_log_pos 198 CRC32 0x58ea2e3a         Query   thread_id=75    exec_time=0     error_code=0
SET TIMESTAMP=1502264360/*!*/;
SET @@session.pseudo_thread_id=75/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 198
# at 313
#170809 15:39:20 server id 1882073306  end_log_pos 344 CRC32 0xb2671190         Xid = 32389
COMMIT/*!*/;
# at 344
#170809 15:41:05 server id 1882073306  end_log_pos 422 CRC32 0x4befc6fe         Query   thread_id=75    exec_time=0     error_code=0
SET TIMESTAMP=1502264465/*!*/;
BEGIN
/*!*/;
# at 422
#170809 15:41:05 server id 1882073306  end_log_pos 526 CRC32 0x2b0ef4d4         Query   thread_id=75    exec_time=0     error_code=0
use `db1`/*!*/;
SET TIMESTAMP=1502264465/*!*/;
delete from test.t1 where id=11
/*!*/;
# at 526
#170809 15:41:05 server id 1882073306  end_log_pos 557 CRC32 0xb34d26a0         Xid = 32473
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
(END) 

上面的binlog只記錄了 delete from test.t1 where id=11mysql

 
描述這樣一種場景,上述案例本想恢復update那一條數據,結果只能恢復delete那一條。
而delete的倒是test庫的表,若是在測試環境進行恢復,因只需恢復db1,沒有建立test庫,則恢復時會報錯,恢復終止。
 
也就是說,在binlog_format='statement/mix'時,若是mysqlbinlog 不加 --database(-d)參數,解析的binlog裏記錄全部變動sql。但若是增長了 --database db1,則只記錄use db1後面的變動sql
 
這樣有一個問題,若是db1裏的一張表 t1被刪除了,咱們想恢復。按照通常方法,是先使用最近備份恢復db1全部數據(由於按庫備份),而後使用mysqlbinlog恢復db1的數據到drop t1前一刻,此時使用mysqlbinlog 若是加了 --databae=db1,那在有跨庫操做的狀況下,會遺漏部分sql; 而若是不加 --database=db1,那就更無法恢復了,由於以前只使用了db1的備份先恢復的,也就是隻恢復了db1。綜上,不加--database的話,就要將所庫都先恢復一遍,而後使用mysqlbinlog不加參數繼續恢復。
 
當設置row模式時,則不存在這種問題。
binlog_format='row'
 
(root@g1-db-test-v01:3306)[(none)]>use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
(root@g1-db-test-v01:3306)[test]>update db1.t2 set name='hehe' where id=105;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

(root@g1-db-test-v01:3306)[test]>use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
(root@g1-db-test-v01:3306)[db1]>delete from test.t1 where id=10;
Query OK, 1 row affected (0.02 sec)
 
mysqlbinlog --base64-output=decode-rows -vv -d db1 test-mysql-bin.000116 > 116d.sql
 
/*!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
#170809 16:28:12 server id 1882073306  end_log_pos 120 CRC32 0x6c11777a         Start: binlog v 4, server v 5.6.23-72.1-log created 170809 16:28:12
# Warning: this binlog is either in use or was not closed properly.
# at 120
#170809 16:28:51 server id 1882073306  end_log_pos 192 CRC32 0x256a9f3c         Query   thread_id=78    exec_time=0     error_code=0
SET TIMESTAMP=1502267331/*!*/;
SET @@session.pseudo_thread_id=78/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 192
#170809 16:28:51 server id 1882073306  end_log_pos 246 CRC32 0x285d8d5d         Table_map: `db1`.`t2` mapped to number 395
# at 246
#170809 16:28:51 server id 1882073306  end_log_pos 366 CRC32 0xb84daa2e         Update_rows: table id 395 flags: STMT_END_F
### UPDATE `db1`.`t2`
### WHERE
###   @1=105 /* INT meta=0 nullable=0 is_null=0 */
###   @2='haha' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
###   @3='57747ab889255af96b48d65e505382' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
###   @4='' /* VARSTRING(72) meta=72 nullable=0 is_null=0 */
###   @5=NULL /* VARSTRING(72) meta=0 nullable=1 is_null=1 */
### SET
###   @1=105 /* INT meta=0 nullable=0 is_null=0 */
###   @2='hehe' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
###   @3='57747ab889255af96b48d65e505382' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
###   @4='' /* VARSTRING(72) meta=72 nullable=0 is_null=0 */
###   @5=NULL /* VARSTRING(72) meta=0 nullable=1 is_null=1 */
# at 366
#170809 16:28:51 server id 1882073306  end_log_pos 397 CRC32 0x74a5b1df         Xid = 32558
COMMIT/*!*/;
# at 397
#170809 16:29:07 server id 1882073306  end_log_pos 468 CRC32 0x66079b66         Query   thread_id=78    exec_time=0     error_code=0
SET TIMESTAMP=1502267347/*!*/;
BEGIN
/*!*/;
# at 468
# at 519
# at 567
#170809 16:29:07 server id 1882073306  end_log_pos 598 CRC32 0xb3ffd246         Xid = 32583
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
(END) 
 

 

相關文章
相關標籤/搜索