一、MHA介紹
MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,它由日本DeNA公司youshimaton(現就職于Facebook公司)開發,是日本的一位MySQL專家采用Perl語言編寫的一個腳本管理工具,該工具僅适用于MySQLReplication(二層)環境,目的在于維持Master主庫的高可用性。是一套優秀的作為MySQL高可用性環境下故障切換和主從提升的高可用軟體。在MySQL故障切換過程中,MHA能做到在0~30秒之内自動完成資料庫的故障切換操作,并且在進行故障切換的過程中,MHA能在最大程度上保證資料的一緻性,以達到真正意義上的高可用。
MHA是自動的master故障轉移和Slave提升的軟體包.它是基于标準的MySQL複制(異步/半同步).該軟體由兩部分組成:MHA Manager(管理節點)和MHA Node(資料節點)。
1. MHA Manager可以單獨部署在一台獨立的機器上管理多個master-slave叢集,也可以部署在一台slave節點上。MHA Manager會定時探測叢集中的node節點,當發現master出現故障的時候,它可以自動将具有最新資料的slave提升為新的master,然後将所有其它的slave導向新的master上.整個故障轉移過程對應用程式是透明的。
2. MHA Node運作在每台MySQL伺服器上,它通過監控具備解析和清理logs功能的腳本來加快故障轉移的。
在MHA自動故障切換過程中,MHA試圖從當機的主伺服器上儲存二進制日志,最大程度的保證資料的不丢失,但這并不總是可行的。例如,如果主伺服器硬體故障或無法通過ssh通路,MHA沒法儲存二進制日志,隻進行故障轉移而丢失了最新的資料。使用MySQL 5.5的半同步複制,可以大大降低資料丢失的風險。MHA可以與半同步複制結合起來。如果隻有一個slave已經收到了最新的二進制日志,MHA可以将最新的二進制日志應用于其他所有的slave伺服器上,是以可以保證所有節點的資料一緻性。
目前MHA主要支援一主多從的架構,要搭建MHA,要求一個複制叢集中必須最少有三台資料庫伺服器,一主二從,即一台充當master,一台充當備用master,另外一台充當從庫,因為至少需要三台伺服器,出于機器成本的考慮,淘寶也在該基礎上進行了改造,目前淘寶TMHA已經支援一主一從。
二、MHA工作架構說明
展示了如何通過MHA Manager管理多組主從複制。可以将MHA工作原理總結為如下:

相較于其它HA軟體,MHA的目的在于維持MySQL Replication中Master庫的高可用性,其最大特點是可以修複多個Slave之間的差異日志,最終使所有Slave保持資料一緻,然後從中選擇一個充當新的Master,并将其它Slave指向它。工作流程主要如下:
1. 從當機崩潰的master儲存二進制日志事件(binlog events);
2. 識别含有最新更新的slave;
3. 應用差異的中繼日志(relay log)到其他的slave;
4. 應用從master儲存的二進制日志事件(binlog events);
5. 提升一個slave為新的master;
6. 使其他的slave連接配接新的master進行複制;
################ MHA工作原理 ###############
當master出現故障時,通過對比slave之間I/O線程讀取master binlog的位置,選取最接近的slave做為latest slave。其它slave通過與latest slave對比生成差異中繼日志。在latest slave上應用從master儲存的binlog,同時将latest slave提升為master。最後在其它slave上應用相應的差異中繼日志并開始從新的master開始複制。
在MHA實作Master故障切換過程中,MHA Node會試圖通路故障的master(通過SSH),如果可以通路(不是硬體故障,比如InnoDB資料檔案損壞等),會儲存二進制檔案,以最大程度保證資料不丢失。MHA和半同步複制一起使用會大大降低資料丢失的危險。
############ MHA軟體的架構 ########### 由兩部分組成,Manager工具包 和 Node工具包,具體的說明如下。
1. Manager工具包主要包括以下幾個工具:
masterha_check_ssh # 檢查MHA的SSH配置狀況
masterha_check_repl # 檢查MySQL複制狀況
masterha_manger # 啟動MHA
masterha_check_status # 檢測目前MHA運作狀态
masterha_master_monitor # 檢測master是否當機
masterha_master_switch # 控制故障轉移(自動或者手動)
masterha_conf_host # 添加或删除配置的server資訊
2. Node工具包(這些工具通常由MHA Manager的腳本觸發,無需人為操作)主要包括以下幾個工具:
save_binary_logs #(儲存二進制日志) 儲存和複制master的二進制日志
apply_diff_relay_logs # (應用差異中繼日志) 識别差異的中繼日志事件并将其差異的事件應用于其他的slave
filter_mysqlbinlog # 去除不必要的ROLLBACK事件(MHA已不再使用這個工具)
purge_relay_logs # (清理中繼日志) 清除中繼日志(不會阻塞SQL線程)
########## MHA如何保持資料的一緻性呢?######### 主要通過MHA node的以下幾個工具實作,但是這些工具由mha manager觸發:
save_binary_logs 如果master的二進制日志可以存取的話,儲存複制master的二進制日志,最大程度保證資料不丢失
apply_diff_relay_logs 相對于最新的slave,生成差異的中繼日志并将所有差異事件應用到其他所有的slave
注意:
對比的是relay log,relay log越新就越接近于master,才能保證資料是最新的。
purge_relay_logs删除中繼日志而不阻塞sql線程
################# MHA的優勢 ##################
1. 故障切換快
在主從複制叢集中,隻要從庫在複制上沒有延遲,MHA通常可以在數秒内實作故障切換。9-10秒内檢查到master故障,可以選擇在7-10秒關閉master以避免出現裂腦,幾秒鐘内,将差異中繼日志(relay log)應用到新的master上,是以總的當機時間通常為10-30秒。恢複新的master後,MHA并行的恢複其餘的slave。即使在有數萬台slave,也不會影響master的恢複時間。
DeNA在超過150個MySQL(主要5.0/5.1版本)主從環境下使用了MHA。當mater故障後,MHA在4秒内就完成了故障切換。在傳統的主動/被動叢集解決方案中,4秒内完成故障切換是不可能的。
2. master故障不會導緻資料不一緻
當目前的master出現故障時,MHA自動識别slave之間中繼日志(relay log)的不同,并應用到所有的slave中。這樣所有的salve能夠保持同步,隻要所有的slave處于存活狀态。和Semi-Synchronous Replication一起使用,(幾乎)可以保證沒有資料丢失。
3. 無需修改目前的MySQL設定
MHA的設計的重要原則之一就是盡可能地簡單易用。MHA工作在傳統的MySQL版本5.0和之後版本的主從複制環境中。和其它高可用解決方法比,MHA并不需要改變MySQL的部署環境。MHA适用于異步和半同步的主從複制。
啟動/停止/更新/降級/安裝/解除安裝MHA不需要改變(包擴啟動/停止)MySQL複制。當需要更新MHA到新的版本,不需要停止MySQL,僅僅替換到新版本的MHA,然後重新開機MHA Manager就好了。
MHA運作在MySQL 5.0開始的原生版本上。一些其它的MySQL高可用解決方案需要特定的版本(比如MySQL叢集、帶全局事務ID的MySQL等等),但并不僅僅為了master的高可用才遷移應用的。在大多數情況下,已經部署了比較舊MySQL應用,并且不想僅僅為了實作Master的高可用,花太多的時間遷移到不同的存儲引擎或更新的前沿發行版。MHA工作的包括5.0/5.1/5.5的原生版本的MySQL上,是以并不需要遷移。
4. 無需增加大量的伺服器
MHA由MHA Manager和MHA Node組成。MHA Node運作在需要故障切換/恢複的MySQL伺服器上,是以并不需要額外增加伺服器。MHA Manager運作在特定的伺服器上,是以需要增加一台(實作高可用需要2台),但是MHA Manager可以監控大量(甚至上百台)單獨的master,是以,并不需要增加大量的伺服器。即使在一台slave上運作MHA Manager也是可以的。綜上,實作MHA并沒用額外增加大量的服務。
5. 無性能下降
MHA适用與異步或半同步的MySQL複制。監控master時,MHA僅僅是每隔幾秒(預設是3秒)發送一個ping包,并不發送重查詢。可以得到像原生MySQL複制一樣快的性能。
6. 适用于任何存儲引擎
MHA可以運作在隻要MySQL複制運作的存儲引擎上,并不僅限制于InnoDB,即使在不易遷移的傳統的MyISAM引擎環境,一樣可以使用MHA。
三、MHA高可用環境部署記錄
1. 機器環境
ip位址 主機名 角色
182.48.115.236 Node_Master 寫入,資料節點
182.48.115.237 Node_Slave 讀,資料節點,備選Master(candicate master)
182.48.115.238 Manager_Slave 讀,資料節點,也作為Manager server(即也作為manager節點)
........................................................................................................
為了節省機器,這裡選擇隻讀的從庫182.48.115.237(從庫不對外提供讀的服務)作為候選主庫,即candicate master,或是專門用于備份
同樣,為了節省機器,這裡選擇182.48.115.238這台從庫作為manager server(實際生産環節中,機器充足的情況下, 一般是專門選擇一台機器作為Manager server)
........................................................................................................
關閉三台機器的iptables和selinux
部署節點之間ssh無密碼登陸的信任關系(即在所有節點上做ssh免密碼登入,包括對節點本機的信任)
[root@Node_Master ~]# ssh-copy-id 182.48.115.236
[root@Node_Master ~]# ssh-copy-id 182.48.115.237
[root@Node_Master ~]# ssh-copy-id 182.48.115.238
[root@Node_Slave ~]# ssh-copy-id 182.48.115.236
[root@Node_Slave ~]# ssh-copy-id 182.48.115.237
[root@Node_Slave ~]# ssh-copy-id 182.48.115.238
[root@Manager_Slave ~]# ssh-copy-id 182.48.115.236
[root@Manager_Slave ~]# ssh-copy-id 182.48.115.237
[root@Manager_Slave ~]# ssh-copy-id 182.48.115.238
現在3台節點已經能實作兩兩互相ssh通了,不需要輸入密碼即可。如果不能實作任何兩台主機互相之間可以無密碼登入,後面的環節可能會有問題。
2. 實作主機名hostname登入(在三台節點上都需要執行)(這一步不是必須要操作的)
分别設定三台節點機器的主機名(主機名上面已提出),并綁定hosts.
三台機器的/etc/hosts檔案的綁定資訊如下:
[root@Node_Master ~]# vim /etc/hosts
.......
182.48.115.236 Node_Master
182.48.115.237 Node_Slave
182.48.115.238 Manager_Slave
互相驗證下使用主機名登陸是否正常,是否可以互相使用主機名ssh無密碼登陸到對方。
3. 準備好Mysql主從環境
架構如下,一主二從的架構:
主庫:182.48.115.236 從庫:182.48.115.237
主庫:182.48.115.236 從庫:182.48.115.238
Mysql主從環境部署可以參考:http://www.cnblogs.com/kevingrace/p/6256603.html
.......................................................................................
------主庫配置------
server-id=1
log-bin=mysql-bin
binlog-ignore-db=mysql
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
------從庫1配置-------
server-id=2
log-bin=mysql-bin
binlog-ignore-db=mysql # 千萬要注意:主從同步中的過濾字段要一緻,否則後面使用masterha_check_repl 檢查複制時就會出錯!
slave-skip-errors = all
------從庫2配置-------
server-id=3
log-bin=mysql-bin
binlog-ignore-db=mysql
slave-skip-errors = all
然後主庫授權給從庫連接配接的權限,設定後,最好在從庫上驗證下是否能使用授予的權限連接配接主庫。
然後在從庫上根據主庫的“show master status;” 信心進行change master.....同步設定。
注意:
主從設定時,如果設定了bbinlog-ignore-db 和 replicate-ignore-db 過濾規則,則主從必須相同。即要使用binlog-ignore-db過濾字段,則主從配置都使用這個,
要是使用replicate-ignore-db過濾字段,則主從配置都使用這個,千萬不能主從配置使用的過濾字段不一樣!因為MHA 在啟動時候會檢測過濾規則,如果過濾規則不同,MHA 不啟動監控和故障轉移。
.......................................................................................
4. 建立使用者mha管理的賬号(在三台節點上都需要執行)
mysql> GRANT SUPER,RELOAD,REPLICATION CLIENT,SELECT ON *.* TO manager@'182.48.115.%' IDENTIFIED BY 'manager_1234';
Query OK, 0 rows affected (0.06 sec)
mysql> GRANT CREATE,INSERT,UPDATE,DELETE,DROP ON*.* TO manager@'182.48.115.%';
Query OK, 0 rows affected (0.05 sec)
建立主從賬号(在三台節點上都需要執行):
mysql> GRANT RELOAD, SUPER, REPLICATION SLAVE ON*.* TO 'repl'@'182.48.115.%' IDENTIFIED BY 'repl_1234';
Query OK, 0 rows affected (0.09 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)
5. 開始安裝mha
mha包括manager節點和data節點,其中:
data節點包括原有的MySQL複制結構中的主機,至少3台,即1主2從,當master failover後,還能保證主從結構;隻需安裝node包。
manager server:運作監控腳本,負責monitoring 和 auto-failover;需要安裝node包和manager包。
5.1 在所有data資料節點機上安裝安裝MHA node
下載下傳mha4mysql-node-0.56.tar.gz
下載下傳位址:http://pan.baidu.com/s/1cphgLo
提取密碼:7674
[root@Node_Master ~]# yum -y install perl-DBD-MySQL //先安裝所需的perl子產品
[root@Node_Master ~]# tar -zvxf mha4mysql-node-0.56.tar.gz
[root@Node_Master ~]# cd mha4mysql-node-0.56
[root@Node_Master mha4mysql-node-0.56]# perl Makefile.PL
................................................................................................................
這一步可能報錯如下:
1)Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: inc /usr/local/lib64/perl5 /usr/local/share/perl5......
解決辦法:
[root@Node_Master mha4mysql-node-0.56]# yum install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
2)Can't locate CPAN.pm in @INC (@INC contains: inc /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5....
解決辦法:
[root@Node_Master mha4mysql-node-0.56]# yum install -y perl-CPAN
................................................................................................................
[root@Node_Master mha4mysql-node-0.56]# make && make install
5.2 在manager節點(即182.48.115.238)上安裝MHA Manager(注意manager節點也要安裝MHA node)
首先下載下傳第三方yum源
[root@Manager_Slave ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
安裝perl的mysql包:
[root@Manager_Slave ~]# yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Config-IniFiles perl-Time-HiRes -y
安裝MHA Manager軟體包:
下載下傳位址:https://pan.baidu.com/s/1slyfXN3
提取密碼:86wb
[root@Manager_Slave ~]# tar -vxf mha4mysql-manager-0.56.tar
[root@Manager_Slave ~]# cd mha4mysql-manager-0.56
[root@Manager_Slave mha4mysql-manager-0.56]# perl Makefile.PL
[root@Manager_Slave mha4mysql-manager-0.56]# make && make install
安裝完MHA Manager後,在/usr/local/bin目錄下會生成以下腳本:
[root@Manager_Slave mha4mysql-manager-0.56]# ll /usr/local/bin/
總用量 84
-r-xr-xr-x. 1 root root 16367 5月 31 21:37 apply_diff_relay_logs
-r-xr-xr-x. 1 root root 4807 5月 31 21:37 filter_mysqlbinlog
-r-xr-xr-x. 1 root root 1995 5月 31 22:23 masterha_check_repl
-r-xr-xr-x. 1 root root 1779 5月 31 22:23 masterha_check_ssh
-r-xr-xr-x. 1 root root 1865 5月 31 22:23 masterha_check_status
-r-xr-xr-x. 1 root root 3201 5月 31 22:23 masterha_conf_host
-r-xr-xr-x. 1 root root 2517 5月 31 22:23 masterha_manager
-r-xr-xr-x. 1 root root 2165 5月 31 22:23 masterha_master_monitor
-r-xr-xr-x. 1 root root 2373 5月 31 22:23 masterha_master_switch
-r-xr-xr-x. 1 root root 5171 5月 31 22:23 masterha_secondary_check
-r-xr-xr-x. 1 root root 1739 5月 31 22:23 masterha_stop
-r-xr-xr-x. 1 root root 8261 5月 31 21:37 purge_relay_logs
-r-xr-xr-x. 1 root root 7525 5月 31 21:37 save_binary_logs
其中:
masterha_check_repl 檢查MySQL複制狀況
masterha_check_ssh 檢查MHA的SSH配置狀況
masterha_check_status 檢測目前MHA運作狀态
masterha_conf_host 添加或删除配置的server資訊
masterha_manager 啟動MHA
masterha_stop 停止MHA
masterha_master_monitor 檢測master是否當機
masterha_master_switch 控制故障轉移(自動或者手動)
masterha_secondary_check 多種線路檢測master是否存活
另外:
在../mha4mysql-manager-0.56/samples/scripts下還有以下腳本,需要将其複制到/usr/local/bin
[root@Manager_Slave mha4mysql-manager-0.56]# cd samples/scripts/
[root@Manager_Slave scripts]# ll
總用量 32
-rwxr-xr-x. 1 4984 users 3648 4月 1 2014 master_ip_failover //自動切換時VIP管理腳本,不是必須,如果我們使用keepalived的,我們可以自己編寫腳本完成對vip的管理,比如監控mysql,如果mysql異常,我們停止keepalived就行,這樣vip就會自動漂移
-rwxr-xr-x. 1 4984 users 9870 4月 1 2014 master_ip_online_change //線上切換時VIP腳本,不是必須,同樣可以可以自行編寫簡單的shell完成
-rwxr-xr-x. 1 4984 users 11867 4月 1 2014 power_manager //故障發生後關閉master腳本,不是必須
-rwxr-xr-x. 1 4984 users 1360 4月 1 2014 send_report //故障切換發送報警腳本,不是必須,可自行編寫簡單的shell完成
[root@Manager_Slave scripts]# cp ./* /usr/local/bin/
5.3 在管理節點(182.48.115.238)上進行下面配置
[root@Manager_Slave mha4mysql-manager-0.56]# mkdir -p /etc/masterha
[root@Manager_Slave mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /etc/masterha/
[root@Manager_Slave mha4mysql-manager-0.56]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1 //設定manager的工作目錄
manager_log=/var/log/masterha/app1/manager.log //設定manager的日志
ssh_user=root //ssh免密鑰登入的帳号名
repl_user=repl //mysql複制帳号,用來在主從機之間同步二進制日志等
repl_password=repl_1234 //設定mysql中root使用者的密碼,這個密碼是前文中建立監控使用者的那個密碼
ping_interval=1 //設定監控主庫,發送ping包的時間間隔,用來檢查master是否正常,預設是3秒,嘗試三次沒有回應的時候自動進行railover
master_ip_failover_script= /usr/local/bin/master_ip_failover //設定自動failover時候的切換腳本
master_ip_online_change_script= /usr/local/bin/master_ip_online_change //設定手動切換時候的切換腳本
[server1]
hostname=182.48.115.236
port=3306
master_binlog_dir=/data/mysql/data/ //設定master 儲存binlog的位置,以便MHA可以找到master的日志,我這裡的也就是mysql的資料目錄
[server2]
hostname=182.48.115.237
port=3306
candidate_master=1 //設定為候選master,即master機宕掉後,優先啟用這台作為新master,如果設定該參數以後,發生主從切換以後将會将此從庫提升為主庫,即使這個主庫不是叢集中事件最新的slave
check_repl_delay=0 //預設情況下如果一個slave落後master 100M的relay logs的話,MHA将不會選擇該slave作為一個新的master,因為對于這個slave的恢複需要花費很長時間,通過設定check_repl_delay=0,MHA觸發切換在選擇一個新的master的時候将會忽略複制延時,這個參數對于設定了candidate_master=1的主機非常有用,因為這個候選主在切換的過程中一定是新的master
master_binlog_dir=/data/mysql/data/
[server3]
hostname=182.48.115.238
port=3306
#candidate_master=1
master_binlog_dir=/data/mysql/data/
#[server4]
#hostname=host4
#no_master=1
5.4 設定relay log的清除方式(在兩台slave節點上)
[root@Node_Slave ~]# mysql -p123456 -e 'set global relay_log_purge=0'
[root@Manager_Slave ~]# mysql -p123456 -e 'set global relay_log_purge=0'
..................................................................................................
溫馨提示:
MHA在發生切換的過程中,從庫的恢複過程中依賴于relay log的相關資訊,是以這裡要将relay log的自動清除設定為OFF,采用手動清除relay log的方式。
在預設情況下,從伺服器上的中繼日志會在SQL線程執行完畢後被自動删除。但是在MHA環境中,這些中繼日志在恢複其他從伺服器時可能會被用到,是以需要禁用
中繼日志的自動删除功能。定期清除中繼日志需要考慮到複制延時的問題。在ext3的檔案系統下,删除大的檔案需要一定的時間,會導緻嚴重的複制延時。為了避
免複制延時,需要暫時為中繼日志建立硬連結,因為在linux系統中通過硬連結删除大檔案速度會很快。(在mysql資料庫中,删除大表時,通常也采用建立硬連結的方式)
MHA節點中包含了pure_relay_logs指令工具,它可以為中繼日志建立硬連結,執行SET GLOBAL relay_log_purge=1,等待幾秒鐘以便SQL線程切換到新的中繼日志,
再執行SET GLOBAL relay_log_purge=0。
pure_relay_logs腳本參數如下所示:
--user mysql 使用者名
--password mysql 密碼
--port 端口号
--workdir 指定建立relay log的硬連結的位置,預設是/var/tmp,由于系統不同分區建立硬連結檔案會失敗,故需要執行硬連結具體位置,成功執行腳本後,硬連結的中繼日志檔案被删除
--disable_relay_log_purge 預設情況下,如果relay_log_purge=1,腳本會什麼都不清理,自動退出,通過設定這個參數,當relay_log_purge=1的情況下會将relay_log_purge設定為0。清理relay log之後,最後将參數設定為OFF。
設定定期清理relay腳本(在兩台slave節點上操作)
[root@Node_Slave ~]# vim /root/purge_relay_log.sh
#!/bin/bash
user=root
passwd=123456
port=3306
host=localhost
log_dir='/data/masterha/log'
work_dir='/data'
purge='/usr/local/bin/purge_relay_logs'
if [ ! -d $log_dir ]
then
mkdir $log_dir -p
fi
$purge --user=$user --host=$host --password=$passwd --disable_relay_log_purge --port=$port --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1
[root@Node_Slave ~]# chmod 755 /root/purge_relay_log.sh
添加到crontab定期執行
[root@Node_Slave ~]# crontab -e
0 4 * * * /bin/bash /root/purge_relay_log.sh
purge_relay_logs腳本删除中繼日志不會阻塞SQL線程。下面手動執行看看什麼情況。
[root@Node_Slave ~]# /usr/local/bin/purge_relay_logs --user=root --host=localhost --password=123456 --disable_relay_log_purge --port=3306 --workdir=/data
2017-05-31 23:27:13: purge_relay_logs script started.
Found relay_log.info: /data/mysql/data/relay-log.info
Opening /data/mysql/data/mysql-relay-bin.000002 ..
Opening /data/mysql/data/mysql-relay-bin.000003 ..
Executing SET GLOBAL relay_log_purge=1; FLUSH LOGS; sleeping a few seconds so that SQL thread can delete older relay log files (if it keeps up); SET GLOBAL relay_log_purge=0; .. ok.
2017-05-31 23:27:17: All relay log purging operations succeeded.
[root@Node_Slave ~]# ll /data/masterha/log/
總用量 4
-rw-r--r--. 1 root root 905 5月 31 23:26 purge_relay_logs.log
5.5 檢查SSH配置
檢查MHA Manger到所有MHA Node的SSH連接配接狀态:
[root@Manager_Slave ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
Wed May 31 23:06:01 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed May 31 23:06:01 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed May 31 23:06:01 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed May 31 23:06:01 2017 - [info] Starting SSH connection tests..
Wed May 31 23:06:04 2017 - [debug]
Wed May 31 23:06:01 2017 - [debug] Connecting via SSH from [email protected](182.48.115.236:22) to [email protected](182.48.115.237:22)..
Wed May 31 23:06:02 2017 - [debug] ok.
Wed May 31 23:06:02 2017 - [debug] Connecting via SSH from [email protected](182.48.115.236:22) to [email protected](182.48.115.238:22)..
Wed May 31 23:06:03 2017 - [debug] ok.
Wed May 31 23:06:04 2017 - [debug]
Wed May 31 23:06:01 2017 - [debug] Connecting via SSH from [email protected](182.48.115.237:22) to [email protected](182.48.115.236:22)..
Wed May 31 23:06:03 2017 - [debug] ok.
Wed May 31 23:06:03 2017 - [debug] Connecting via SSH from [email protected](182.48.115.237:22) to [email protected](182.48.115.238:22)..
Wed May 31 23:06:04 2017 - [debug] ok.
Wed May 31 23:06:04 2017 - [debug]
Wed May 31 23:06:02 2017 - [debug] Connecting via SSH from [email protected](182.48.115.238:22) to [email protected](182.48.115.236:22)..
Wed May 31 23:06:03 2017 - [debug] ok.
Wed May 31 23:06:03 2017 - [debug] Connecting via SSH from [email protected](182.48.115.238:22) to [email protected](182.48.115.237:22)..
Wed May 31 23:06:04 2017 - [debug] ok.
Wed May 31 23:06:04 2017 - [info] All SSH connection tests passed successfully.
可以看見各個節點ssh驗證都是ok的。
5.6 使用mha工具check檢查repl環境
通過masterha_check_repl腳本檢視整個mysql叢集的複制狀态
[root@Manager_Slave ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Wed May 31 23:43:43 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed May 31 23:43:43 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed May 31 23:43:43 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed May 31 23:43:43 2017 - [info] MHA::MasterMonitor version 0.56.
Wed May 31 23:43:43 2017 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 182.48.115.237(182.48.115.237:3306) :1045:Access denied for user 'root'@'182.48.115.238' (using password: NO), but this is not a MySQL crash. Check MySQL server settings.
at /usr/local/share/perl5/MHA/ServerManager.pm line 297
Wed May 31 23:43:43 2017 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 182.48.115.236(182.48.115.236:3306) :1045:Access denied for user 'root'@'182.48.115.238' (using password: NO), but this is not a MySQL crash. Check MySQL server settings.
at /usr/local/share/perl5/MHA/ServerManager.pm line 297
Wed May 31 23:43:43 2017 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 182.48.115.238(182.48.115.238:3306) :1045:Access denied for user 'root'@'182.48.115.238' (using password: NO), but this is not a MySQL crash. Check MySQL server settings.
at /usr/local/share/perl5/MHA/ServerManager.pm line 297
Wed May 31 23:43:43 2017 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln309] Got fatal error, stopping operations
Wed May 31 23:43:43 2017 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln424] Error happened on checking configurations. at /usr/local/share/perl5/MHA/MasterMonitor.pm line 326
Wed May 31 23:43:43 2017 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln523] Error happened on monitoring servers.
Wed May 31 23:43:43 2017 - [info] Got exit code 1 (Not master dead).
MySQL Replication Health is NOT OK!
發現上面的複制環節是不ok的!!!
原因是通過root使用者遠端連接配接節點的mysql不通
..............................................................................................................
解決辦法:在三個節點機器上的mysql上授權,允許182.48.115.%的機器通過root使用者無密碼登陸,即
mysql> update mysql.user set password=password("") where user="root" and host="182.48.115.%"; //如果沒有這個權限,就grant指令建立這個使用者權限
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from mysql.user;
+---------+--------------+-------------------------------------------+
| user | host | password |
+---------+--------------+-------------------------------------------+
.........
| root | 182.48.115.% | |
+---------+--------------+-------------------------------------------+
11 rows in set (0.00 sec)
..............................................................................................................
然後再次通過masterha_check_repl腳本檢視整個mysql叢集的複制狀态
[root@Manager_Slave ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
..............................
Bareword "FIXME_xxx" not allowed while "strict subs" in use at /usr/local/bin/master_ip_failover line 93.
還是出現如上報錯,原因是:
原來Failover兩種方式:一種是虛拟IP位址,一種是全局配置檔案。MHA并沒有限定使用哪一種方式,而是讓使用者自己選擇,虛拟IP位址的方式會牽扯到其它的軟體,比如keepalive軟體,而且還要修改腳本master_ip_failover。
解決辦法如下:
添加軟連接配接(所有節點):
[root@Manager_Slave ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
[root@Manager_Slave ~]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
先暫時注釋掉管理節點的/etc/masterha/app1.cnf檔案中的master_ip_failover_script= /usr/local/bin/master_ip_failover這個選項。
後面引入keepalived後和修改該腳本以後再開啟該選項。
[root@Manager_Slave ~]# cat /etc/masterha/app1.cnf
.........
#master_ip_failover_script= /usr/local/bin/master_ip_failover
最後在通過masterha_check_repl腳本檢視整個mysql叢集的複制狀态
[root@Manager_Slave ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Thu Jun 1 00:20:58 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 1 00:20:58 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Thu Jun 1 00:20:58 2017 - [info] read_only=1 is not set on slave 182.48.115.237(182.48.115.237:3306).
Thu Jun 1 00:20:58 2017 - [warning] relay_log_purge=0 is not set on slave 182.48.115.237(182.48.115.237:3306).
Thu Jun 1 00:20:58 2017 - [info] read_only=1 is not set on slave 182.48.115.238(182.48.115.238:3306).
Thu Jun 1 00:20:58 2017 - [warning] relay_log_purge=0 is not set on slave 182.48.115.238(182.48.115.238:3306).
Thu Jun 1 00:20:58 2017 - [info] Checking replication filtering settings..
Thu Jun 1 00:20:58 2017 - [info] binlog_do_db= , binlog_ignore_db= mysql
Thu Jun 1 00:20:58 2017 - [info] Replication filtering check ok.
Thu Jun 1 00:20:58 2017 - [info] GTID (with auto-pos) is not supported
Thu Jun 1 00:20:58 2017 - [info] Starting SSH connection tests..
Thu Jun 1 00:21:02 2017 - [info] All SSH connection tests passed successfully.
...........
Thu Jun 1 00:21:07 2017 - [info] Checking replication health on 182.48.115.237..
Thu Jun 1 00:21:07 2017 - [info] ok.
Thu Jun 1 00:21:07 2017 - [info] Checking replication health on 182.48.115.238..
Thu Jun 1 00:21:07 2017 - [info] ok.
Thu Jun 1 00:21:07 2017 - [warning] master_ip_failover_script is not defined.
Thu Jun 1 00:21:07 2017 - [warning] shutdown_script is not defined.
Thu Jun 1 00:21:07 2017 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
這個時候,發現整個複制環境狀況是ok的了!!
6. 管理mha操作
6.1 檢查MHA Manager的狀态
通過master_check_status腳本檢視Manager的狀态
[root@Manager_Slave ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
注意:如果正常,會顯示"PING_OK",否則會顯示"NOT_RUNNING",這代表MHA監控沒有開啟
6.2 開啟MHA Manager監控
使用下面指令放在背景執行啟動動作
[root@Manager_Slave ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
啟動參數介紹:
--remove_dead_master_conf 該參數代表當發生主從切換後,老的主庫的ip将會從配置檔案中移除。
--manger_log 日志存放位置
--ignore_last_failover 在預設情況下,如果MHA檢測到連續發生當機,且兩次當機間隔不足8小時的話,則不會進行Failover,之是以這樣限制是為了
避免ping-pong效應。該參數代表忽略上次MHA觸發切換産生的檔案,預設情況下,MHA發生切換後會在日志目錄,也就是上面我
設定的/data産生app1.failover.complete檔案,下次再次切換的時候如果發現該目錄下存在該檔案将不允許觸發切換,除非
在第一次切換後收到删除該檔案,為了友善,這裡設定為--ignore_last_failover。
再次檢視MHA Manager監控是否正常:
[root@Manager_Slave ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:2542) is running(0:PING_OK), master:182.48.115.236
可以看見已經在監控了,而且master的主機為182.48.115.236
檢視啟動日志
[root@Manager_Slave ~]# tail -n20 /var/log/masterha/app1/manager.log
Checking slave recovery environment settings..
Opening /data/mysql/data/relay-log.info ... ok.
Relay log found at /data/mysql/data, up to mysql-relay-bin.000006
Temporary relay log file is /data/mysql/data/mysql-relay-bin.000006
Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Jun 1 00:37:29 2017 - [info] Slaves settings check done.
Thu Jun 1 00:37:29 2017 - [info]
182.48.115.236(182.48.115.236:3306) (current master)
+--182.48.115.237(182.48.115.237:3306)
+--182.48.115.238(182.48.115.238:3306)
Thu Jun 1 00:37:29 2017 - [warning] master_ip_failover_script is not defined.
Thu Jun 1 00:37:29 2017 - [warning] shutdown_script is not defined.
Thu Jun 1 00:37:29 2017 - [info] Set master ping interval 1 seconds.
Thu Jun 1 00:37:29 2017 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Thu Jun 1 00:37:29 2017 - [info] Starting ping health check on 182.48.115.236(182.48.115.236:3306)..
Thu Jun 1 00:37:29 2017 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
其中"Ping(SELECT) succeeded, waiting until MySQL doesn't respond.."說明整個系統已經開始監控了。
6.3 關閉MHA Manage監控
關閉很簡單,使用masterha_stop指令完成。
[root@Manager_Slave ~]# masterha_stop --conf=/etc/masterha/app1.cnf
Stopped app1 successfully.
[1]+ Exit 1 nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1
[root@Manager_Slave ~]#
檢視MHA Manager監控,發現已關閉
[root@Manager_Slave ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
7. 配置VIP
vip配置可以采用兩種方式,一種通過keepalived的方式管理虛拟ip浮動;另外一種通過腳本方式啟動虛拟ip的方式(即不需要keepalived或者heartbeat類似的軟體)。
第一種方式:通過keepalive的方式管理vip
1. 下載下傳軟體進行并進行安裝(在兩台master上都要安裝,準确的說一台是master(182.48.115.236);另外一台是備選master(182.48.115.237),在沒有切換以前是slave)
[root@Node_Master ~]# yum install -y openssl-devel
[root@Node_Master ~]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
[root@Node_Master ~]# tar -zvxf keepalived-1.3.5.tar.gz
[root@Node_Master ~]# cd keepalived-1.3.5
[root@Node_Master keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
[root@Node_Master keepalived-1.3.5]# make && make install
[root@Node_Master keepalived-1.3.5]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@Node_Master keepalived-1.3.5]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Node_Master keepalived-1.3.5]# mkdir /etc/keepalived
[root@Node_Master keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Node_Master keepalived-1.3.5]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
2. keepalived配置
------------在master上配置(182.48.115.236節點上的配置)------------------
[root@Node_Master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@Node_Master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 150
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
182.48.115.239
}
}
其中router_id MySQL HA表示設定keepalived組的名稱,将182.48.115.239這個虛拟ip綁定到該主機的eth1網卡上,并且設定了狀态為backup模式,
将keepalived的模式設定為非搶占模式(nopreempt),priority 150表示設定的優先級為150。
------------在candicate master上配置(182.48.115.237節點上的配置)------------------
[root@Node_Slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 120
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
182.48.115.239
}
}
3. 啟動keepalived服務
--------------在master上啟動并檢視日志----------------
[root@Node_Master ~]# /etc/init.d/keepalived start
正在啟動 keepalived: [确定]
[root@Node_Master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:5f:58:dc brd ff:ff:ff:ff:ff:ff
inet 182.48.115.236/27 brd 182.48.115.255 scope global eth0
inet 182.48.115.239/32 scope global eth0
inet6 fe80::5054:ff:fe5f:58dc/64 scope link
valid_lft forever preferred_lft forever
[root@Node_Master ~]# tail -100 /var/log/messages
..........
Jun 1 02:12:10 percona1 Keepalived_vrrp[10329]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.48.115.239
Jun 1 02:12:10 percona1 Keepalived_vrrp[10329]: Sending gratuitous ARP on eth0 for 182.48.115.239
Jun 1 02:12:10 percona1 Keepalived_vrrp[10329]: Sending gratuitous ARP on eth0 for 182.48.115.239
Jun 1 02:12:10 percona1 Keepalived_vrrp[10329]: Sending gratuitous ARP on eth0 for 182.48.115.239
Jun 1 02:12:10 percona1 Keepalived_vrrp[10329]: Sending gratuitous ARP on eth0 for 182.48.115.239
發現vip資源已經綁定到182.48.115.236這個master節點機上了
--------------在candicate master上啟動----------------
[root@Node_Slave ~]# /etc/init.d/keepalived start
正在啟動 keepalived: [确定]
[root@Node_Slave ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:1b:6e:53 brd ff:ff:ff:ff:ff:ff
inet 182.48.115.237/27 brd 182.48.115.255 scope global eth0
inet6 fe80::5054:ff:fe1b:6e53/64 scope link
valid_lft forever preferred_lft forever
.....................................................................
從上面的資訊可以看到keepalived已經配置成功。
注意:
上面兩台伺服器的keepalived都設定為了BACKUP模式,在keepalived中2種模式,分别是master->backup模式和backup->backup模式。這兩種模式有很大差別。
在master->backup模式下,一旦主庫當機,虛拟ip會自動漂移到從庫,當主庫修複後,keepalived啟動後,還會把虛拟ip搶占過來,即使設定了非搶占模式(nopreempt)
搶占ip的動作也會發生。在backup->backup模式下,當主庫當機後虛拟ip會自動漂移到從庫上,當原主庫恢複和keepalived服務啟動後,并不會搶占新主的虛拟ip,即使是
優先級高于從庫的優先級别,也不會發生搶占。為了減少ip漂移次數,通常是把修複好的主庫當做新的備庫。
4. MHA引入keepalived(MySQL服務程序挂掉時通過MHA 停止keepalived)
要想把keepalived服務引入MHA,隻需要修改切換是觸發的腳本檔案master_ip_failover即可,在該腳本中添加在master發生當機時對keepalived的處理。
編輯腳本/usr/local/bin/master_ip_failover,修改後如下:
[root@Manager_Slave ~]# vim /usr/local/bin/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
);
my $vip = '182.48.115.239';
my $ssh_start_vip = "/etc/init.d/keepalived start";
my $ssh_stop_vip = "/etc/init.d/keepalived stop";
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";
#`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
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";
}
現在已經修改這個腳本了,現在打開在上面提到過的參數,再檢查叢集狀态,看是否會報錯
[root@Manager_Slave ~]# grep 'master_ip_failover_script' /etc/masterha/app1.cnf
master_ip_failover_script= /usr/local/bin/master_ip_failover
[root@Manager_Slave ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
.......
Checking the Status of the script.. OK
Thu Jun 1 03:31:57 2017 - [info] OK.
Thu Jun 1 03:31:57 2017 - [warning] shutdown_script is not defined.
Thu Jun 1 03:31:57 2017 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
可以看出複制情況正常!
/usr/local/bin/master_ip_failover添加或者修改的内容意思是當主庫資料庫發生故障時,會觸發MHA切換,MHA Manager會停掉主庫上的keepalived服務,
觸發虛拟ip漂移到備選從庫,進而完成切換。當然可以在keepalived裡面引入腳本,這個腳本監控mysql是否正常運作,如果不正常,則調用該腳本殺掉keepalived程序。
第二種方式:通過腳本的方式管理VIP
這裡是修改/usr/local/bin/master_ip_failover,修改完成後内容如下。還需要手動在master伺服器上綁定一個vip
1. 現在master節點上綁定vip
[root@Master_node ~]# ifconfig eth0:0 182.48.115.239/27 //本機子網路遮罩是27,一般都是24
[root@Master_node ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:5F:58:DC
inet addr:182.48.115.236 Bcast:182.48.115.255 Mask:255.255.255.224
inet6 addr: fe80::5054:ff:fe5f:58dc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:25505 errors:0 dropped:0 overruns:0 frame:0
TX packets:3358 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3254957 (3.1 MiB) TX bytes:482420 (471.1 KiB)
eth0:0 Link encap:Ethernet HWaddr 52:54:00:5F:58:DC
inet addr:182.48.115.239 Bcast:182.48.115.255 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
2. manager節點修改/usr/local/bin/master_ip_failover
[root@Manager_Slave ~]# cat /usr/local/bin/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
);
my $vip = '182.48.115.239/27';
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 {
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";
}
注意要将/etc/masterha/app1.cnf檔案中的master_ip_failover_script注釋打開
為了防止腦裂發生,推薦生産環境采用腳本的方式來管理虛拟ip,而不是使用keepalived來完成。到此為止,基本MHA叢集已經配置完畢。
接下來就是實際的測試環節了。通過一些測試來看一下MHA到底是如何進行工作的。
8. failover故障切換
1. 自動切換(必須先啟動MHA Manager,否則無法自動切換。(當然手動切換不需要開啟MHA Manager監控))
1)在master主庫上使用sysbench生成測試資料
[root@Master_node ~]# yum install sysbench -y
在主庫(182.48.115.236)上進行sysbench資料生成,在sbtest庫下生成sbtest表,共100W記錄。
[root@Master_node ~]# sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=1800 --mysql-user=root --mysql-socket=/local/mysql/var/mysql.sock --mysql-password=123456 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex prepare
1.1 在candicate master(182.48.115.237)上停掉slave sql線程,模拟主從延時。
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.08 sec)
注意:另外一台slave沒有停止io線程,是以還在繼續接收日志。
1.2 模拟sysbench壓力測試
在主庫上(182.48.115.236)進行壓力測試,持續時間為3分鐘,産生大量的binlog
[root@Master_node ~]# sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=180 --mysql-user=root --mysql-socket=/local/mysql/var/mysql.sock --mysql-password=123456 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex run
1.3 開啟在candicate master(182.48.115.237)上的IO線程,追趕落後于master的binlog
mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
1.4 殺掉主庫(182.48.115.236)mysql程序,模拟主庫發生故障,進行自動failover操作
[root@Master_node ~]# pkill -9 mysqld
1.5 檢視MHA切換日志,了解整個切換過程。在manager管理節點(182.48.115.238)上檢視日志
[root@Manager_Slave ~]# cat /var/log/masterha/app1/manager.log
........
........
----- Failover Report -----
app1: MySQL Master failover 182.48.115.236 to 182.48.115.237 succeeded
Master 182.48.115.236 is down!
Check MHA Manager logs at server01:/var/log/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 182.48.115.236.
The latest slave 182.48.115.237(182.48.115.237:3306) has all relay logs for recovery.
Selected 182.48.115.237 as a new master.
182.48.115.237: OK: Applying all logs succeeded.
182.48.115.237: OK: Activated master IP address.
192.168.0.70: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.0.70: OK: Applying all logs succeeded. Slave started, replicating from 182.48.115.237.
182.48.115.237: Resetting slave info succeeded.
Master failover to 182.48.115.237(182.48.115.237:3306) completed successfully.
看到最後的Master failover to 182.48.115.237(182.48.115.237:3306) completed successfully.說明備選master現在已經上位了。
從上面的輸出可以看出整個MHA的切換過程,共包括以下的步驟:
1)配置檔案檢查階段,這個階段會檢查整個叢集配置檔案配置
2)當機的master處理,這個階段包括虛拟ip摘除操作,主機關機操作(這個我這裡還沒有實作,需要研究)
3)複制dead maste和最新slave相差的relay log,并儲存到MHA Manger具體的目錄下
4)識别含有最新更新的slave
5)應用從master儲存的二進制日志事件(binlog events)
6)提升一個slave為新的master進行複制
7)使其他的slave連接配接新的master進行複制
最後啟動MHA Manger監控,檢視叢集裡面現在誰是master
[root@Manager_Slave ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:13301) is running(0:PING_OK), master:182.48.115.237
2. 手動Failover(MHA Manager必須沒有運作)
手動failover,這種場景意味着在業務上沒有啟用MHA自動切換功能,當主伺服器故障時,人工手動調用MHA來進行故障切換操作,具體指令如下:
確定mha manager關閉
[root@Manager_Slave ~]# masterha_stop --conf=/etc/masterha/app1.cnf
注意:如果MHA manager檢測到沒有dead的server,将報錯,并結束failover:
[root@Manager_Slave ~]# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=182.48.115.236 --dead_master_port=3306 --new_master_host=182.48.115.237 --new_master_port=3306 --ignore_last_failover
輸出的資訊會詢問你是否進行切換:
........
----- Failover Report -----
app1: MySQL Master failover 182.48.115.236 to 182.48.115.237 succeeded
Master 182.48.115.236 is down!
Check MHA Manager logs at server01 for details.
Started manual(interactive) failover.
Invalidated master IP address on 182.48.115.236.
The latest slave 182.48.115.237(182.48.115.237:3306) has all relay logs for recovery.
Selected 182.48.115.237 as a new master.
182.48.115.237: OK: Applying all logs succeeded.
182.48.115.237: OK: Activated master IP address.
192.168.0.70: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.0.70: OK: Applying all logs succeeded. Slave started, replicating from 182.48.115.237.
182.48.115.237: Resetting slave info succeeded.
Master failover to 182.48.115.237(182.48.115.237:3306) completed successfully.
這樣即模拟了master當機的情況下手動把192.168.0.60提升為主庫的操作過程。
9. 線上進行切換
在許多情況下, 需要将現有的主伺服器遷移到另外一台伺服器上,比如主伺服器硬體故障,RAID 控制卡需要重建,将主伺服器移到性能更好的伺服器上等等。維護主伺服器引起性能下降,
導緻停機時間至少無法寫入資料。 另外, 阻塞或殺掉目前運作的會話會導緻主主之間資料不一緻的問題發生。 MHA提供快速切換和優雅的阻塞寫入,這個切換過程隻需要 0.5-2s 的時
間,這段時間内資料是無法寫入的。在很多情況下,0.5-2s 的阻塞寫入是可以接受的。是以切換主伺服器不需要計劃配置設定維護時間視窗。
MHA線上切換的大概過程:
1)檢測複制設定和确定目前主伺服器
2)确定新的主伺服器
3)阻塞寫入到目前主伺服器
4)等待所有從伺服器趕上複制
5)授予寫入到新的主伺服器
6)重新設定從伺服器
注意,線上切換的時候應用架構需要考慮以下兩個問題:
1)自動識别master和slave的問題(master的機器可能會切換),如果采用了vip的方式,基本可以解決這個問題。
2)負載均衡的問題(可以定義大概的讀寫比例,每台機器可承擔的負載比例,當有機器離開叢集時,需要考慮這個問題)
為了保證資料完全一緻性,在最快的時間内完成切換,MHA的線上切換必須滿足以下條件才會切換成功,否則會切換失敗。
1)所有slave的IO線程都在運作
2)所有slave的SQL線程都在運作
3)所有的show slave status的輸出中Seconds_Behind_Master參數小于或者等于running_updates_limit秒,如果在切換過程中不指定running_updates_limit,那麼
預設情況下running_updates_limit為1秒。
4)在master端,通過show processlist輸出,沒有一個更新花費的時間大于running_updates_limit秒。
線上切換步驟如下:
首先,manager節點上停掉MHA監控:
[root@Manager_Slave ~]# masterha_stop --conf=/etc/masterha/app1.cnf
其次,進行線上切換操作(模拟線上切換主庫操作,原主庫182.48.115.236變為slave,182.48.115.237提升為新的主庫)
[root@Manager_Slave ~]# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=182.48.115.237 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
.........
Thu Jun 1 00:28:02 2014 - [info] Executed CHANGE MASTER.
Thu Jun 1 00:28:02 2014 - [info] Slave started.
Thu Jun 1 00:28:02 2014 - [info] All new slave servers switched successfully.
Thu Jun 1 00:28:02 2014 - [info]
Thu Jun 1 00:28:02 2014 - [info] * Phase 5: New master cleanup phease..
Thu Jun 1 00:28:02 2014 - [info]
Thu Jun 1 00:28:02 2014 - [info] 192.168.0.60: Resetting slave info succeeded.
Thu Jun 1 00:28:02 2014 - [info] Switching master to 192.168.0.60(192.168.0.60:3306) completed successfully.
其中參數的意思:
--orig_master_is_new_slave 切換時加上此參數是将原 master 變為 slave 節點,如果不加此參數,原來的 master 将不啟動
--running_updates_limit=10000 故障切換時,候選master 如果有延遲的話, mha 切換不能成功,加上此參數表示延遲在此時間範圍内都可切換(機關為s),但是切換的
時間長短是由recover 時relay 日志的大小決定
注意:
由于線上進行切換需要調用到master_ip_online_change這個腳本,但是由于該腳本不完整,需要進行相應的修改,腳本調整如下:
[root@Manager_Slave ~]# vim /usr/local/bin/master_ip_online_change
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
use Time::HiRes qw( sleep gettimeofday tv_interval );
use Data::Dumper;
my $_tstart;
my $_running_interval = 0.1;
my (
$command, $orig_master_host, $orig_master_ip,
$orig_master_port, $orig_master_user,
$new_master_host, $new_master_ip, $new_master_port,
$new_master_user,
);
my $vip = '182.48.115.239/27'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth1:$key down";
my $ssh_user = "root";
my $new_master_password='123456';
my $orig_master_password='123456';
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,
'orig_master_user=s' => \$orig_master_user,
#'orig_master_password=s' => \$orig_master_password,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
'new_master_user=s' => \$new_master_user,
#'new_master_password=s' => \$new_master_password,
);
exit &main();
sub current_time_us {
my ( $sec, $microsec ) = gettimeofday();
my $curdate = localtime($sec);
return $curdate . " " . sprintf( "%06d", $microsec );
}
sub sleep_until {
my $elapsed = tv_interval($_tstart);
if ( $_running_interval > $elapsed ) {
sleep( $_running_interval - $elapsed );
}
}
sub get_threads_util {
my $dbh = shift;
my $my_connection_id = shift;
my $running_time_threshold = shift;
my $type = shift;
$running_time_threshold = 0 unless ($running_time_threshold);
$type = 0 unless ($type);
my @threads;
my $sth = $dbh->prepare("SHOW PROCESSLIST");
$sth->execute();
while ( my $ref = $sth->fetchrow_hashref() ) {
my $id = $ref->{Id};
my $user = $ref->{User};
my $host = $ref->{Host};
my $command = $ref->{Command};
my $state = $ref->{State};
my $query_time = $ref->{Time};
my $info = $ref->{Info};
$info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
next if ( $my_connection_id == $id );
next if ( defined($query_time) && $query_time < $running_time_threshold );
next if ( defined($command) && $command eq "Binlog Dump" );
next if ( defined($user) && $user eq "system user" );
next
if ( defined($command)
&& $command eq "Sleep"
&& defined($query_time)
&& $query_time >= 1 );
if ( $type >= 1 ) {
next if ( defined($command) && $command eq "Sleep" );
next if ( defined($command) && $command eq "Connect" );
}
if ( $type >= 2 ) {
next if ( defined($info) && $info =~ m/^select/i );
next if ( defined($info) && $info =~ m/^show/i );
}
push @threads, $ref;
}
return @threads;
}
sub main {
if ( $command eq "stop" ) {
## Gracefully killing connections on the current master
# 1. Set read_only= 1 on the new master
# 2. DROP USER so that no app user can establish new connections
# 3. Set read_only= 1 on the current master
# 4. Kill current queries
# * Any database access failure will result in script die.
my $exit_code = 1;
eval {
## Setting read_only=1 on the new master (to avoid accident)
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error(die_on_error)_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
print current_time_us() . " Set read_only on the new master.. ";
$new_master_handler->enable_read_only();
if ( $new_master_handler->is_read_only() ) {
print "ok.\n";
}
else {
die "Failed!\n";
}
$new_master_handler->disconnect();
# Connecting to the orig master, die if any database error happens
my $orig_master_handler = new MHA::DBHelper();
$orig_master_handler->connect( $orig_master_ip, $orig_master_port,
$orig_master_user, $orig_master_password, 1 );
## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
#$orig_master_handler->disable_log_bin_local();
#print current_time_us() . " Drpping app user on the orig master..\n";
#FIXME_xxx_drop_app_user($orig_master_handler);
## Waiting for N * 100 milliseconds so that current connections can exit
my $time_until_read_only = 15;
$_tstart = [gettimeofday];
my @threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
while ( $time_until_read_only > 0 && $#threads >= 0 ) {
if ( $time_until_read_only % 5 == 0 ) {
printf
"%s Waiting all running %d threads are disconnected.. (max %d milliseconds)\n",
current_time_us(), $#threads + 1, $time_until_read_only * 100;
if ( $#threads < 5 ) {
print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
foreach (@threads);
}
}
sleep_until();
$_tstart = [gettimeofday];
$time_until_read_only--;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
}
## Setting read_only=1 on the current master so that nobody(except SUPER) can write
print current_time_us() . " Set read_only=1 on the orig master.. ";
$orig_master_handler->enable_read_only();
if ( $orig_master_handler->is_read_only() ) {
print "ok.\n";
}
else {
die "Failed!\n";
}
## Waiting for M * 100 milliseconds so that current update queries can complete
my $time_until_kill_threads = 5;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
if ( $time_until_kill_threads % 5 == 0 ) {
printf
"%s Waiting all running %d queries are disconnected.. (max %d milliseconds)\n",
current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
if ( $#threads < 5 ) {
print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
foreach (@threads);
}
}
sleep_until();
$_tstart = [gettimeofday];
$time_until_kill_threads--;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
}
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
## Terminating all threads
print current_time_us() . " Killing all application threads..\n";
$orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
print current_time_us() . " done.\n";
#$orig_master_handler->enable_log_bin_local();
$orig_master_handler->disconnect();
## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
## Activating master ip on the new master
# 1. Create app user with write privileges
# 2. Moving backup script if needed
# 3. Register new master's ip to the catalog database
# We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
# If exit code is 0 or 10, MHA does not abort
my $exit_code = 10;
eval {
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
## Set read_only=0 on the new master
#$new_master_handler->disable_log_bin_local();
print current_time_us() . " Set read_only=0 on the new master.\n";
$new_master_handler->disable_read_only();
## Creating an app user on the new master
#print current_time_us() . " Creating app user on the new master..\n";
#FIXME_xxx_create_app_user($new_master_handler);
#$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
# do nothing
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_online_change --command=start|stop|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";
die;
}
由于線上進行切換需要調用到master_ip_online_change這個腳本,但是由于該腳本不完整,需要自己進行相應的修改。在測試中發現腳本還是有問題,腳本中new_master_password這個變量擷取不到,導緻線上切換失敗,是以進行了相關的寫死,直接把mysql的root使用者密碼指派給變量new_master_password。另外這個腳本還可以管理vip。
10. 修複當機後的master節點
通常情況下自動切換以後,原master可能已經廢棄掉,待原master主機修複後,如果資料完整的情況下,可能想把原來master重新作為新主庫的slave,這時我們可以借助當時自動切換時刻的MHA日志來完成對原master的修複。下面是提取相關日志的指令:
[root@Manager_Slave ~]# grep -i "All other slaves should start" manager.log
Mon Apr 21 22:28:33 2016 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='182.48.115.237', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000022', MASTER_LOG_POS=506716, MASTER_USER='repl', MASTER_PASSWORD='xxx';
擷取上述資訊以後,就可以直接在修複後的master上執行change master to相關操作,重新作為從庫了。
最後補充一下郵件發送腳本send_report ,這個腳本經過調整後可以使用,如下:
#!/usr/bin/perl
# Copyright (C) 2011 DeNA Co.,Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
## Note: This is a sample script and is not complete. Modify the script based on your environment.
use strict;
use warnings FATAL => 'all';
use Mail::Sender;
use Getopt::Long;
#new_master_host and new_slave_hosts are set only when recovering master succeeded
my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
my $smtp='smtp.163.com';
my $mail_from='xxxx';
my $mail_user='xxxxx';
my $mail_pass='xxxxx';
my $mail_to=['xxxx','xxxx'];
GetOptions(
'orig_master_host=s' => \$dead_master_host,
'new_master_host=s' => \$new_master_host,
'new_slave_hosts=s' => \$new_slave_hosts,
'subject=s' => \$subject,
'body=s' => \$body,
);
mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);
sub mailToContacts {
my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;
open my $DEBUG, "> /tmp/monitormail.log"
or die "Can't open the debug file:$!\n";
my $sender = new Mail::Sender {
ctype => 'text/plain; charset=utf-8',
encoding => 'utf-8',
smtp => $smtp,
from => $mail_from,
auth => 'LOGIN',
TLS_allowed => '0',
authid => $user,
authpwd => $passwd,
to => $mail_to,
subject => $subject,
debug => $DEBUG
};
$sender->MailMsg(
{ msg => $msg,
debug => $DEBUG
}
) or print $Mail::Sender::Error;
return 1;
}
# Do whatever you want here
exit 0;
告警郵件如下:
目前高可用方案可以一定程度上實作資料庫的高可用,出于對資料庫的高可用和資料一緻性的要求,推薦使用MHA架構。
*************** 當你發現自己的才華撐不起野心時,就請安靜下來學習吧!***************