MySQL redo log 與 binlog 的區別


MySQL redo log 與 binlog 的區別

1. 什么是redo log?

redo log又稱重做日志文件,用於記錄事務操作的變化,記錄的是數據修改之后的值,不管事務是否提交都會記錄下來。在實例和介質失敗(media failure)時,redo log文件就能派上用場,如數據庫掉電,InnoDB存儲引擎會使用redo log恢復到掉電前的時刻,以此來保證數據的完整性。

1.1 redo日志文件名

每個InnoDB存儲引擎至少有1個重做日志文件組(group),每個文件組至少有2個重做日志文件,如默認的ib_logfile0ib_logfile1

1.2 影響redo log參數

  • innodb_log_file_size:指定每個redo日志大小,默認值48MB

  • innodb_log_files_in_group:指定日志文件組中redo日志文件數量,默認為2

  • innodb_log_group_home_dir:指定日志文件組所在路勁,默認值./,指mysql的數據目錄datadir

  • innodb_mirrored_log_groups:指定日志鏡像文件組的數量,默認為1,此功能屬於未實現的功能,在5.6版本中廢棄,在5.7版本中刪除了。

以下顯示了一個關於redo日志組的配置:

mysql> show variables like 'innodb%log%';
+----------------------------------+------------+ | Variable_name | Value | +----------------------------------+------------+ ... | innodb_log_file_size | 2147483648 | | innodb_log_files_in_group | 2 | | innodb_log_group_home_dir | ./ | ... +----------------------------------+------------+ 15 rows in set (0.00 sec)

 

1.3 redo log大小怎么設置?

redo log文件的大小設置對於InnoDB存儲引擎的性能有着非常大的影響。

  • 設置的太大
    設置很大以后減少了checkpoint,並且由於redo log是順序I/O,大大提高了I/O性能。但是如果數據庫意外出現了問題,比如意外宕機,那么需要重放日志並且恢復已經提交的事務,如果日志很大,那么將會導致恢復時間很長。甚至到我們不能接受的程度。

  • 設置的太小
    當一個日志文件寫滿后,innodb會自動切換到另外一個日志文件,而且會觸發數據庫的檢查點(checkpoint),這會導致innodb緩存臟頁的小批量刷新,會明顯降低innodb的性能。

  • 怎么設置?
    參考官方文檔'Optimizing InnoDB Redo Logging'章節

    • 把重做日志文件設置很大,甚至與緩沖池一樣大。當InnoDB將重做日志文件寫滿時,它會觸發數據庫的檢查點,把緩沖池的臟數據寫入磁盤。小的重做日志文件會導致許多不必要的磁盤寫入。雖然在以前版本中,大的重做日志文件導致冗長的恢復時間,但現在恢復速度更快,可以放心地使用大型重做日志文件。

    • 考慮增加日志緩沖區的大小。 大型日志緩沖區可以在事務提交之前運行大型事務,而無需將日志寫入磁盤。 因此,如果您有更新,插入或刪除許多行的事務,則使日志緩沖區更大可以節省磁盤I/O. 使用innodb_log_buffer_size配置選項配置日志緩沖區大小。

    • 設置innodb_log_write_ahead_size參數,表示redo log寫前的塊大小。InnoDB以512字節一個block的方式對齊寫入ib_logfile文件,但文件系統一般以4096字節為一個block單位。如果即將寫入的日志文件塊不在OS Cache時,就需要將對應的4096字節的block讀入內存,修改其中的512字節,然后再把該block寫回磁盤。當 當前寫入文件的偏移量不能整除該值時,則補0,多寫一部分數據。這樣當寫入的數據是以磁盤block size對齊時,就可以直接write磁盤,而無需read-modify-write這三步了。

2. 什么是binlog 

binlog記錄了對MySQL數據庫執行更改的所有操作,但是不包括SELECT和SHOW這類操作,因為這類操作對數據本身並沒有修改。然后,若操作本身並沒有導致數據庫發生變化,那么該操作也會寫入二進制日志。例如:

root@localhost [(none)] 08:30:14>set binlog_format = 'STATEMENT';

root@localhost [(none)] 08:30:26>use test;
Database changed
root@localhost [test] 08:30:33>select * from account;
+----------+---------+
| acct_num | amount  |
+----------+---------+
|      138 |   14.98 |
|      141 | 1937.50 |
|       97 | -100.00 |
+----------+---------+
3 rows in set (0.00 sec)


root@localhost [test] 08:30:53>show master status;
+----------------------+----------+--------------+------------------+--------------------------------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                          |
+----------------------+----------+--------------+------------------+--------------------------------------------+
| my3306_binlog.000052 |      471 |              |                  | e4382832-949d-11e8-97ba-080027793430:1-205 |
+----------------------+----------+--------------+------------------+--------------------------------------------+


root@localhost [test] 08:31:04>update account set acct_num=139 where amount=14;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0  Changed: 0  Warnings: 0

root@localhost [test] 08:31:35>show binlog events in 'my3306_binlog.000052';
+----------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
| Log_name             | Pos | Event_type     | Server_id | End_log_pos | Info                                                                |
+----------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
| my3306_binlog.000052 |   4 | Format_desc    |   1003306 |         123 | Server ver: 5.7.23-log, Binlog ver: 4                               |
| my3306_binlog.000052 | 123 | Previous_gtids |   1003306 |         194 | e4382832-949d-11e8-97ba-080027793430:1-204                          |
| my3306_binlog.000052 | 194 | Gtid           |   1003306 |         259 | SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:205' |
| my3306_binlog.000052 | 259 | Query          |   1003306 |         331 | BEGIN                                                               |
| my3306_binlog.000052 | 331 | Table_map      |   1003306 |         384 | table_id: 108 (test.account)                                        |
| my3306_binlog.000052 | 384 | Update_rows    |   1003306 |         440 | table_id: 108 flags: STMT_END_F                                     |
| my3306_binlog.000052 | 440 | Xid            |   1003306 |         471 | COMMIT /* xid=14 */                                                 |
| my3306_binlog.000052 | 471 | Gtid           |   1003306 |         536 | SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:206' |
| my3306_binlog.000052 | 536 | Query          |   1003306 |         615 | BEGIN                                                               |
| my3306_binlog.000052 | 615 | Query          |   1003306 |         736 | use `test`; update account set acct_num=139 where amount=14         |
| my3306_binlog.000052 | 736 | Query          |   1003306 |         816 | COMMIT                                                              |
+----------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
11 rows in set (0.01 sec)

 

從上述例子中可以看到,MySQL數據庫首先進行update操作,從返回的結果看到Changed為0,這意味着該操作並沒有導致數據庫的變化。但是通過show binlog events in '...'可以看出在二進制日志中的確進行了記錄。

如果想記錄SELECT和SHOW操作,那只能使用查詢日志--general_log[={0|1}](1為啟用)

2.1 binlog文件名

通過配置參數--log-bin[=name]可以啟動二進制日志。如果不指定那么,則默認binlog日志文件名為主機名,后綴名為binlog的序列號,默認路勁為數據目錄(datadir).你也可以指定絕對路徑,如:

# cat /etc/my.cnf|grep log-bin
log-bin = /data/mysql/mysql3306/logs/my3306_binlog

# cd /data/mysql/mysql3306/logs
# ls -l
total 60
-rw-r----- 1 mysql mysql   194 Aug 15 10:04 my3306_binlog.000045
-rw-r----- 1 mysql mysql  1552 Aug 16 10:01 my3306_binlog.000046
-rw-r----- 1 mysql mysql  2953 Aug 17 09:56 my3306_binlog.000047
-rw-r----- 1 mysql mysql  1239 Aug 20 10:29 my3306_binlog.000048
-rw-r----- 1 mysql mysql   217 Aug 20 10:29 my3306_binlog.000049
-rw-r----- 1 mysql mysql 19567 Aug 21 10:24 my3306_binlog.000050
-rw-r----- 1 mysql mysql   194 Aug 22 08:01 my3306_binlog.000051
-rw-r----- 1 mysql mysql   816 Aug 22 08:31 my3306_binlog.000052
-rw-r----- 1 mysql mysql   384 Aug 22 08:01 my3306_binlog.index

 

2.2 影響binlog的參數

  • max_binlog_size:指定單個binlog文件最大值。默認值為1g,最大值1g,如果超過該值,則產生新的binlog文件,后綴名+1,並記錄到.index文件。

  • binlog_cache_size:使用事務表存儲引擎(如innodb存儲引擎)時,所有未提交的binlog日志會被記錄到一個緩存中去,等事務提交時再將緩存中的binlog寫入到binlog文件中。緩存的大小由binlog_cache_size決定,默認大小為32K。

    binlog_cache_size是基於session的,也就是說,當一個線程開始一個事務時,MySQL會自動分配一個大小為binlog_cache_size的緩存,因此該值的設置需要非常小心,不能設置過大。
    當一個事務的記錄大於設定的binlog_cache_size時,MySQL會把緩存中的日志寫入一個臨時文件中,因此該值又不能設的太小。

    那怎么設置呢?


    通過show global status命令查看binlog_cache_use,binlog_cache_disk_use的狀態,以判斷當前binlog_cache_size設置是否合適。 binlog_cache_use:記錄了使用緩存寫binlog次數 binlog_cache_disk_use:記錄了使用臨時文件寫binlog次數。 示例: root
    @localhost [(none)] 09:55:48>show variables like 'binlog_cache_size'; +-------------------+---------+ | Variable_name | Value | +-------------------+---------+ | binlog_cache_size | 32768 | +-------------------+---------+ 1 row in set (0.00 sec) root@localhost [(none)] 09:53:26>show global status like 'binlog_cache%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 33553 | +-----------------------+-------+ 2 rows in set (0.00 sec) 使用緩存次數為33553,臨時文件使用次數為0。說明32KB的緩存大小對於當前MySQL數據庫是夠用的。
  • max_binlog_cache_size:如果事務需要的內存超過很多字節,則服務器會生成多於“max_binlog_cache_size”字節的存儲錯誤所需的並發事務。 最小值為4096字節,最大可能值為16EB(exabytes)。 建議的最大值為4GB; 這是因為MySQL目前無法使用大於4GB的二進制日志位置。

  • expire_logs_days:表示binlog文件自動刪除N天前的文件。默認值為0,表示不自動刪除,最大值99.要手動刪除binlog文件,可以使用purge binary logs語句。例如:

    PURGE { BINARY | MASTER } LOGS
      { TO 'log_name' | BEFORE datetime_expr }
    
    PURGE BINARY LOGS TO 'mysql-bin.010';
    PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';
    PURGE BINARY LOGS BEFORE now() - interval 3 days;

 

  • binlog_rows_query_log_events:默認為不啟用,啟用binlog_rows_query_log_events時,會在binlog日志中記錄原始SQL語句。
    root@localhost [test] 08:07:52>show binlog events in 'my3306_binlog.000056';
    +----------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
    | Log_name             | Pos | Event_type     | Server_id | End_log_pos | Info                                                                |
    +----------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+
    | my3306_binlog.000056 |   4 | Format_desc    |   1003306 |         123 | Server ver: 5.7.23-log, Binlog ver: 4                               |
    | my3306_binlog.000056 | 123 | Previous_gtids |   1003306 |         194 | e4382832-949d-11e8-97ba-080027793430:1-206                          |
    | my3306_binlog.000056 | 194 | Gtid           |   1003306 |         259 | SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:207' |
    | my3306_binlog.000056 | 259 | Query          |   1003306 |         331 | BEGIN                                                               |
    | my3306_binlog.000056 | 331 | Table_map      |   1003306 |         375 | table_id: 108 (test.t)                                              |
    | my3306_binlog.000056 | 375 | Update_rows    |   1003306 |         421 | table_id: 108 flags: STMT_END_F                                     |
    | my3306_binlog.000056 | 421 | Xid            |   1003306 |         452 | COMMIT /* xid=16 */                                                 |
    | my3306_binlog.000056 | 452 | Gtid           |   1003306 |         517 | SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:208' |
    | my3306_binlog.000056 | 517 | Query          |   1003306 |         589 | BEGIN                                                               |
    | my3306_binlog.000056 | 589 | Table_map      |   1003306 |         633 | table_id: 108 (test.t)                                              |
    | my3306_binlog.000056 | 633 | Write_rows     |   1003306 |         673 | table_id: 108 flags: STMT_END_F                                     |
    | my3306_binlog.000056 | 673 | Xid            |   1003306 |         704 | COMMIT /* xid=18 */                                                 |
    | my3306_binlog.000056 | 704 | Gtid           |   1003306 |         769 | SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:209' |
    | my3306_binlog.000056 | 769 | Query          |   1003306 |         841 | BEGIN                                                               |
    | my3306_binlog.000056 | 841 | Rows_query | 1003306 | 887 | # insert into t select 3                                         |
    | my3306_binlog.000056 | 887 | Table_map      |   1003306 |         931 | table_id: 108 (test.t)                                              |
    | my3306_binlog.000056 | 931 | Write_rows     |   1003306 |         971 | table_id: 108 flags: STMT_END_F                                     |
    | my3306_binlog.000056 | 971 | Xid            |   1003306 |        1002 | COMMIT /* xid=24 */                                                 |
    +----------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------+

    #黃顏色標記的就是,開啟binlog_rows_query_log_events選項時,顯示的原始SQL語句。

 

  • sync_binlog:sync_binlog=[N]表示沒寫緩沖N次就同步到磁盤,如果將N設為1,即sync_binlog表示采用同步寫磁盤的方式來寫二進制日志,在MySQL5.7.7后,默認為1。會對數據庫的IO系統帶來一定影響,但可以得到最大的高可用行。

  • binlog_checksum:該參數目的就是寫入binlog進行校驗,有兩個值[crc32|none],默認為crc32

  • binlog-do-db:表示需要寫入日志的數據庫,默認為空,表示同步所有庫

  • binlog-ignore-db:表示忽略寫入日志的數據庫,默認為空,表示同步所有庫

  • log-slave-update:表示從master端取得並執行的binlog,寫入自己的binlog文件中,一般應用在master=>slave=>slave架構

  • binlog_format:記錄binlog的格式。[statement,row,mixed],在MySQL5.7.7之后,默認為row。

    存儲引起對binlog格式的支持情況:

2.3 查看binlog

使用mysqlbinlog程序進行查看,例如:

[root@mysqldb1 10:58:18 /data/mysql/mysql3306/logs]
# mysqlbinlog -v --base64-output=decode-rows my3306_binlog.000052|more

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180822  8:01:00 server id 1003306  end_log_pos 123 CRC32 0xcbe20047     Start: binlog v 4, server v 5.7.23-log created 180822  8:01:00 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 123
#180822  8:01:00 server id 1003306  end_log_pos 194 CRC32 0xb1bda518     Previous-GTIDs
# e4382832-949d-11e8-97ba-080027793430:1-204
# at 194
#180822  8:10:59 server id 1003306  end_log_pos 259 CRC32 0xeced9ada     GTID    last_committed=0    sequence_number=1    rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:205'/*!*/;
# at 259
#180822  8:10:59 server id 1003306  end_log_pos 331 CRC32 0x6da7802a     Query    thread_id=2    exec_time=0    error_code=0
SET TIMESTAMP=1534896659/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
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=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 331
#180822  8:10:59 server id 1003306  end_log_pos 384 CRC32 0xf239dd79     Table_map: `test`.`account` mapped to number 108
# at 384
#180822  8:10:59 server id 1003306  end_log_pos 440 CRC32 0xef6460fe     Update_rows: table id 108 flags: STMT_END_F
### UPDATE `test`.`account`
### WHERE
###   @1=137
###   @2=14.98
### SET
###   @1=138
###   @2=14.98
# at 440
#180822  8:10:59 server id 1003306  end_log_pos 471 CRC32 0x360f05d0     Xid = 14
COMMIT/*!*/;
# at 471
#180822  8:31:35 server id 1003306  end_log_pos 536 CRC32 0x662c8f17     GTID    last_committed=1    sequence_number=2    rbr_only=no
SET @@SESSION.GTID_NEXT= 'e4382832-949d-11e8-97ba-080027793430:206'/*!*/;
# at 536
#180822  8:31:35 server id 1003306  end_log_pos 615 CRC32 0xa728a60a     Query    thread_id=3    exec_time=0    error_code=0
SET TIMESTAMP=1534897895/*!*/;
BEGIN
/*!*/;
# at 615
#180822  8:31:35 server id 1003306  end_log_pos 736 CRC32 0x7513aa73     Query    thread_id=3    exec_time=0    error_code=0
use `test`/*!*/;
SET TIMESTAMP=1534897895/*!*/;
update account set acct_num=139 where amount=14
/*!*/;
# at 736
#180822  8:31:35 server id 1003306  end_log_pos 816 CRC32 0x1cd7f41c     Query    thread_id=3    exec_time=0    error_code=0
SET TIMESTAMP=1534897895/*!*/;
COMMIT
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

 

3. redo log與binlog的區別

第一:redo log是在InnoDB存儲引擎層產生,而binlog是MySQL數據庫的上層產生的,並且二進制日志不僅僅針對INNODB存儲引擎,MySQL數據庫中的任何存儲引擎對於數據庫的更改都會產生二進制日志。

第二:兩種日志記錄的內容形式不同。MySQL的binlog是邏輯日志,其記錄是對應的SQL語句。而innodb存儲引擎層面的重做日志是物理日志。

第三:兩種日志與記錄寫入磁盤的時間點不同,二進制日志只在事務提交完成后進行一次寫入。而innodb存儲引擎的重做日志在事務進行中不斷地被寫入,並日志不是隨事務提交的順序進行寫入的。

二進制日志僅在事務提交時記錄,並且對於每一個事務,僅在事務提交時記錄,並且對於每一個事務,僅包含對應事務的一個日志。而對於innodb存儲引擎的重做日志,由於其記錄是物理操作日志,因此每個事務對應多個日志條目,並且事務的重做日志寫入是並發的,並非在事務提交時寫入,其在文件中記錄的順序並非是事務開始的順序。

第四:binlog不是循環使用,在寫滿或者重啟之后,會生成新的binlog文件,redo log是循環使用。

第五:binlog可以作為恢復數據使用,主從復制搭建,redo log作為異常宕機或者介質故障后的數據恢復使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM