MySQL分布式概念(读写分离)
作用
对数据进行备份,实现高可用HA通过读写分离,提高吞吐量,实现高性能
原理
MySQL的复制是一个异步的赋值过程过程本质为Slave从Master端获取Binary Log,然后再在自己身上完全顺序的执行日志中所记录的各种操作MySQL复制的基本过程如下:
Slave 上面的IO线程连接上Master,并请求从指定日志文件的指定位置之后的日志内容。Master接受到来自Slave的IO线程的请求后,通过负责复制的IO线程根据请求信息读取日志信息,返回给Slave端的IO线程。Slave的IO线程接收到信息后,将接收到的日志内容依次写入到Slave端的Relay Log文件。Slave的SQL线程检测到Relay Log中新增加了内容后,会马上解析该文件中的内容,并在自身执行这些原始SQL语句。
#mermaid-svg-4BmvSFFfT9hWRGKB .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .label text{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .node rect,#mermaid-svg-4BmvSFFfT9hWRGKB .node circle,#mermaid-svg-4BmvSFFfT9hWRGKB .node ellipse,#mermaid-svg-4BmvSFFfT9hWRGKB .node polygon,#mermaid-svg-4BmvSFFfT9hWRGKB .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-4BmvSFFfT9hWRGKB .node .label{text-align:center;fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .node.clickable{cursor:pointer}#mermaid-svg-4BmvSFFfT9hWRGKB .arrowheadPath{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-4BmvSFFfT9hWRGKB .flowchart-link{stroke:#333;fill:none}#mermaid-svg-4BmvSFFfT9hWRGKB .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-4BmvSFFfT9hWRGKB .edgeLabel rect{opacity:0.9}#mermaid-svg-4BmvSFFfT9hWRGKB .edgeLabel span{color:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-4BmvSFFfT9hWRGKB .cluster text{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-4BmvSFFfT9hWRGKB .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-4BmvSFFfT9hWRGKB text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-4BmvSFFfT9hWRGKB .actor-line{stroke:grey}#mermaid-svg-4BmvSFFfT9hWRGKB .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-4BmvSFFfT9hWRGKB #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .sequenceNumber{fill:#fff}#mermaid-svg-4BmvSFFfT9hWRGKB #sequencenumber{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB #crosshead path{fill:#333;stroke:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .messageText{fill:#333;stroke:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-4BmvSFFfT9hWRGKB .labelText,#mermaid-svg-4BmvSFFfT9hWRGKB .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-4BmvSFFfT9hWRGKB .loopText,#mermaid-svg-4BmvSFFfT9hWRGKB .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-4BmvSFFfT9hWRGKB .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-4BmvSFFfT9hWRGKB .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-4BmvSFFfT9hWRGKB .noteText,#mermaid-svg-4BmvSFFfT9hWRGKB .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-4BmvSFFfT9hWRGKB .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-4BmvSFFfT9hWRGKB .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-4BmvSFFfT9hWRGKB .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-4BmvSFFfT9hWRGKB .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .section{stroke:none;opacity:0.2}#mermaid-svg-4BmvSFFfT9hWRGKB .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-4BmvSFFfT9hWRGKB .section2{fill:#fff400}#mermaid-svg-4BmvSFFfT9hWRGKB .section1,#mermaid-svg-4BmvSFFfT9hWRGKB .section3{fill:#fff;opacity:0.2}#mermaid-svg-4BmvSFFfT9hWRGKB .sectionTitle0{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .sectionTitle1{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .sectionTitle2{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .sectionTitle3{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-4BmvSFFfT9hWRGKB .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .grid path{stroke-width:0}#mermaid-svg-4BmvSFFfT9hWRGKB .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-4BmvSFFfT9hWRGKB .task{stroke-width:2}#mermaid-svg-4BmvSFFfT9hWRGKB .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .taskText:not([font-size]){font-size:11px}#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-4BmvSFFfT9hWRGKB .task.clickable{cursor:pointer}#mermaid-svg-4BmvSFFfT9hWRGKB .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-4BmvSFFfT9hWRGKB .taskText0,#mermaid-svg-4BmvSFFfT9hWRGKB .taskText1,#mermaid-svg-4BmvSFFfT9hWRGKB .taskText2,#mermaid-svg-4BmvSFFfT9hWRGKB .taskText3{fill:#fff}#mermaid-svg-4BmvSFFfT9hWRGKB .task0,#mermaid-svg-4BmvSFFfT9hWRGKB .task1,#mermaid-svg-4BmvSFFfT9hWRGKB .task2,#mermaid-svg-4BmvSFFfT9hWRGKB .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutside0,#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutside2{fill:#000}#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutside1,#mermaid-svg-4BmvSFFfT9hWRGKB .taskTextOutside3{fill:#000}#mermaid-svg-4BmvSFFfT9hWRGKB .active0,#mermaid-svg-4BmvSFFfT9hWRGKB .active1,#mermaid-svg-4BmvSFFfT9hWRGKB .active2,#mermaid-svg-4BmvSFFfT9hWRGKB .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-4BmvSFFfT9hWRGKB .activeText0,#mermaid-svg-4BmvSFFfT9hWRGKB .activeText1,#mermaid-svg-4BmvSFFfT9hWRGKB .activeText2,#mermaid-svg-4BmvSFFfT9hWRGKB .activeText3{fill:#000 !important}#mermaid-svg-4BmvSFFfT9hWRGKB .done0,#mermaid-svg-4BmvSFFfT9hWRGKB .done1,#mermaid-svg-4BmvSFFfT9hWRGKB .done2,#mermaid-svg-4BmvSFFfT9hWRGKB .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-4BmvSFFfT9hWRGKB .doneText0,#mermaid-svg-4BmvSFFfT9hWRGKB .doneText1,#mermaid-svg-4BmvSFFfT9hWRGKB .doneText2,#mermaid-svg-4BmvSFFfT9hWRGKB .doneText3{fill:#000 !important}#mermaid-svg-4BmvSFFfT9hWRGKB .crit0,#mermaid-svg-4BmvSFFfT9hWRGKB .crit1,#mermaid-svg-4BmvSFFfT9hWRGKB .crit2,#mermaid-svg-4BmvSFFfT9hWRGKB .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-4BmvSFFfT9hWRGKB .activeCrit0,#mermaid-svg-4BmvSFFfT9hWRGKB .activeCrit1,#mermaid-svg-4BmvSFFfT9hWRGKB .activeCrit2,#mermaid-svg-4BmvSFFfT9hWRGKB .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-4BmvSFFfT9hWRGKB .doneCrit0,#mermaid-svg-4BmvSFFfT9hWRGKB .doneCrit1,#mermaid-svg-4BmvSFFfT9hWRGKB .doneCrit2,#mermaid-svg-4BmvSFFfT9hWRGKB .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-4BmvSFFfT9hWRGKB .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-4BmvSFFfT9hWRGKB .milestoneText{font-style:italic}#mermaid-svg-4BmvSFFfT9hWRGKB .doneCritText0,#mermaid-svg-4BmvSFFfT9hWRGKB .doneCritText1,#mermaid-svg-4BmvSFFfT9hWRGKB .doneCritText2,#mermaid-svg-4BmvSFFfT9hWRGKB .doneCritText3{fill:#000 !important}#mermaid-svg-4BmvSFFfT9hWRGKB .activeCritText0,#mermaid-svg-4BmvSFFfT9hWRGKB .activeCritText1,#mermaid-svg-4BmvSFFfT9hWRGKB .activeCritText2,#mermaid-svg-4BmvSFFfT9hWRGKB .activeCritText3{fill:#000 !important}#mermaid-svg-4BmvSFFfT9hWRGKB .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-4BmvSFFfT9hWRGKB g.classGroup text .title{font-weight:bolder}#mermaid-svg-4BmvSFFfT9hWRGKB g.clickable{cursor:pointer}#mermaid-svg-4BmvSFFfT9hWRGKB g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-4BmvSFFfT9hWRGKB g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-4BmvSFFfT9hWRGKB .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-4BmvSFFfT9hWRGKB .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-4BmvSFFfT9hWRGKB .dashed-line{stroke-dasharray:3}#mermaid-svg-4BmvSFFfT9hWRGKB #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB .commit-id,#mermaid-svg-4BmvSFFfT9hWRGKB .commit-msg,#mermaid-svg-4BmvSFFfT9hWRGKB .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-4BmvSFFfT9hWRGKB g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-4BmvSFFfT9hWRGKB g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-4BmvSFFfT9hWRGKB g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-4BmvSFFfT9hWRGKB .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-4BmvSFFfT9hWRGKB .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-4BmvSFFfT9hWRGKB .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-4BmvSFFfT9hWRGKB .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-4BmvSFFfT9hWRGKB .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-4BmvSFFfT9hWRGKB .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-4BmvSFFfT9hWRGKB .edgeLabel text{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-4BmvSFFfT9hWRGKB .node circle.state-start{fill:black;stroke:black}#mermaid-svg-4BmvSFFfT9hWRGKB .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-4BmvSFFfT9hWRGKB #statediagram-barbEnd{fill:#9370db}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-state .divider{stroke:#9370db}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-4BmvSFFfT9hWRGKB .note-edge{stroke-dasharray:5}#mermaid-svg-4BmvSFFfT9hWRGKB .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-4BmvSFFfT9hWRGKB .error-icon{fill:#522}#mermaid-svg-4BmvSFFfT9hWRGKB .error-text{fill:#522;stroke:#522}#mermaid-svg-4BmvSFFfT9hWRGKB .edge-thickness-normal{stroke-width:2px}#mermaid-svg-4BmvSFFfT9hWRGKB .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-4BmvSFFfT9hWRGKB .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-4BmvSFFfT9hWRGKB .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-4BmvSFFfT9hWRGKB .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-4BmvSFFfT9hWRGKB .marker{fill:#333}#mermaid-svg-4BmvSFFfT9hWRGKB .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-4BmvSFFfT9hWRGKB {
color: rgba(0, 0, 0, 0.75);
font: ;
}
Master
Slave
Datechanges
Read
Write
Replay
Read
Relay
log
I/O
thread
SQL
thread
Binary
log
主库
常用架构
主从架构
性能
一主多从,读写分离,提高吞吐量 可用性
主库单点,一旦挂了,无法写入从库高可用
主备架构
性能
单库读写,性能一般 高可用
高可用,一旦主库挂了,就启用备库
(这种方案被阿里云,美团等企业广泛使用)
高可用复合框架(结合两种框架的优点设计)
性能
读写分离,提高吞吐量 可用性
高可用,一旦主库挂了,就启用备库