首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

MySQL 複制源碼解析:Slave IO 註冊與 Binlog Dump 請求協議詳解

     編輯:MySQL 綜合教學

COM_REGISTER_SLAVE 與 COM_BINLOG_DUMP 協議包構造與發送流程

承接上文,深入剖析 Slave IO 連接建立後的兩個關鍵命令:註冊從庫身份、請求主庫推送 binlog。

一、COM_REGISTER_SLAVE 註冊請求

協議包結構


1  byte       0x15 (COM_REGISTER_SLAVE)
4  uint32     server_id (slave 的 server-id,my.cnf 配置)
1  byte       slave_hostname_len
N  string     slave_hostname
1  byte       slave_user_len
N  string     slave_user (主庫授權的 replication 用戶)
1  byte       slave_password_len
N  string     slave_password
2  uint16     slave_mysql_port
4  uint32     replication_rank (已廢棄,固定 0)
4  uint32     master_id (固定 0,由主庫填充)

源碼構造流程


// sql/slave.cc: register_slave_on_master()
int4store(pos, server_id); pos += 4;
pos = net_store_data(pos, report_host, report_host_len);
pos = net_store_data(pos, report_user, report_user_len);
pos = net_store_data(pos, report_password, report_password_len);
int2store(pos, report_port); pos += 2;
int4store(pos, 0); pos += 4;  // rpl_recovery_rank
int4store(pos, 0); pos += 4;  // master_id
simple_command(mysql, COM_REGISTER_SLAVE, buffer, length, 1);

net_store_data 先寫長度(1 字節),再寫內容。simple_command 封裝 net_write_command 添加協議頭(長度 3B + 序號 1B)。

二、COM_BINLOG_DUMP Binlog 請求

兩種格式對比

命令值適用場景包結構
COM_BINLOG_DUMP0x12傳統位置基礎複製(主流)pos(4) + flags(2) + server_id(4) + filename(EOF)
COM_BINLOG_DUMP_GTID0x1EGTID 模式複製 (5.6.5+)flags(2) + server_id(4) + sid_map + gtid_set

COM_BINLOG_DUMP 詳細結構


1  byte       0x12 (COM_BINLOG_DUMP)
4  uint32     binlog_pos (請求起始位置,mi->get_master_log_pos())
2  uint16     flags (通常 BINLOG_DUMP_NON_BLOCK = 0x01)
4  uint32     server_id (slave 的 server-id)
N  string[EOF] binlog_filename (mi->get_master_log_name(), 如 mysql-bin.000001)

源碼構造流程


// sql/slave.cc: request_dump()
int4store(ptr, mi->get_master_log_pos()); ptr += 4;
int2store(ptr, binlog_flags); ptr += 2;  // BINLOG_DUMP_NON_BLOCK
int4store(ptr, server_id); ptr += 4;
memcpy(ptr, mi->get_master_log_name(), BINLOG_NAME_INFO_SIZE); // 通常 60 字節
simple_command(mysql, COM_BINLOG_DUMP, buffer, length, 1);

三、MySQL 協議通用包頭

所有命令包統一前綴:


3  byte       payload_length (小端序,最大 16MB-1)
1  byte       sequence_id (從 0 遞增,重置於新命令)
N  byte       payload (命令碼 + 參數)

超過 16MB 自動分片:每片 16MB,序號遞增,最後一片長度 < 16MB。

四、關鍵標志位含義

標志值含義
BINLOG_DUMP_NON_BLOCK0x01非阻塞模式:主庫無新 binlog 時立即返回 EOF 包,不等待
CLIENT_PROTOCOL_410x000002004.1 協議,認證響應包必須設置
CLIENT_PLUGIN_AUTH0x00080000支持插件認證,包含 plugin_name
CLIENT_CONNECT_WITH_DB0x00000008認證包攜帶默認庫

五、調試與抓包建議


# 抓取 3306 端口協議包
tcpdump -i eth0 -nn -s 0 -X port 3306 -w mysql_repl.pcap

# Wireshark 過濾
mysql.protocol.cmd == 0x12  # Binlog Dump
mysql.protocol.cmd == 0x15  # Register Slave

理解註冊與 Dump 協議,可實現:自定義複製代理、binlog 解析工具、異構同步中間件、複製延遲監控探針。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved