MySQL主庫(kù)binlog(master-log)與從庫(kù)relay-log關(guān)系代碼詳解
主庫(kù)binlog:
# at 2420 #170809 17:16:20 server id 1882073306 end_log_pos 2451 CRC32 0x58f2db87 Xid = 32880 COMMIT/*!*/; # at 2451 #170814 11:07:18 server id 1882073306 end_log_pos 2528 CRC32 0x40774a4b Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680038/*!*/; BEGIN /*!*/; # at 2528 # at 2560 #170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b Intvar SET INSERT_ID=107/*!*/; #170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8 Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680038/*!*/; insert into t2 (name) values ('a100') /*!*/; # at 2669 # at 2701 #170814 11:07:27 server id 1882073306 end_log_pos 2701 CRC32 0xcf89b910 Intvar SET INSERT_ID=108/*!*/; #170814 11:07:27 server id 1882073306 end_log_pos 2810 CRC32 0x78466d7b Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680047/*!*/; insert into t2 (name) values ('a200') /*!*/; # at 2810 # at 2842 #170814 11:07:30 server id 1882073306 end_log_pos 2842 CRC32 0x1e5a0847 Intvar SET INSERT_ID=109/*!*/; #170814 11:07:30 server id 1882073306 end_log_pos 2951 CRC32 0xebeb947c Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680050/*!*/; insert into t2 (name) values ('a300') /*!*/; # at 2951 #170814 11:07:34 server id 1882073306 end_log_pos 2982 CRC32 0x6436ad60 Xid = 32934 COMMIT/*!*/;
從庫(kù)relay-log:
/*!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 17:17:24 server id 1882083306 end_log_pos 120 CRC32 0x5df4221c Start: binlog v 4, server v 5.6.23-72.1-log created 170809 17:17:24 # at 120 #700101 8:00:00 server id 1882073306 end_log_pos 0 CRC32 0x0b8a412f Rotate to test-mysql-bin.000116 pos: 2451 # at 172 #170809 16:28:12 server id 1882073306 end_log_pos 0 CRC32 0xd0d3bf30 Start: binlog v 4, server v 5.6.23-72.1-log created 170809 16:28:12 # at 288 #170814 11:07:18 server id 1882073306 end_log_pos 2528 CRC32 0x40774a4b Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680038/*!*/; SET @@session.pseudo_thread_id=92/*!*/; 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 365 # at 397 #170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b Intvar SET INSERT_ID=107/*!*/; #170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8 Query thread_id=92 exec_time=0 error_code=0 use `db1`/*!*/; SET TIMESTAMP=1502680038/*!*/; insert into t2 (name) values ('a100') /*!*/; # at 506 # at 538 #170814 11:07:27 server id 1882073306 end_log_pos 2701 CRC32 0xcf89b910 Intvar SET INSERT_ID=108/*!*/; #170814 11:07:27 server id 1882073306 end_log_pos 2810 CRC32 0x78466d7b Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680047/*!*/; insert into t2 (name) values ('a200') /*!*/; # at 647 # at 679 #170814 11:07:30 server id 1882073306 end_log_pos 2842 CRC32 0x1e5a0847 Intvar SET INSERT_ID=109/*!*/; #170814 11:07:30 server id 1882073306 end_log_pos 2951 CRC32 0xebeb947c Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680050/*!*/; insert into t2 (name) values ('a300') /*!*/; # at 788 #170814 11:07:34 server id 1882073306 end_log_pos 2982 CRC32 0x6436ad60 Xid = 32934 COMMIT/*!*/;
注意relay log的這一行:
#700101 8:00:00 server id 1882073306 end_log_pos 0 CRC32 0x0b8a412f Rotate to test-mysql-bin.000116 pos: 2451
說明此relay log保存的是主庫(kù) test-mysql-bin.000116 的信息,從position 2451 開始。
看一個(gè)具體的對(duì)應(yīng)關(guān)系:
主庫(kù)的binlog如下:
# at 2560 #170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b Intvar SET INSERT_ID=107/*!*/; #170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8 Query thread_id=92 exec_time=0 error_code=0 SET TIMESTAMP=1502680038/*!*/; insert into t2 (name) values ('a100') /*!*/; # at 2669
對(duì)應(yīng)從庫(kù)relay-log如下幾行:
# at 397 #170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b Intvar SET INSERT_ID=107/*!*/; #170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8 Query thread_id=92 exec_time=0 error_code=0 use `db1`/*!*/; SET TIMESTAMP=1502680038/*!*/; insert into t2 (name) values ('a100') /*!*/; # at 506
另外注意show slave status\G的以下幾行的關(guān)系:
Master_Log_File: test-mysql-bin.000117 Read_Master_Log_Pos: 774
上面二行代表IO線程,相對(duì)于主庫(kù)
Relay_Log_File: relay-log.000038 Relay_Log_Pos: 723
上面二行代表了sql線程,相對(duì)于從庫(kù)
Relay_Master_Log_File: test-mysql-bin.000117 Exec_Master_Log_Pos: 555
上面二行代表了sql線程,相對(duì)主庫(kù)
其中Relay_Log_Pos: 723 和 Exec_Master_Log_Pos: 555 對(duì)應(yīng)的sql語句一致。
總結(jié)
以上就是本文關(guān)于MySQL主庫(kù)binlog與從庫(kù)relay-log關(guān)系代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:mysql中binlog_format模式與配置詳細(xì)分析、幾個(gè)比較重要的MySQL變量、MySQL prepare原理詳解等,有什么問題可以隨時(shí)留言,歡迎大家交流討論。
相關(guān)文章
Mysql聯(lián)合查詢UNION和Order by同時(shí)使用報(bào)錯(cuò)問題的解決辦法
很多朋友剛使用聯(lián)合查詢UNION的時(shí)候常常會(huì)理所當(dāng)然的將聯(lián)合查詢理解為把沒一個(gè)子查詢的結(jié)果集組合成一個(gè)大的結(jié)果集2014-04-04MySQL查詢倒數(shù)第二條記錄實(shí)現(xiàn)方法
這篇文章主要介紹了MySQL查詢倒數(shù)第二條記錄實(shí)現(xiàn)方法,本文直接給出代碼實(shí)例,重要部分已經(jīng)加紅提示,需要的朋友可以參考下2015-05-05詳細(xì)介紹基于MySQL的搜索引擎MySQL-Fullltext
這篇文章主要詳細(xì)介紹基于MySQL的搜索引擎MySQL-Fullltext,需要用到C和C#以及JavaScript的知識(shí),屬于MySQL的高階應(yīng)用,需要的朋友可以參考下2015-04-04網(wǎng)站前端和后臺(tái)性能優(yōu)化的34條寶貴經(jīng)驗(yàn)和方法
網(wǎng)站前端和后臺(tái)性能優(yōu)化的34條寶貴經(jīng)驗(yàn)和方法,相關(guān)網(wǎng)頁(yè)技術(shù)人員,需要注意的地方。2011-05-05MySQL中CURRENT_TIMESTAMP時(shí)間戳的使用詳解
這篇文章主要給大家介紹了關(guān)于MySQL中CURRENT_TIMESTAMP時(shí)間戳的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03