首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

MHA MySQL 高可用故障轉移:從原理到生產級部署的超詳細實戰

     編輯:MySQL 資料庫相關知識

MHA (Master High Availability) 是由日本 MySQL 專家松信嘉範開發的 Perl 故障轉移框架,在 10-30 秒內完成主庫故障檢測、差異 Relay Log 補齊、新主提升、VIP 漂移,實現近零數據丟失的高可用。本文涵蓋架構原理、三節點部署、SSH 互信、權限配置、Manager/Node 安裝、故障轉移腳本、VIP 漂移驗證的完整實戰。

MHA 核心優勢

特性說明
秒級故障轉移9-12s 檢測 + 7s 電源隔離 + 10-30s 恢復 = 總停機 < 30s
自動一致性修復從最新 Slave 補齊差異 Relay Log,所有 Slave 統一到同一 GTID/位點
在線切主0.5-2s 只阻塞寫,讀不受影響,適合硬件維護窗口
無侵入部署不修改 MySQL 配置,支援 5.0+/5.5/5.6/5.7,任意存儲引擎
低資源佔用Manager 單獨部署或共置 Slave,3s 心跳 ping,無大查詢

實驗環境

  • OS:CentOS 6.4 x64
  • MySQL:5.6.16 二進制包
  • MHA:mha4mysql-manager-0.54 / mha4mysql-node-0.54
  • 拓撲:Manager(192.168.216.50)、Master(51)、Slave(52)

SSH 雙向免密(三節點互信)

# 每台執行
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.216.50
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.216.51
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.216.52
# 驗證
ssh 192.168.216.50 date && ssh 192.168.216.51 date && ssh 192.168.216.52 date

MySQL 安裝與配置(三節點同步)

groupadd mysql; useradd -g mysql -s /sbin/nologin -M mysql
mkdir -p /data/mysql
tar -zxvf mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.16-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql/scripts
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
# /usr/local/mysql/my.cnf 關鍵差異:server_id
[mysqld]
user=mysql
default-storage-engine=InnoDB
socket=/tmp/mysql.sock
pid-file=/data/mysql/mysql.pid
datadir=/data/mysql
server_id=1  # 51→1, 52→2, 50→3
log-bin=/data/mysql/mysql-bin
expire-logs-days=14
sync-binlog=1
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=592M
slow_query_log=1
slow_query_log_file=/data/mysql/mysql-slow.log
log_queries_not_using_indexes=1

權限授予(三節點同步執行)

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.216.50' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.216.51' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.216.52' IDENTIFIED BY 'root';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.216.50' IDENTIFIED BY 'slave';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.216.51' IDENTIFIED BY 'slave';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.216.52' IDENTIFIED BY 'slave';

Perl 依賴包安裝

# Manager 節點全裝
yum -y install perl-CPAN
cpan -i Config::Tiny Params::Validate Parallel::ForkManager Log::Dispatch MIME::Lite Mail::Sender Net::SSLeay IO::Socket::SSL

# Node 節點輕量
yum -y install perl-DBD-MySQL
# 若 rpm 衝突,手工編譯 DBD::mysql
tar -zxvf DBD-mysql-4.027.tar.gz
cd DBD-mysql-4.027; perl Makefile.PL; make && make install

軟連接與庫路徑

ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql-x86_64.conf
ldconfig

MHA Node 安裝(三節點)

tar -zxvf mha4mysql-node-0.54.tar.gz
cd mha4mysql-node-0.54
perl Makefile.PL; make && make install

MHA Manager 安裝與配置

tar -zxvf mha4mysql-manager-0.54.tar.gz
cd mha4mysql-manager-0.54
perl Makefile.PL; make && make install
mkdir -p /etc/masterha /masterha/app1
cp samples/conf/* /etc/masterha
cp samples/scripts/* /usr/local/bin
# /etc/masterha/app1.cnf
[server default]
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
user=root
password=root
ssh_user=root
repl_user=slave
repl_password=slave
shutdown_script=""
master_ip_failover_script=/usr/local/bin/masterha_ip_failover
master_ip_online_change_script=/usr/local/bin/masterha_ip_failover
report_script=""

[server1]
hostname=192.168.216.50
master_binlog_dir=/data/mysql/
candidate_master=1

[server2]
hostname=192.168.216.51
master_binlog_dir=/data/mysql/
candidate_master=1

[server3]
hostname=192.168.216.52
master_binlog_dir=/data/mysql/
candidate_master=1

驗證 SSH 與復制

masterha_check_ssh --conf=/etc/masterha/app1.cnf
masterha_check_repl --conf=/etc/masterha/app1.cnf

啟動 Manager

nohup masterha_manager --conf=/etc/masterha/app1.cnf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1 &

VIP 漂移腳本 /usr/local/bin/masterha_ip_failover

#!/usr/bin/env perl
use strict; use warnings FATAL => 'all'; use Getopt::Long;
my ($command,$ssh_user,$orig_master_host,$orig_master_ip,$orig_master_port,
    $new_master_host,$new_master_ip,$new_master_port);
my $vip = '192.168.216.100/24';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip  = "/sbin/ifconfig eth0:$key down";

GetOptions('command=s'=>\$command,'ssh_user=s'=>\$ssh_user,
  'orig_master_host=s'=>\$orig_master_host,'orig_master_ip=s'=>\$orig_master_ip,
  'orig_master_port=i'=>\$orig_master_port,'new_master_host=s'=>\$new_master_host,
  'new_master_ip=s'=>\$new_master_ip,'new_master_port=i'=>\$new_master_port);

exit main();
sub main {
  if ($command eq 'stop' || $command eq 'stopssh') {
    print "Disabling VIP on old master: $orig_master_host\n";
    system("ssh $ssh_user\@$orig_master_host \"$ssh_stop_vip\"");
    exit $? >> 8;
  } elsif ($command eq 'start') {
    print "Enabling VIP on new master: $new_master_host\n";
    system("ssh $ssh_user\@$new_master_host \"$ssh_start_vip\"");
    exit $? >> 8;
  } elsif ($command eq 'status') {
    system("ssh $ssh_user\@$orig_master_host \"$ssh_start_vip\"");
    exit 0;
  } else { die "Usage: ..."; }
}
chmod +x /usr/local/bin/masterha_ip_failover
# 測試
/usr/local/bin/masterha_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.216.51 --orig_master_ip=192.168.216.51 --orig_master_port=3306

故障演練與驗證

  1. 手動 service mysql stop 在 Master (51)
  2. 觀察 Manager 日志:檢測 → 選主 (52) → 補齊 Relay Log → VIP 漂移 → 完成
  3. 應用端連接 VIP (192.168.216.100) 驗證業務無感切換

生產化補強

  • 半同步復制:rpl_semi_sync_master=ON + rpl_semi_sync_slave=ON,極大降低數據丟失窗口。
  • 多 Manager 互備:Keepalived/VRRP 保護 Manager 自身高可用。
  • 監控告警:Prometheus 抓取 MHA 狀態,Grafana 儀表板 + Alertmanager 告警。
  • 遷移建議:MySQL 8.0+ 環境評估 MGR (Group Replication) 或 Orchestrator,原生高可用、無外部依賴。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved