MHA高可用架構的實現方式
VIP:10.0.3.200/24 | |||
角色 | IP | server_id | 許可權 |
master | 10.0.3.105 | 105 | 寫 |
slave(備用master) | 10.0.3.115 | 115 | 讀 |
manager(slave2) | 10.0.3.112 | 112 | 讀 |
系統版本:centos7.8 | |||
資料庫版本:MySQL5.7.30 | |||
MHA版本:mha0.58 |
一 mysql主從搭建
。。。略(一主兩從GTID)
二 MHA基本環境準備
1.配置三臺機器的ssh互信(三臺機器都要操作)
ssh-keygen -t rsa ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.3.105 ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.3.115 ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.3.112 # 測試是否成功 ssh 10.0.3.105 date ssh 10.0.3.115 date ssh 10.0.3.112 date
2.繫結VIP
先在主庫master上繫結VIP(只需手工繫結一次,後續指令碼會自動切換)
ifconfig ens192:1 10.0.3.200/24
3.安裝MHA軟體
專案地址: https://github.com/yoshinorim/mha4mysql-manager/releases/tag/v0.58
三臺機器上都要安裝mha的node軟體
先安裝依賴
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager -y
安裝node軟體
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
僅在manager機器上安裝mha管理軟體
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
三 配置MHA(在manager機器上操作10.0.3.112)
3.建立目錄
mkdir -p /etc/mha/scripts mkdir -p /var/log/mha/app1
4.編寫全域性配置檔案
cat /etc/masterha_default.cnf #一定要是這個路徑,不然後期masterha_check_ssh會提示未找到全域性檔案 [server default] user=mha password=mha ssh_user=root repl_user=repl repl_password=123 ping_interval=3 secondary_check_script=masterha_secondary_check -s 10.0.3.105 -s 10.0.3.115 -s 10.0.3.112 master_ip_failover_script="/etc/mha/scripts/master_ip_failover" master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change" report_script="/etc/mha/scripts/send_report"
5.編寫主配置檔案
cat /etc/mha/app1.cnf [server default] manager_log=/var/log/mha/app1/manager.log manager_workdir=/var/log/mha/app1 [server1] candidate_master=1 hostname=10.0.3.105 master_binlog_dir="/data/mysql/binlog" [server2] candidate_master=1 hostname=10.0.3.115 master_binlog_dir="/data/mysql/binlog" [server3] hostname=10.0.3.112 master_binlog_dir="/data/mysql/binlog" no_master=1
6.配置VIP
#為了防止腦裂發生,推薦生產環境採用指令碼的方式來管理虛擬 ip,而不是使用 keepalived來完成。 cat /etc/mha/scripts/master_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 ); #定義VIP變數 my $vip = '10.0.3.200/24'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig ens192:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig ens192:$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 { print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; if ( $command eq "stop" || $command eq "stopssh" ) { my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host \n"; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host \n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK \n"; exit 0; } else { &usage(); exit 1; } } sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } sub stop_vip() { return 0 unless ($ssh_user); `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
7.配置微信機器人報警指令碼
cat /etc/mha/scripts/send_report #!/bin/bash source /root/.bash_profile # 解析變數 orig_master_host=`echo "$1" | awk -F = '{print $2}'` new_master_host=`echo "$2" | awk -F = '{print $2}'` new_slave_hosts=`echo "$3" | awk -F = '{print $2}'` subject=`echo "$4" | awk -F = '{print $2}'` body=`echo "$5" | awk -F = '{print $2}'` tac /var/log/mha/app1/manager.log | sed -n 2p | grep 'successfully' > /dev/null if [ $? -eq 0 ] then messages=`echo -e "MHA $subject 主從切換成功\n master:$orig_master_host --> $new_master_host \n $body \n 當前從庫:$new_slave_hosts"` TOKEN="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bb09197b-xxxx-xxxx-xxxx-aa5fa8802689" DING="curl -H \"Content-Type: application/json\" -X POST --data '{\"msgtype\": \"text\", \"text\": {\"content\": \"${messages}\"},\"isAtAll\": false}}' ${TOKEN}" eval $DING echo "$messages" >>/tmp/gaojin.log 2>&1 else messages=`echo -e "MHA $subject 主從切換失敗\n master:$orig_master_host --> $new_master_host \n $body" ` TOKEN="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bb09197b-xxxx-xxxx-xxxx-aa5fa8802689" DING="curl -H \"Content-Type: application/json\" -X POST --data '{\"msgtype\": \"text\", \"text\": {\"content\": \"${messages}\"},\"isAtAll\": false}}' ${TOKEN}" eval $DING echo "$messages" >>/tmp/gaojin.log 2>&1 fi
8.配置編寫VIP指令碼
cat /etc/mha/scripts/master_ip_online_change #!/bin/bash source /root/.bash_profile vip=`echo '10.0.3.200/24'` #設定VIP key=`echo '1'` command=`echo "$1" | awk -F = '{print $2}'` orig_master_host=`echo "$2" | awk -F = '{print $2}'` new_master_host=`echo "$7" | awk -F = '{print $2}'` orig_master_ssh_user=`echo "${12}" | awk -F = '{print $2}'` new_master_ssh_user=`echo "${13}" | awk -F = '{print $2}'` #要求服務的網路卡識別名一樣,都為ens192(這裡是) stop_vip=`echo "ssh root@$orig_master_host /usr/sbin/ifconfig ens192:$key down"` start_vip=`echo "ssh root@$new_master_host /usr/sbin/ifconfig ens192:$key $vip"` if [ $command = 'stop' ] then echo -e "\n\n\n****************************\n" echo -e "Disabled thi VIP - $vip on old master: $orig_master_host \n" $stop_vip if [ $? -eq 0 ] then echo "Disabled the VIP successfully" else echo "Disabled the VIP failed" fi echo -e "***************************\n\n\n" fi if [ $command = 'start' -o $command = 'status' ] then echo -e "\n\n\n*************************\n" echo -e "Enabling the VIP - $vip on new master: $new_master_host \n" $start_vip if [ $? -eq 0 ] then echo "Enabled the VIP successfully" else echo "Enabled the VIP failed" fi echo -e "***************************\n\n\n" fi
9.賦予指令碼執行許可權
chmod -R 744 /etc/mha/scripts
四 執行MHA
10.使用 masterha_check_ssh 命令檢查 ssh 互信是否成功
masterha_check_ssh --conf=/etc/mha/app1.cnf
All SSH connection tests passed successfully. #輸出這個表示ssh檢查通過
11. 使用 masterha_check_repl 命令檢查 mysql 主從是否正常
masterha_check_repl --conf=/etc/mha/app1.cnf
MySQL Replication Health is OK. #輸出這個代表主從檢查通過
12.最後一步啟動MHA
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
13.通過日誌檢查MHA是否啟動成功
tail -f /var/log/masterha/app1/manager.log
Ping(SELECT) succeeded, waiting until MySQL doesn't respond.. #最後一行出現如下字樣表明啟動成功
14.檢查MHA叢集狀態
[root@mha ~]# masterha_check_status --conf=/etc/mha/app1.cnf app1 (pid:76925) is running(0:PING_OK), master:10.0.3.105
五 故障模擬
15.手動結束主庫mysql程式(10.0.3.105)
systemctl stop mysqld.service
16.微信收到監控報警資訊
17.檢視MHA日誌,瞭解整個整個切換過程(10.0.3.112)
cat /var/log/mha/app1/manager.log
18.觀察切換完成後變化
1、vip 自動從原來的 master 切換到新的 master。 2、manager 節點的MHA監控程式自動退出(一次性高可用,需要手動恢復)。 3、/etc/mha/app1.cnf 配置檔案中原來老的 master 配置節點被刪除。
六 將修復好的節點重新加入叢集
19.修復mysql主從關係
在MHA日誌中尋找 CHANGE MASTER TO 資訊
cat /var/log/mha/app1/manager.log
Mon Aug 31 16:54:59 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.3.105', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
在修復的故障節點中執行如上 CHANGE MASTER TO 資訊,'xxx'為主從複製的密碼
mysql> CHANGE MASTER TO MASTER_HOST='10.0.3.105', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx'; mysql> start slave; mysql> show slave status\G;
20.修改manger配置檔案
將修復好的節點資訊重新加入到配置檔案中
vim/etc/mha/app1.conf [server1] candidate_master=1 hostname=10.0.3.105 master_binlog_dir="/data/mysql/binlog"
21.重新檢查啟動MHA
masterha_check_ssh --conf=/etc/mha/app1.cnf masterha_check_repl --conf=/etc/mha/app1.cnf nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 & masterha_check_status --conf=/etc/mha/app1.cnf
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69982604/viewspace-2716136/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 實現高可用架構之 MHAMySql架構
- 構建MHA實現MySQL高可用叢集架構MySql架構
- mysql高可用架構MHA搭建MySql架構
- MySQL資料庫實現高可用架構之MHA的實戰MySql資料庫架構
- MySQL高可用架構之MHA 原理與實踐MySql架構
- MySQL高可用架構-MMM、MHA、MGR、PXCMySql架構
- 部署MHA+keepalived+ProxySQL高可用架構SQL架構
- MySQL 高可用架構 - MHA環境部署記錄MySql架構
- MySQL叢集架構:MHA+MySQL-PROXY+LVS實現MySQL叢集架構高可用/高效能MySql架構
- mysql高可用架構MHA搭建(centos7+mysql5.7.28)MySql架構CentOS
- MySQL高可用架構:mysql+keepalived實現MySql架構
- 【DB寶42】MySQL高可用架構MHA+ProxySQL實現讀寫分離和負載均衡MySql架構負載
- MHA+MySQL主從配置實現MySQL高可用MySql
- MHA+ProxySQL實現讀寫分離高可用SQL
- MHA高可用架構工作原理?主庫當機處理過程架構
- MHA高可用+VIP漂移
- Mysql 5.7 MHA 高可用MySql
- 高可用架構架構
- ELK架構下利用Kafka Group實現Logstash的高可用架構Kafka
- 基於 MHA 高可用的 MySQLMySql
- MySQL 高可用架構之 MMM 架構MySql架構
- Mysql高可用架構方案MySql架構
- Canal高可用架構部署架構
- MHA高可用配置及故障切換
- MySQL高可用架構對比MySql架構
- AWS 高可用AWS架構方案架構
- MySQL高可用架構案例篇:UCloud最佳實踐MySql架構Cloud
- 高效能,高可用,安全的架構架構
- 會員服務在高可用架構的實戰探索架構
- Mysql 高可用(MHA)-讀寫分離(Atlas)MySql
- MySQL高可用架構設計分析MySql架構
- k8s高可用架構K8S架構
- 深度解析KubeEdge EdgeMesh 高可用架構架構
- MQ系列9:高可用架構分析MQ架構
- 同程旅行基於 RocketMQ 高可用架構實踐MQ架構
- 如何做高可用的架構設計?架構
- 高可用架構設計全面詳解(8大高可用方案)架構
- 用 Hystrix 構建高可用服務架構架構