MySQL MHA高可用架构搭建与故障自动切换实战:从零到一构建数据库高可用解决方案 - 安装并配置 MySQL:在三台服务器上安装相同版本的 MySQL,并配置主从复制

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd助力 👁️ 449 次阅读

/* 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 MHA 架构概述:理解高可用核心组件

MySQL MHA(Master High Availability)是一套优秀的 MySQL 高可用解决方案,能在 Master 节点故障时,自动将其中一台 Slave 提升为新的 Master,并保证数据的一致性。MHA 架构主要由以下几个核心组件构成:

Master:主数据库,处理写操作,其 Binlog 被所有 Slave 复制。Slave:从数据库,通过复制 Master 的 Binlog 来同步数据,通常配置为只读。MHA Node:部署在每个 MySQL 节点(Master 和 Slave)上的一个轻量级脚本集合,负责监控、故障转移时执行脚本等。MHA Manager:部署在一台独立服务器上的管理节点,负责检测 Master 节点健康状态,协调故障转移,并通知客户端更新 VIP(虚拟 IP)。

首先,我们通过一个流程图来直观展示 MHA 架构中各组件的协作关系。

#publish-mermaid-0-1792156965651{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-1792156965651 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792156965651 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-0-1792156965651 .error-icon{fill:#552222;}#publish-mermaid-0-1792156965651 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-0-1792156965651 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-0-1792156965651 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-0-1792156965651 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-0-1792156965651 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-0-1792156965651 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-0-1792156965651 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-0-1792156965651 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-0-1792156965651 .marker.cross{stroke:#333333;}#publish-mermaid-0-1792156965651 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-0-1792156965651 p{margin:0;}#publish-mermaid-0-1792156965651 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-0-1792156965651 .cluster-label text{fill:#333;}#publish-mermaid-0-1792156965651 .cluster-label span{color:#333;}#publish-mermaid-0-1792156965651 .cluster-label span p{background-color:transparent;}#publish-mermaid-0-1792156965651 .label text,#publish-mermaid-0-1792156965651 span{fill:#333;color:#333;}#publish-mermaid-0-1792156965651 .node rect,#publish-mermaid-0-1792156965651 .node circle,#publish-mermaid-0-1792156965651 .node ellipse,#publish-mermaid-0-1792156965651 .node polygon,#publish-mermaid-0-1792156965651 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-0-1792156965651 .rough-node .label text,#publish-mermaid-0-1792156965651 .node .label text,#publish-mermaid-0-1792156965651 .image-shape .label,#publish-mermaid-0-1792156965651 .icon-shape .label{text-anchor:middle;}#publish-mermaid-0-1792156965651 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-0-1792156965651 .rough-node .label,#publish-mermaid-0-1792156965651 .node .label,#publish-mermaid-0-1792156965651 .image-shape .label,#publish-mermaid-0-1792156965651 .icon-shape .label{text-align:center;}#publish-mermaid-0-1792156965651 .node.clickable{cursor:pointer;}#publish-mermaid-0-1792156965651 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-0-1792156965651 .arrowheadPath{fill:#333333;}#publish-mermaid-0-1792156965651 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-0-1792156965651 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-0-1792156965651 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-0-1792156965651 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792156965651 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792156965651 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-0-1792156965651 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-0-1792156965651 .cluster text{fill:#333;}#publish-mermaid-0-1792156965651 .cluster span{color:#333;}#publish-mermaid-0-1792156965651 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-0-1792156965651 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-0-1792156965651 rect.text{fill:none;stroke-width:0;}#publish-mermaid-0-1792156965651 .icon-shape,#publish-mermaid-0-1792156965651 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-0-1792156965651 .icon-shape p,#publish-mermaid-0-1792156965651 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-0-1792156965651 .icon-shape .label rect,#publish-mermaid-0-1792156965651 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-0-1792156965651 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-0-1792156965651 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-0-1792156965651 .node .neo-node{stroke:#9370DB;}#publish-mermaid-0-1792156965651 [data-look="neo"].node rect,#publish-mermaid-0-1792156965651 [data-look="neo"].cluster rect,#publish-mermaid-0-1792156965651 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792156965651 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-0-1792156965651 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-0-1792156965651 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792156965651 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-0-1792156965651 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792156965651 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-0-1792156965651 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792156965651 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-0-1792156965651 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}正常异常客户端请求VIP 路由MasterSlave1Slave2MHA NodeMHA Manager监控 Master 状态记录 Binlog 位置发起故障切换选择新 Master执行数据补偿更新 VIP通知客户端

该图清晰地展示了 MHA 架构中 Master、Slave、MHA Node 和 MHA Manager 之间的数据流向与控制关系。客户端通过 VIP 访问 Master,MHA Manager 负责监控和故障切换,MHA Node 在各数据库节点上执行具体操作。

二、环境准备与基础配置:为高可用铺路

在搭建 MHA 架构之前,需要准备好硬件和软件环境,并进行必要的基础配置。

2.1 环境规划

我们规划三台服务器,分别作为 Master、Slave 和 MHA Manager。为了实现高可用,还需要一个 VIP(虚拟 IP)。

Master:IP 192.168.1.10Slave1:IP 192.168.1.11Slave2:IP 192.168.1.12MHA Manager:IP 192.168.1.20VIP:IP 192.168.1.100

2.2 系统与 MySQL 基础配置

关闭防火墙和 Selinux:确保各节点间网络互通。

```bash

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

```

配置主机名与 hosts 文件:在三台服务器上分别设置主机名,并确保可以通过主机名互相 ping 通。

```bash

hostnamectl set-hostname master

hostnamectl set-hostname slave1

hostnamectl set-hostname slave2

```

编辑 /etc/hosts 文件,添加以下内容:

```

192.168.1.10 master

192.168.1.11 slave1

192.168.1.12 slave2

192.168.1.20 mha-manager

```

安装并配置 MySQL:在三台服务器上安装相同版本的 MySQL,并配置主从复制。确保主从复制功能正常工作。

三、MHA Node 安装与配置:构建自动化切换引擎

MHA Node 是 MHA 架构的基础,需要部署在所有 MySQL 节点上。

3.1 安装 MHA Node

在 Master、Slave1 和 Slave2 节点上执行以下命令安装 MHA Node。

# 下载 MHA Node 软件包

wget https://github.com/yoshinorim/mha4mysql-node/archive/v0.58.tar.gz

tar -xzf v0.58.tar.gz

cd mha4mysql-node-0.58

# 安装依赖

perl Makefile.PL

make

make install

3.2 配置 SSH 免密登录

MHA Manager 需要通过 SSH 连接到所有 MySQL 节点来执行命令,因此需要配置免密登录。

在 MHA Manager 节点上生成 SSH 密钥对。

```bash

ssh-keygen -t rsa

```

将公钥复制到所有 MySQL 节点(Master、Slave1、Slave2)。

```bash

ssh-copy-id -i ~/.ssh/id_rsa.pub root@master

ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1

ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2

```

验证免密登录是否成功。

```bash

ssh root@master "hostname"

ssh root@slave1 "hostname"

ssh root@slave2 "hostname"

```

MHA Node 的配置相对简单,主要任务是确保 Manager 能通过 SSH 无缝管理各个数据库节点。其核心配置流程如下所示。

#publish-mermaid-1-1792156965723{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-1792156965723 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792156965723 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1-1792156965723 .error-icon{fill:#552222;}#publish-mermaid-1-1792156965723 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1-1792156965723 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1-1792156965723 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1-1792156965723 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1-1792156965723 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1-1792156965723 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1-1792156965723 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1-1792156965723 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1-1792156965723 .marker.cross{stroke:#333333;}#publish-mermaid-1-1792156965723 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1-1792156965723 p{margin:0;}#publish-mermaid-1-1792156965723 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-1-1792156965723 .cluster-label text{fill:#333;}#publish-mermaid-1-1792156965723 .cluster-label span{color:#333;}#publish-mermaid-1-1792156965723 .cluster-label span p{background-color:transparent;}#publish-mermaid-1-1792156965723 .label text,#publish-mermaid-1-1792156965723 span{fill:#333;color:#333;}#publish-mermaid-1-1792156965723 .node rect,#publish-mermaid-1-1792156965723 .node circle,#publish-mermaid-1-1792156965723 .node ellipse,#publish-mermaid-1-1792156965723 .node polygon,#publish-mermaid-1-1792156965723 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1-1792156965723 .rough-node .label text,#publish-mermaid-1-1792156965723 .node .label text,#publish-mermaid-1-1792156965723 .image-shape .label,#publish-mermaid-1-1792156965723 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1-1792156965723 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1-1792156965723 .rough-node .label,#publish-mermaid-1-1792156965723 .node .label,#publish-mermaid-1-1792156965723 .image-shape .label,#publish-mermaid-1-1792156965723 .icon-shape .label{text-align:center;}#publish-mermaid-1-1792156965723 .node.clickable{cursor:pointer;}#publish-mermaid-1-1792156965723 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1-1792156965723 .arrowheadPath{fill:#333333;}#publish-mermaid-1-1792156965723 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1-1792156965723 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1-1792156965723 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1-1792156965723 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1-1792156965723 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1-1792156965723 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1-1792156965723 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1-1792156965723 .cluster text{fill:#333;}#publish-mermaid-1-1792156965723 .cluster span{color:#333;}#publish-mermaid-1-1792156965723 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-1-1792156965723 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1-1792156965723 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1-1792156965723 .icon-shape,#publish-mermaid-1-1792156965723 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1-1792156965723 .icon-shape p,#publish-mermaid-1-1792156965723 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1-1792156965723 .icon-shape .label rect,#publish-mermaid-1-1792156965723 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1-1792156965723 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1-1792156965723 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1-1792156965723 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1-1792156965723 [data-look="neo"].node rect,#publish-mermaid-1-1792156965723 [data-look="neo"].cluster rect,#publish-mermaid-1-1792156965723 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792156965723 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-1-1792156965723 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1-1792156965723 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792156965723 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1-1792156965723 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792156965723 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-1-1792156965723 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792156965723 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1-1792156965723 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}安装 MHA Node配置 SSH 免密登录启动 MHA Node 服务验证 Manager 连接

四、MHA Manager 安装与配置:部署监控与切换中心

MHA Manager 是整个架构的大脑,负责监控和决策。

4.1 安装 MHA Manager

在 MHA Manager 节点上执行以下命令安装 MHA Manager。

# 下载 MHA Manager 软件包

wget https://github.com/yoshinorim/mha4mysql-manager/archive/v0.58.tar.gz

tar -xzf v0.58.tar.gz

cd mha4mysql-manager-0.58

# 安装依赖

perl Makefile.PL

make

make install

4.2 配置 MHA Manager

MHA Manager 的核心配置文件是 app.cnf,需要创建并配置。

创建配置文件目录。

```bash

mkdir -p /etc/mha

```

创建 app.cnf 文件。

```bash

vim /etc/mha/app.cnf

```

在 app.cnf 中添加以下内容:

```ini

[server default]

manager_log=/var/log/mha_manager.log

manager_workdir=/var/log/mha_manager

master_binlog_dir=/var/lib/mysql

master_ip_failover_script=/usr/local/bin/master_ip_failover

master_ip_online_change_script=/usr/local/bin/master_ip_online_change

password=your_mysql_root_password

ping_interval=3

remote_workdir=/tmp

repl_password=your_repl_user_password

repl_user=repl_user

secondary_check=1

ssh_user=root

user=root

[server1]

hostname=master

port=3306

candidate_master=1

[server2]

hostname=slave1

port=3306

[server3]

hostname=slave2

port=3306

```

candidate_master=1 表示该节点有资格被选为新 Master。repl_user 和 repl_password 是用于复制的 MySQL 用户。password 是用于登录 MySQL 的 root 密码。

MHA Manager 的配置流程概括了其关键配置步骤,确保其能够正确监控和执行切换操作。

#publish-mermaid-2-1792156965762{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-1792156965762 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792156965762 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-2-1792156965762 .error-icon{fill:#552222;}#publish-mermaid-2-1792156965762 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-2-1792156965762 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-2-1792156965762 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-2-1792156965762 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-2-1792156965762 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-2-1792156965762 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-2-1792156965762 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-2-1792156965762 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-2-1792156965762 .marker.cross{stroke:#333333;}#publish-mermaid-2-1792156965762 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-2-1792156965762 p{margin:0;}#publish-mermaid-2-1792156965762 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#publish-mermaid-2-1792156965762 .cluster-label text{fill:#333;}#publish-mermaid-2-1792156965762 .cluster-label span{color:#333;}#publish-mermaid-2-1792156965762 .cluster-label span p{background-color:transparent;}#publish-mermaid-2-1792156965762 .label text,#publish-mermaid-2-1792156965762 span{fill:#333;color:#333;}#publish-mermaid-2-1792156965762 .node rect,#publish-mermaid-2-1792156965762 .node circle,#publish-mermaid-2-1792156965762 .node ellipse,#publish-mermaid-2-1792156965762 .node polygon,#publish-mermaid-2-1792156965762 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-2-1792156965762 .rough-node .label text,#publish-mermaid-2-1792156965762 .node .label text,#publish-mermaid-2-1792156965762 .image-shape .label,#publish-mermaid-2-1792156965762 .icon-shape .label{text-anchor:middle;}#publish-mermaid-2-1792156965762 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-2-1792156965762 .rough-node .label,#publish-mermaid-2-1792156965762 .node .label,#publish-mermaid-2-1792156965762 .image-shape .label,#publish-mermaid-2-1792156965762 .icon-shape .label{text-align:center;}#publish-mermaid-2-1792156965762 .node.clickable{cursor:pointer;}#publish-mermaid-2-1792156965762 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-2-1792156965762 .arrowheadPath{fill:#333333;}#publish-mermaid-2-1792156965762 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-2-1792156965762 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-2-1792156965762 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-2-1792156965762 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-2-1792156965762 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-2-1792156965762 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-2-1792156965762 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-2-1792156965762 .cluster text{fill:#333;}#publish-mermaid-2-1792156965762 .cluster span{color:#333;}#publish-mermaid-2-1792156965762 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-2-1792156965762 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-2-1792156965762 rect.text{fill:none;stroke-width:0;}#publish-mermaid-2-1792156965762 .icon-shape,#publish-mermaid-2-1792156965762 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-2-1792156965762 .icon-shape p,#publish-mermaid-2-1792156965762 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-2-1792156965762 .icon-shape .label rect,#publish-mermaid-2-1792156965762 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-2-1792156965762 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-2-1792156965762 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-2-1792156965762 .node .neo-node{stroke:#9370DB;}#publish-mermaid-2-1792156965762 [data-look="neo"].node rect,#publish-mermaid-2-1792156965762 [data-look="neo"].cluster rect,#publish-mermaid-2-1792156965762 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792156965762 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-2-1792156965762 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-2-1792156965762 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792156965762 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-2-1792156965762 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792156965762 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-2-1792156965762 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792156965762 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-2-1792156965762 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}安装 MHA Manager创建配置文件目录编写 app.cnf配置 SSH 免密登录启动 MHA Manager 服务监控集群状态

五、故障模拟与自动切换实战:验证高可用性

配置完成后,我们可以启动 MHA Manager 并模拟 Master 节点故障,验证其自动切换能力。

5.1 启动 MHA Manager

在 MHA Manager 节点上启动 MHA Manager。

nohup masterha_manager --conf=/etc/mha/app.cnf --remove_dead_master_conf --ignore_last_failover > /var/log/mha_manager.log 2>&1 &

检查 MHA Manager 状态。

masterha_check_ssh --conf=/etc/mha/app.cnf

masterha_check_repl --conf=/etc/mha/app.cnf

5.2 模拟 Master 故障

在 Master 节点上模拟故障,例如杀死 MySQL 进程。

ssh root@master "pkill -9 mysqld"

当 Master 节点故障时,MHA 的自动切换流程如下所示。

VIP ScriptClientSlave2Slave1MasterMHA ManagerVIP ScriptClientSlave2Slave1MasterMHA Manager#publish-mermaid-3-1792156965798{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-1792156965798 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792156965798 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-3-1792156965798 .error-icon{fill:#552222;}#publish-mermaid-3-1792156965798 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-3-1792156965798 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-3-1792156965798 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-3-1792156965798 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-3-1792156965798 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-3-1792156965798 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-3-1792156965798 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-3-1792156965798 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-3-1792156965798 .marker.cross{stroke:#333333;}#publish-mermaid-3-1792156965798 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-3-1792156965798 p{margin:0;}#publish-mermaid-3-1792156965798 .actor{stroke:#9370DB;fill:#ECECFF;stroke-width:1;}#publish-mermaid-3-1792156965798 rect.actor.outer-path[data-look="neo"]{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 rect.note[data-look="neo"]{stroke:#aaaa33;fill:#fff5ad;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 text.actor>tspan{fill:black;stroke:none;}#publish-mermaid-3-1792156965798 .actor-line{stroke:#9370DB;}#publish-mermaid-3-1792156965798 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#publish-mermaid-3-1792156965798 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#publish-mermaid-3-1792156965798 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#publish-mermaid-3-1792156965798 [id$="-arrowhead"] path{fill:#333;stroke:#333;}#publish-mermaid-3-1792156965798 .sequenceNumber{fill:white;}#publish-mermaid-3-1792156965798 [id$="-sequencenumber"]{fill:#333;}#publish-mermaid-3-1792156965798 [id$="-crosshead"] path{fill:#333;stroke:#333;}#publish-mermaid-3-1792156965798 .messageText{fill:#333;stroke:none;}#publish-mermaid-3-1792156965798 .labelBox{stroke:#9370DB;fill:#ECECFF;filter:none;}#publish-mermaid-3-1792156965798 .labelText,#publish-mermaid-3-1792156965798 .labelText>tspan{fill:black;stroke:none;}#publish-mermaid-3-1792156965798 .loopText,#publish-mermaid-3-1792156965798 .loopText>tspan{fill:black;stroke:none;}#publish-mermaid-3-1792156965798 .sectionTitle,#publish-mermaid-3-1792156965798 .sectionTitle>tspan{fill:black;stroke:none;}#publish-mermaid-3-1792156965798 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:#9370DB;fill:#9370DB;}#publish-mermaid-3-1792156965798 .note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-3-1792156965798 .noteText,#publish-mermaid-3-1792156965798 .noteText>tspan{fill:black;stroke:none;font-weight:normal;}#publish-mermaid-3-1792156965798 .activation0{fill:#f4f4f4;stroke:#666;}#publish-mermaid-3-1792156965798 .activation1{fill:#f4f4f4;stroke:#666;}#publish-mermaid-3-1792156965798 .activation2{fill:#f4f4f4;stroke:#666;}#publish-mermaid-3-1792156965798 .actorPopupMenu{position:absolute;}#publish-mermaid-3-1792156965798 .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-3-1792156965798 .actor-man circle,#publish-mermaid-3-1792156965798 line{fill:#ECECFF;stroke-width:2px;}#publish-mermaid-3-1792156965798 g rect.rect{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));stroke:#9370DB;}#publish-mermaid-3-1792156965798 .node .neo-node{stroke:#9370DB;}#publish-mermaid-3-1792156965798 [data-look="neo"].node rect,#publish-mermaid-3-1792156965798 [data-look="neo"].cluster rect,#publish-mermaid-3-1792156965798 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-3-1792156965798 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-3-1792156965798 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-3-1792156965798 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-3-1792156965798 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-3-1792156965798 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}检测到故障通知故障通知故障选择为新 Master执行数据补偿同步新 Master 信息更新 VIP 到 Slave1客户端通过新 VIP 访问

这个时序图详细描绘了 MHA 从检测到故障到完成切换的完整自动化流程,确保了服务的高可用性。

5.3 验证切换结果

检查新 Master:在 Slave1 节点上查看是否已成为新的 Master。

```bash

mysql -u root -p -e "SHOW MASTER STATUS;"

```

检查 VIP:在 MHA Manager 节点上检查 VIP 是否已切换到 Slave1。

```bash

ip a | grep 192.168.1.100

```

检查日志:查看 MHA Manager 的日志文件,确认切换过程。

```bash

tail -f /var/log/mha_manager.log

```

六、总结与优化建议:提升 MHA 架构稳定性

通过以上步骤,我们成功搭建了 MySQL MHA 高可用架构,并验证了其自动故障切换功能。MHA 提供了强大的自动化能力,但在生产环境中,我们还可以通过以下方式进行优化:

监控与告警:将 MHA Manager 的状态和切换事件接入监控系统(如 Zabbix、Prometheus),并设置告警。VIP 脚本优化:默认的 master_ip_failover 脚本功能简单,可以根据实际网络环境(如 Keepalived)进行定制。定期演练:定期进行故障切换演练,确保 MHA 配置和脚本的有效性。数据一致性检查:虽然 MHA 保证了数据一致性,但仍建议定期进行数据校验。

我们可以用状态图来表示 Master 节点在 MHA 架构中的不同状态及其转换条件,帮助我们理解其生命周期。

#publish-mermaid-4-1792156965826{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-1792156965826 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792156965826 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-4-1792156965826 .error-icon{fill:#552222;}#publish-mermaid-4-1792156965826 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-4-1792156965826 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-4-1792156965826 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-4-1792156965826 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-4-1792156965826 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-4-1792156965826 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-4-1792156965826 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-4-1792156965826 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792156965826 .marker.cross{stroke:#333333;}#publish-mermaid-4-1792156965826 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-4-1792156965826 p{margin:0;}#publish-mermaid-4-1792156965826 defs [id$="-barbEnd"]{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792156965826 g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#publish-mermaid-4-1792156965826 g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#publish-mermaid-4-1792156965826 g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#publish-mermaid-4-1792156965826 g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#publish-mermaid-4-1792156965826 g.stateGroup line{stroke:#333333;stroke-width:1;}#publish-mermaid-4-1792156965826 .transition{stroke:#333333;stroke-width:1;fill:none;}#publish-mermaid-4-1792156965826 .stateGroup .composit{fill:white;border-bottom:1px;}#publish-mermaid-4-1792156965826 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#publish-mermaid-4-1792156965826 .state-note{stroke:#aaaa33;fill:#fff5ad;}#publish-mermaid-4-1792156965826 .state-note text{fill:black;stroke:none;font-size:10px;}#publish-mermaid-4-1792156965826 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#publish-mermaid-4-1792156965826 .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#publish-mermaid-4-1792156965826 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-4-1792156965826 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792156965826 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-4-1792156965826 .edgeLabel .label text{fill:#333;}#publish-mermaid-4-1792156965826 .label div .edgeLabel{color:#333;}#publish-mermaid-4-1792156965826 .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#publish-mermaid-4-1792156965826 .node circle.state-start{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792156965826 .node .fork-join{fill:#333333;stroke:#333333;}#publish-mermaid-4-1792156965826 .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#publish-mermaid-4-1792156965826 .end-state-inner{fill:white;stroke-width:1.5;}#publish-mermaid-4-1792156965826 .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792156965826 .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792156965826 [id$="-barbEnd"]{fill:#333333;}#publish-mermaid-4-1792156965826 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792156965826 .cluster-label,#publish-mermaid-4-1792156965826 .nodeLabel{color:#131300;}#publish-mermaid-4-1792156965826 .statediagram-cluster rect.outer{rx:5px;ry:5px;}#publish-mermaid-4-1792156965826 .statediagram-state .divider{stroke:#9370DB;}#publish-mermaid-4-1792156965826 .statediagram-state .title-state{rx:5px;ry:5px;}#publish-mermaid-4-1792156965826 .statediagram-cluster.statediagram-cluster .inner{fill:white;}#publish-mermaid-4-1792156965826 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#publish-mermaid-4-1792156965826 .statediagram-cluster .inner{rx:0;ry:0;}#publish-mermaid-4-1792156965826 .statediagram-state rect.basic{rx:5px;ry:5px;}#publish-mermaid-4-1792156965826 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#publish-mermaid-4-1792156965826 .note-edge{stroke-dasharray:5;}#publish-mermaid-4-1792156965826 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#publish-mermaid-4-1792156965826 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#publish-mermaid-4-1792156965826 .statediagram-note text{fill:black;}#publish-mermaid-4-1792156965826 .statediagram-note .nodeLabel{color:black;}#publish-mermaid-4-1792156965826 .statediagram .edgeLabel{color:red;}#publish-mermaid-4-1792156965826 [id$="-dependencyStart"],#publish-mermaid-4-1792156965826 [id$="-dependencyEnd"]{fill:#333333;stroke:#333333;stroke-width:1;}#publish-mermaid-4-1792156965826 .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-4-1792156965826 [data-look="neo"].statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1;}#publish-mermaid-4-1792156965826 [data-look="neo"].statediagram-cluster rect.outer{rx:5px;ry:5px;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792156965826 .node .neo-node{stroke:#9370DB;}#publish-mermaid-4-1792156965826 [data-look="neo"].node rect,#publish-mermaid-4-1792156965826 [data-look="neo"].cluster rect,#publish-mermaid-4-1792156965826 [data-look="neo"].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792156965826 [data-look="neo"].swimlane.cluster rect{filter:none;}#publish-mermaid-4-1792156965826 [data-look="neo"].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-4-1792156965826 [data-look="neo"].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792156965826 [data-look="neo"].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-4-1792156965826 [data-look="neo"].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792156965826 [data-look="neo"].node circle .state-start{fill:#000000;}#publish-mermaid-4-1792156965826 [data-look="neo"].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792156965826 [data-look="neo"].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-4-1792156965826 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Master 宕机检测超时MHA 切换完成新 Master 稳定正常运行故障检测故障故障恢复

状态图帮助我们理解 Master 节点在不同阶段的状态变化,以及触发这些变化的条件,从而更好地进行故障排查和预防。

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧