MySQL GTID复制原理与与传统复制模式对比:从传统到现代的复制演进 - label rect,#publish-mermaid-3-1792074364768
/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */
#content_views .toc,
/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */
#content_views.markdown_views > p:empty:has(+ .toc),
#content_views.markdown_views > .toc + p:empty,
/* 富文本旧版目录标记 */
#content_views.htmledit_views #main-toc,
#content_views.htmledit_views #hr-toc,
#content_views.htmledit_views p[id*="-toc"] {
display: none !important;
}
/* 目录去掉后,紧跟的首个标题不再多出一块上边距 */
#content_views.markdown_views > .toc + h1,
#content_views.markdown_views > .toc + h2,
#content_views.markdown_views > .toc + h3,
#content_views.markdown_views > .toc + h4,
#content_views.markdown_views > .toc + p:empty + h1,
#content_views.markdown_views > .toc + p:empty + h2,
#content_views.markdown_views > .toc + p:empty + h3,
#content_views.markdown_views > .toc + p:empty + h4 {
margin-top: 0 !important;
}
一、MySQL 传统复制模式:原理与痛点 拼多多帮助力入口
1.1 传统复制的工作流程
在 MySQL 5.6 之前,主从复制主要依赖二进制日志文件名(binlog file)和文件内的位置(position)来追踪和同步数据。主库将二进制日志事件发送给从库,从库的 I/O 线程读取并写入中继日志,SQL 线程再从中继日志中读取并应用这些事件。这个过程需要手动记录和配置主库的日志文件和位置,才能确保从库从正确的点开始复制。
SlaveMasterSlaveMaster#publish-mermaid-0-1792074364378{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-0-1792074364378 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792074364378 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792074364378 .error-icon{fill:#552222;}#publish-mermaid-0-1792074364378 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-0-1792074364378 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-0-1792074364378 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-0-1792074364378 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-0-1792074364378 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-0-1792074364378 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-0-1792074364378 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-0-1792074364378 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-0-1792074364378 .marker.cross{stroke:#333333;}#publish-mermaid-0-1792074364378 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-0-1792074364378 p{margin:0;}#publish-mermaid-0-1792074364378 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-0-1792074364378 rect.actor.outer-path[data-look="neo"]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 rect.note[data-look="neo"]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-0-1792074364378 .actor-line{stroke:#9370DB;}#publish-mermaid-0-1792074364378 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-0-1792074364378 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-0-1792074364378 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-0-1792074364378 [id$="-arrowhead"] path{fill:#333;stroke:#333;}#publish-mermaid-0-1792074364378 .sequenceNumber{fill:white;}#publish-mermaid-0-1792074364378 [id$="-sequencenumber"]{fill:#333;}#publish-mermaid-0-1792074364378 [id$="-crosshead"] path{fill:#333;stroke:#333;}#publish-mermaid-0-1792074364378 .messageText{fill:#333;stroke:none;}#publish-mermaid-0-1792074364378 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-0-1792074364378 .labelText,#publish-mermaid-0-1792074364378 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-0-1792074364378 .loopText,#publish-mermaid-0-1792074364378 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-0-1792074364378 .sectionTitle,#publish-mermaid-0-1792074364378 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-0-1792074364378 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-0-1792074364378 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-0-1792074364378 .noteText,#publish-mermaid-0-1792074364378 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-0-1792074364378 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-0-1792074364378 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-0-1792074364378 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-0-1792074364378 .actorPopupMenu{position:absolute;}#publish-mermaid-0-1792074364378 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-0-1792074364378 .actor-man circle,#publish-mermaid-0-1792074364378 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-0-1792074364378 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-0-1792074364378 .node .neo-node{stroke:#9370DB;}#publish-mermaid-0-1792074364378 [data-look="neo"].node rect,#publish-mermaid-0-1792074364378 [data-look="neo"].cluster rect,#publish-mermaid-0-1792074364378 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-0-1792074364378 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-0-1792074364378 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-0-1792074364378 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-0-1792074364378 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792074364378 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}发送 Binlog Event (File: mysql-bin.000001, Pos: 1234)I/O 线程写入 Relay LogSQL 线程读取 Relay Log 并应用
该时序图展示了传统复制的基本交互过程,主库将带有文件名和位置信息的二进制日志事件发送给从库,从库通过 I/O 线程和 SQL 线程完成中继和应用。
1.2 传统复制的核心痛点
传统复制模式虽然成熟,但在实际运维中存在一些显著的痛点,主要包括主从切换复杂、数据不一致风险以及复制中断后定位困难。
#publish-mermaid-1-1792074364503{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1-1792074364503 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792074364503 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792074364503 .error-icon{fill:#552222;}#publish-mermaid-1-1792074364503 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1-1792074364503 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1-1792074364503 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1-1792074364503 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1-1792074364503 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1-1792074364503 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1-1792074364503 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1-1792074364503 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1-1792074364503 .marker.cross{stroke:#333333;}#publish-mermaid-1-1792074364503 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1-1792074364503 p{margin:0;}#publish-mermaid-1-1792074364503 .pieCircle{stroke:black;stroke-width:2px;opacity:0.7;}#publish-mermaid-1-1792074364503 .pieCircle.highlighted{scale:1.05;opacity:1;}#publish-mermaid-1-1792074364503 .pieCircle.highlightedOnHover:hover{transition-duration:250ms;scale:1.05;opacity:1;}#publish-mermaid-1-1792074364503 .pieOuterCircle{stroke:black;stroke-width:2px;fill:none;}#publish-mermaid-1-1792074364503 .pieTitleText{text-anchor:middle;font-size:25px;fill:black;font-family:"trebuchet ms",verdana,arial,sans-serif;}#publish-mermaid-1-1792074364503 .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#333;font-size:17px;}#publish-mermaid-1-1792074364503 .legend text{fill:black;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#publish-mermaid-1-1792074364503 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1-1792074364503 [data-look="neo"].node rect,#publish-mermaid-1-1792074364503 [data-look="neo"].cluster rect,#publish-mermaid-1-1792074364503 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792074364503 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-1-1792074364503 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1-1792074364503 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792074364503 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1-1792074364503 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792074364503 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-1-1792074364503 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792074364503 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792074364503 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}40%35%25%传统复制痛点分布主从切换复杂数据不一致风险复制中断定位难
上图通过饼图直观展示了传统复制模式主要痛点的占比,其中主从切换复杂和确保数据一致性是最大的挑战。
二、GTID 的诞生:全局事务标识符
2.1 GTID 的定义与构成
为了解决传统复制的痛点,MySQL 5.6 引入了 GTID(Global Transaction Identifier)。GTID 是一个全局唯一的事务标识符,由两部分组成:server_uuid(服务器唯一标识符)和 transaction_id(事务序列号)。例如,一个 GTID 可能是 3E11FA47-71CA-11E1-9E33-C80AA9429562:23。
#publish-mermaid-2-1792074364644{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-2-1792074364644 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792074364644 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792074364644 .error-icon{fill:#552222;}#publish-mermaid-2-1792074364644 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-2-1792074364644 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-2-1792074364644 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-2-1792074364644 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-2-1792074364644 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-2-1792074364644 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-2-1792074364644 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-2-1792074364644 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-2-1792074364644 .marker.cross{stroke:#333333;}#publish-mermaid-2-1792074364644 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-2-1792074364644 p{margin:0;}#publish-mermaid-2-1792074364644 g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#publish-mermaid-2-1792074364644 g.classGroup text .title{font-weight:bolder;}#publish-mermaid-2-1792074364644 .cluster-label text{fill:#333;}#publish-mermaid-2-1792074364644 .cluster-label span{color:#333;}#publish-mermaid-2-1792074364644 .cluster-label span p{background-color:transparent;}#publish-mermaid-2-1792074364644 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-2-1792074364644 .cluster text{fill:#333;}#publish-mermaid-2-1792074364644 .cluster span{color:#333;}#publish-mermaid-2-1792074364644 .nodeLabel,#publish-mermaid-2-1792074364644 .edgeLabel{color:#131300;}#publish-mermaid-2-1792074364644 .noteLabel .nodeLabel,#publish-mermaid-2-1792074364644 .noteLabel .edgeLabel{color:black;}#publish-mermaid-2-1792074364644 .edgeLabel .label rect{fill:#ECECFF;}#publish-mermaid-2-1792074364644 .label text{fill:#131300;}#publish-mermaid-2-1792074364644 .labelBkg{background:#ECECFF;}#publish-mermaid-2-1792074364644 .edgeLabel .label span{background:#ECECFF;}#publish-mermaid-2-1792074364644 .classTitle{font-weight:bolder;}#publish-mermaid-2-1792074364644 .node rect,#publish-mermaid-2-1792074364644 .node circle,#publish-mermaid-2-1792074364644 .node ellipse,#publish-mermaid-2-1792074364644 .node polygon,#publish-mermaid-2-1792074364644 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1;}#publish-mermaid-2-1792074364644 .divider{stroke:#9370DB;stroke-width:1;}#publish-mermaid-2-1792074364644 g.clickable{cursor:pointer;}#publish-mermaid-2-1792074364644 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#publish-mermaid-2-1792074364644 g.classGroup line{stroke:#9370DB;stroke-width:1;}#publish-mermaid-2-1792074364644 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#publish-mermaid-2-1792074364644 .classLabel .label{fill:#9370DB;font-size:10px;}#publish-mermaid-2-1792074364644 .relation{stroke:#333333;stroke-width:1;fill:none;}#publish-mermaid-2-1792074364644 .dashed-line{stroke-dasharray:3;}#publish-mermaid-2-1792074364644 .dotted-line{stroke-dasharray:1 2;}#publish-mermaid-2-1792074364644 [id$="-compositionStart"],#publish-mermaid-2-1792074364644 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-compositionEnd"],#publish-mermaid-2-1792074364644 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-dependencyStart"],#publish-mermaid-2-1792074364644 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-dependencyEnd"],#publish-mermaid-2-1792074364644 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-extensionStart"],#publish-mermaid-2-1792074364644 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-extensionEnd"],#publish-mermaid-2-1792074364644 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-aggregationStart"],#publish-mermaid-2-1792074364644 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-aggregationEnd"],#publish-mermaid-2-1792074364644 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-lollipopStart"],#publish-mermaid-2-1792074364644 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 [id$="-lollipopEnd"],#publish-mermaid-2-1792074364644 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#publish-mermaid-2-1792074364644 .edgeTerminals{font-size:11px;line-height:initial;}#publish-mermaid-2-1792074364644 .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-2-1792074364644 .edgeLabel[data-look="neo"]{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-2-1792074364644 .edgeLabel[data-look="neo"] p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-2-1792074364644 .edgeLabel[data-look="neo"] rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-2-1792074364644 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-2-1792074364644 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-2-1792074364644 .node .neo-node{stroke:#9370DB;}#publish-mermaid-2-1792074364644 [data-look="neo"].node rect,#publish-mermaid-2-1792074364644 [data-look="neo"].cluster rect,#publish-mermaid-2-1792074364644 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792074364644 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-2-1792074364644 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-2-1792074364644 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792074364644 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-2-1792074364644 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792074364644 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-2-1792074364644 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792074364644 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792074364644 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}GTID+server_uuid String+transaction_id Integer+toString() : String
该类图定义了 GTID 的基本结构,清晰地展示了其由服务器 UUID 和事务序列号构成。
2.2 GTID 的工作机制
GTID 的工作机制非常直接。当一个事务在主库上提交时,MySQL 会自动为其分配一个唯一的 GTID,并将该 GTID 记录在二进制日志中。从库在复制过程中,会记录自己已经应用过的 GTID 集合(gtid_executed),主库则记录自己生成过的 GTID 集合(gtid_purged 和 gtid_executed)。这使得从库可以精确地知道哪些事务已经应用,哪些需要应用。
#publish-mermaid-3-1792074364768{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-3-1792074364768 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792074364768 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792074364768 .error-icon{fill:#552222;}#publish-mermaid-3-1792074364768 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-3-1792074364768 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-3-1792074364768 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-3-1792074364768 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-3-1792074364768 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-3-1792074364768 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-3-1792074364768 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-3-1792074364768 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-3-1792074364768 .marker.cross{stroke:#333333;}#publish-mermaid-3-1792074364768 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-3-1792074364768 p{margin:0;}#publish-mermaid-3-1792074364768 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-3-1792074364768 .cluster-label text{fill:#333;}#publish-mermaid-3-1792074364768 .cluster-label span{color:#333;}#publish-mermaid-3-1792074364768 .cluster-label span p{background-color:transparent;}#publish-mermaid-3-1792074364768 .label text,#publish-mermaid-3-1792074364768 span{fill:#333;color:#333;}#publish-mermaid-3-1792074364768 .node rect,#publish-mermaid-3-1792074364768 .node circle,#publish-mermaid-3-1792074364768 .node ellipse,#publish-mermaid-3-1792074364768 .node polygon,#publish-mermaid-3-1792074364768 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792074364768 .rough-node .label text,#publish-mermaid-3-1792074364768 .node .label text,#publish-mermaid-3-1792074364768 .image-shape .label,#publish-mermaid-3-1792074364768 .icon-shape .label{text-anchor:middle;}#publish-mermaid-3-1792074364768 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-3-1792074364768 .rough-node .label,#publish-mermaid-3-1792074364768 .node .label,#publish-mermaid-3-1792074364768 .image-shape .label,#publish-mermaid-3-1792074364768 .icon-shape .label{text-align:center;}#publish-mermaid-3-1792074364768 .node.clickable{cursor:pointer;}#publish-mermaid-3-1792074364768 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-3-1792074364768 .arrowheadPath{fill:#333333;}#publish-mermaid-3-1792074364768 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-3-1792074364768 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-3-1792074364768 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-3-1792074364768 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792074364768 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792074364768 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-3-1792074364768 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-3-1792074364768 .cluster text{fill:#333;}#publish-mermaid-3-1792074364768 .cluster span{color:#333;}#publish-mermaid-3-1792074364768 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-3-1792074364768 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-3-1792074364768 rect.text{fill:none;stroke-width:0;}#publish-mermaid-3-1792074364768 .icon-shape,#publish-mermaid-3-1792074364768 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-3-1792074364768 .icon-shape p,#publish-mermaid-3-1792074364768 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-3-1792074364768 .icon-shape .label rect,#publish-mermaid-3-1792074364768 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-3-1792074364768 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-3-1792074364768 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-3-1792074364768 .node .neo-node{stroke:#9370DB;}#publish-mermaid-3-1792074364768 [data-look="neo"].node rect,#publish-mermaid-3-1792074364768 [data-look="neo"].cluster rect,#publish-mermaid-3-1792074364768 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792074364768 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-3-1792074364768 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792074364768 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792074364768 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-3-1792074364768 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792074364768 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-3-1792074364768 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792074364768 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792074364768 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}事务开始分配 GTID写入 Binlog更新 Master 的 GTID Set从库接收 Binlog更新 Slave 的 GTID Set应用事务
此流程图展示了 GTID 从分配到在主从间同步和应用的全过程,确保了事务的精确追踪。
三、GTID 复制模式:原理详解
3.1 GTID 复制的工作流程
在 GTID 复制模式下,从库不再需要指定文件名和位置,而是通过 GTID 来标识需要复制的起始点。从库在连接主库时,会将自己的 gtid_executed 集合发送给主库,主库则返回从该集合之后的所有二进制日志事件。
SlaveMasterSlaveMaster#publish-mermaid-4-1792074364849{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-4-1792074364849 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792074364849 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792074364849 .error-icon{fill:#552222;}#publish-mermaid-4-1792074364849 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-4-1792074364849 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-4-1792074364849 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-4-1792074364849 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-4-1792074364849 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-4-1792074364849 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-4-1792074364849 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-4-1792074364849 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792074364849 .marker.cross{stroke:#333333;}#publish-mermaid-4-1792074364849 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-4-1792074364849 p{margin:0;}#publish-mermaid-4-1792074364849 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-4-1792074364849 rect.actor.outer-path[data-look="neo"]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 rect.note[data-look="neo"]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-4-1792074364849 .actor-line{stroke:#9370DB;}#publish-mermaid-4-1792074364849 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-4-1792074364849 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-4-1792074364849 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-4-1792074364849 [id$="-arrowhead"] path{fill:#333;stroke:#333;}#publish-mermaid-4-1792074364849 .sequenceNumber{fill:white;}#publish-mermaid-4-1792074364849 [id$="-sequencenumber"]{fill:#333;}#publish-mermaid-4-1792074364849 [id$="-crosshead"] path{fill:#333;stroke:#333;}#publish-mermaid-4-1792074364849 .messageText{fill:#333;stroke:none;}#publish-mermaid-4-1792074364849 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-4-1792074364849 .labelText,#publish-mermaid-4-1792074364849 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-4-1792074364849 .loopText,#publish-mermaid-4-1792074364849 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-4-1792074364849 .sectionTitle,#publish-mermaid-4-1792074364849 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-4-1792074364849 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-4-1792074364849 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-4-1792074364849 .noteText,#publish-mermaid-4-1792074364849 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-4-1792074364849 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-4-1792074364849 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-4-1792074364849 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-4-1792074364849 .actorPopupMenu{position:absolute;}#publish-mermaid-4-1792074364849 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#publish-mermaid-4-1792074364849 .actor-man circle,#publish-mermaid-4-1792074364849 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-4-1792074364849 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-4-1792074364849 .node .neo-node{stroke:#9370DB;}#publish-mermaid-4-1792074364849 [data-look="neo"].node rect,#publish-mermaid-4-1792074364849 [data-look="neo"].cluster rect,#publish-mermaid-4-1792074364849 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-4-1792074364849 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792074364849 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-4-1792074364849 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-4-1792074364849 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792074364849 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}连接请求,附带 gtid_executed 集合查询自身的 gtid_executed 集合发送 Binlog Event (GTID: 3e11...:101, 3e11...:102)I/O 线程写入 Relay LogSQL 线程读取 Relay Log 并应用更新 gtid_executed 集合
该时序图详细描述了 GTID 复制中主从的交互,特别是从库通过 GTID 集合向主库请求缺失的事务。
3.2 GTID 复制的核心优势
GTID 复制模式带来了诸多优势,最显著的是自动化、一致性和简化运维。它极大地简化了主从切换和故障转移过程,因为从库总能知道从哪里继续复制,而无需手动干预。
#publish-mermaid-5-1792074364876{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-5-1792074364876 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-5-1792074364876 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-5-1792074364876 .error-icon{fill:#552222;}#publish-mermaid-5-1792074364876 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-5-1792074364876 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-5-1792074364876 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-5-1792074364876 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-5-1792074364876 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-5-1792074364876 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-5-1792074364876 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-5-1792074364876 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-5-1792074364876 .marker.cross{stroke:#333333;}#publish-mermaid-5-1792074364876 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-5-1792074364876 p{margin:0;}#publish-mermaid-5-1792074364876 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-5-1792074364876 .cluster-label text{fill:#333;}#publish-mermaid-5-1792074364876 .cluster-label span{color:#333;}#publish-mermaid-5-1792074364876 .cluster-label span p{background-color:transparent;}#publish-mermaid-5-1792074364876 .label text,#publish-mermaid-5-1792074364876 span{fill:#333;color:#333;}#publish-mermaid-5-1792074364876 .node rect,#publish-mermaid-5-1792074364876 .node circle,#publish-mermaid-5-1792074364876 .node ellipse,#publish-mermaid-5-1792074364876 .node polygon,#publish-mermaid-5-1792074364876 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-5-1792074364876 .rough-node .label text,#publish-mermaid-5-1792074364876 .node .label text,#publish-mermaid-5-1792074364876 .image-shape .label,#publish-mermaid-5-1792074364876 .icon-shape .label{text-anchor:middle;}#publish-mermaid-5-1792074364876 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-5-1792074364876 .rough-node .label,#publish-mermaid-5-1792074364876 .node .label,#publish-mermaid-5-1792074364876 .image-shape .label,#publish-mermaid-5-1792074364876 .icon-shape .label{text-align:center;}#publish-mermaid-5-1792074364876 .node.clickable{cursor:pointer;}#publish-mermaid-5-1792074364876 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-5-1792074364876 .arrowheadPath{fill:#333333;}#publish-mermaid-5-1792074364876 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-5-1792074364876 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-5-1792074364876 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-5-1792074364876 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-5-1792074364876 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-5-1792074364876 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-5-1792074364876 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-5-1792074364876 .cluster text{fill:#333;}#publish-mermaid-5-1792074364876 .cluster span{color:#333;}#publish-mermaid-5-1792074364876 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-5-1792074364876 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-5-1792074364876 rect.text{fill:none;stroke-width:0;}#publish-mermaid-5-1792074364876 .icon-shape,#publish-mermaid-5-1792074364876 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-5-1792074364876 .icon-shape p,#publish-mermaid-5-1792074364876 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-5-1792074364876 .icon-shape .label rect,#publish-mermaid-5-1792074364876 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-5-1792074364876 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-5-1792074364876 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-5-1792074364876 .node .neo-node{stroke:#9370DB;}#publish-mermaid-5-1792074364876 [data-look="neo"].node rect,#publish-mermaid-5-1792074364876 [data-look="neo"].cluster rect,#publish-mermaid-5-1792074364876 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792074364876 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-5-1792074364876 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-5-1792074364876 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792074364876 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-5-1792074364876 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792074364876 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-5-1792074364876 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792074364876 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-5-1792074364876 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}自动化故障转移简化运维确保数据一致性无需手动记录 File/Pos精确的事务追踪
此流程图概括了 GTID 复制的核心优势,强调了其在自动化和一致性方面的突破。
四、GTID 与传统复制模式的对比分析
4.1 核心差异对比
下表对 GTID 复制和传统复制在几个关键维度上进行了详细对比,帮助读者更清晰地理解两者的区别。
特性传统复制GTID 复制故障转移手动配置新的 File/Pos,过程复杂且易出错自动定位复制点,切换后从库自动从正确位置开始数据一致性依赖手动检查和配置,存在不一致风险基于 GTID 集合,确保主从数据一致配置复杂度需要精确配置 master_log_file 和 master_log_pos配置简单,只需指定 MASTER_AUTO_POSITION=1复制中断恢复需要人工分析日志,定位中断点自动跳过已执行的事务,恢复复制
4.2 适用场景分析
选择哪种复制模式取决于具体的应用场景和需求。对于需要高可用、自动化运维的现代应用,GTID 是首选。而对于一些简单的、对自动化要求不高的场景,传统复制可能仍然适用。
#publish-mermaid-6-1792074364925{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-6-1792074364925 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-6-1792074364925 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-6-1792074364925 .error-icon{fill:#552222;}#publish-mermaid-6-1792074364925 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-6-1792074364925 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-6-1792074364925 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-6-1792074364925 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-6-1792074364925 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-6-1792074364925 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-6-1792074364925 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-6-1792074364925 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-6-1792074364925 .marker.cross{stroke:#333333;}#publish-mermaid-6-1792074364925 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-6-1792074364925 p{margin:0;}#publish-mermaid-6-1792074364925 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-6-1792074364925 .cluster-label text{fill:#333;}#publish-mermaid-6-1792074364925 .cluster-label span{color:#333;}#publish-mermaid-6-1792074364925 .cluster-label span p{background-color:transparent;}#publish-mermaid-6-1792074364925 .label text,#publish-mermaid-6-1792074364925 span{fill:#333;color:#333;}#publish-mermaid-6-1792074364925 .node rect,#publish-mermaid-6-1792074364925 .node circle,#publish-mermaid-6-1792074364925 .node ellipse,#publish-mermaid-6-1792074364925 .node polygon,#publish-mermaid-6-1792074364925 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-6-1792074364925 .rough-node .label text,#publish-mermaid-6-1792074364925 .node .label text,#publish-mermaid-6-1792074364925 .image-shape .label,#publish-mermaid-6-1792074364925 .icon-shape .label{text-anchor:middle;}#publish-mermaid-6-1792074364925 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-6-1792074364925 .rough-node .label,#publish-mermaid-6-1792074364925 .node .label,#publish-mermaid-6-1792074364925 .image-shape .label,#publish-mermaid-6-1792074364925 .icon-shape .label{text-align:center;}#publish-mermaid-6-1792074364925 .node.clickable{cursor:pointer;}#publish-mermaid-6-1792074364925 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-6-1792074364925 .arrowheadPath{fill:#333333;}#publish-mermaid-6-1792074364925 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-6-1792074364925 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-6-1792074364925 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-6-1792074364925 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-6-1792074364925 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-6-1792074364925 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-6-1792074364925 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-6-1792074364925 .cluster text{fill:#333;}#publish-mermaid-6-1792074364925 .cluster span{color:#333;}#publish-mermaid-6-1792074364925 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-6-1792074364925 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-6-1792074364925 rect.text{fill:none;stroke-width:0;}#publish-mermaid-6-1792074364925 .icon-shape,#publish-mermaid-6-1792074364925 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-6-1792074364925 .icon-shape p,#publish-mermaid-6-1792074364925 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-6-1792074364925 .icon-shape .label rect,#publish-mermaid-6-1792074364925 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-6-1792074364925 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-6-1792074364925 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-6-1792074364925 .node .neo-node{stroke:#9370DB;}#publish-mermaid-6-1792074364925 [data-look="neo"].node rect,#publish-mermaid-6-1792074364925 [data-look="neo"].cluster rect,#publish-mermaid-6-1792074364925 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-6-1792074364925 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-6-1792074364925 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-6-1792074364925 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-6-1792074364925 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-6-1792074364925 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-6-1792074364925 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-6-1792074364925 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-6-1792074364925 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-6-1792074364925 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}是否是否需要自动化、高可用?推荐使用 GTID 复制可考虑传统复制电商、金融等核心系统简单备份、数据同步
此流程图提供了一个决策依据,帮助读者根据自身需求选择合适的复制模式。
五、GTID 复制的配置与最佳实践
5.1 启用 GTID 的关键配置
要在 MySQL 中启用 GTID 复制,需要在主从库的配置文件(my.cnf 或 my.ini)中进行以下关键配置:
gtid_mode=ONenforce-gtid-consistency=ONlog_slave_updates=ON (从库需要此配置以记录自己的 GTID)
5.2 GTID 复制的注意事项
虽然 GTID 简化了复制,但在使用过程中仍需注意一些事项,例如事务跳过机制。当从库遇到一个在主库上不存在的事务(如被过滤的 DDL)时,它会自动跳过该事务,并记录跳过的事务 ID。
#publish-mermaid-7-1792074364974{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-7-1792074364974 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-7-1792074364974 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-7-1792074364974 .error-icon{fill:#552222;}#publish-mermaid-7-1792074364974 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-7-1792074364974 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-7-1792074364974 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-7-1792074364974 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-7-1792074364974 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-7-1792074364974 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-7-1792074364974 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-7-1792074364974 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-7-1792074364974 .marker.cross{stroke:#333333;}#publish-mermaid-7-1792074364974 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-7-1792074364974 p{margin:0;}#publish-mermaid-7-1792074364974 defs [id$="-barbEnd"]{fill:#333333;stroke:#333333;}#publish-mermaid-7-1792074364974 g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#publish-mermaid-7-1792074364974 g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#publish-mermaid-7-1792074364974 g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#publish-mermaid-7-1792074364974 g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#publish-mermaid-7-1792074364974 g.stateGroup line{stroke:#333333;stroke-width:1;}#publish-mermaid-7-1792074364974 .transition{stroke:#333333;stroke-width:1;fill:none;}#publish-mermaid-7-1792074364974 .stateGroup .composit{fill:white;border-bottom:1px;}#publish-mermaid-7-1792074364974 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#publish-mermaid-7-1792074364974 .state-note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-7-1792074364974 .state-note text{fill:black;stroke:none;font-size:10px;}#publish-mermaid-7-1792074364974 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#publish-mermaid-7-1792074364974 .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#publish-mermaid-7-1792074364974 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-7-1792074364974 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-7-1792074364974 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-7-1792074364974 .edgeLabel .label text{fill:#333;}#publish-mermaid-7-1792074364974 .label div .edgeLabel{color:#333;}#publish-mermaid-7-1792074364974 .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#publish-mermaid-7-1792074364974 .node circle.state-start{fill:#333333;stroke:#333333;}#publish-mermaid-7-1792074364974 .node .fork-join{fill:#333333;stroke:#333333;}#publish-mermaid-7-1792074364974 .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#publish-mermaid-7-1792074364974 .end-state-inner{fill:white;stroke-width:1.5;}#publish-mermaid-7-1792074364974 .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-7-1792074364974 .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-7-1792074364974 [id$="-barbEnd"]{fill:#333333;}#publish-mermaid-7-1792074364974 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-7-1792074364974 .cluster-label,#publish-mermaid-7-1792074364974 .nodeLabel{color:#131300;}#publish-mermaid-7-1792074364974 .statediagram-cluster rect.outer{rx:5px;ry:5px;}#publish-mermaid-7-1792074364974 .statediagram-state .divider{stroke:#9370DB;}#publish-mermaid-7-1792074364974 .statediagram-state .title-state{rx:5px;ry:5px;}#publish-mermaid-7-1792074364974 .statediagram-cluster.statediagram-cluster .inner{fill:white;}#publish-mermaid-7-1792074364974 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#publish-mermaid-7-1792074364974 .statediagram-cluster .inner{rx:0;ry:0;}#publish-mermaid-7-1792074364974 .statediagram-state rect.basic{rx:5px;ry:5px;}#publish-mermaid-7-1792074364974 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#publish-mermaid-7-1792074364974 .note-edge{stroke-dasharray:5;}#publish-mermaid-7-1792074364974 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#publish-mermaid-7-1792074364974 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#publish-mermaid-7-1792074364974 .statediagram-note text{fill:black;}#publish-mermaid-7-1792074364974 .statediagram-note .nodeLabel{color:black;}#publish-mermaid-7-1792074364974 .statediagram .edgeLabel{color:red;}#publish-mermaid-7-1792074364974 [id$="-dependencyStart"],#publish-mermaid-7-1792074364974 [id$="-dependencyEnd"]{fill:#333333;stroke:#333333;stroke-width:1;}#publish-mermaid-7-1792074364974 .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-7-1792074364974 [data-look="neo"].statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1;}#publish-mermaid-7-1792074364974 [data-look="neo"].statediagram-cluster rect.outer{rx:5px;ry:5px;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-7-1792074364974 .node .neo-node{stroke:#9370DB;}#publish-mermaid-7-1792074364974 [data-look="neo"].node rect,#publish-mermaid-7-1792074364974 [data-look="neo"].cluster rect,#publish-mermaid-7-1792074364974 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-7-1792074364974 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-7-1792074364974 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-7-1792074364974 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-7-1792074364974 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-7-1792074364974 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-7-1792074364974 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-7-1792074364974 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-7-1792074364974 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-7-1792074364974 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}正常复制事务跳过:当遇到不兼容事务时事务跳过正常复制:记录跳过的事务ID
此状态图展示了 GTID 复制中可能遇到的事务跳过状态及其恢复过程,强调了监控跳过事务的重要性。