MHA官方文檔

Overview

MHA能夠在較短的時間內實現自動故障檢測和故障轉移,通常在10-30秒以內;在複製框架中,MHA能夠很好地解決複製過程中的數據一致性問題,由於不需要在現有的replication中添加額外的服務器,僅需要一個manager節點,而一個Manager能管理多套複製,所以能大大地節約服務器的數量;另外,安裝簡單,無性能損耗,以及不需要修改現有的複製部署也是它的優勢之處。
MHA還提供在線主庫切換的功能,能夠安全地切換當前運行的主庫到一個新的主庫中(通過將從庫提升爲主庫),大概0.5-2秒內即可完成。
MHA提供了上述功能,使得其在適用於對高可用性,數據完整性要求高的場合,還有要求幾乎non-stop的主庫維護。

◎自動故障檢測和自動故障轉移

MHA能夠在一個已經存在的複製環境中監控MySQL,當檢測到Master故障後能夠實現自動故障轉移,通過鑑定出最“新”的Salve的relay log,並將其應用到所有的Slave,這樣MHA就能夠保證各個slave之間的數據一致性,即使有些slave在主庫崩潰時還沒有收到最新的relay log事件。通常情況下MHA能夠達到如下指標:9-12秒檢測到主庫故障,7-10秒關閉master所在的mysqld服務以防止故障擴散,並在幾秒內實現各個slave上的relay log重放到新的master。總共的down time通常控制在10-30秒內。一個slave節點能否成爲候選的主節點可通過在配置文件中配置它的優先級。由於master能夠保證各個slave之間的數據一致性,所以所有的slave節點都有希望成爲主節點。在通常的replication環境中由於複製中斷而極容易產生的數據一致性問題,在MHA中將不會發生。

◎交互式(手動)故障轉移

MHA可以被定義成手動地實現故障轉移,而不必去理會master的狀態,即不監控master狀態,確認故障發生後可通過MHA手動切換。

◎非交互式的故障轉移

即不監控Master狀態,但是發生故障後可通過MHA實現自動轉移。

◎在線切換Master到不同的主機

通常當RAID控制器或者RAM損壞,或者需要將現有的master服務器進行升級的時候,我們就需要切換當前的master到其他的主機中。這並不是主庫崩潰,但是卻需要我們手動切換。這通常是越快越好,因爲這段時間內主庫是寫禁止的。所以,你還需要阻塞或刪除正在進行的會話,因爲不禁止寫就會導致數據一致性問題。舉個例子,updating master1, updating master 2,committing master1, getting error on committing master 2就會導致數據一致性問題。所以說,快速的切換和優美平滑的阻塞寫都是需要的。
MHA能夠在0.5-2秒內實現切換,0.5-2秒的寫阻塞通常是可接受的,所以你甚至能在非維護期間就在線切換master。諸如升級到高版本,升級到更快的服務器之類的工作,將會變得更容易。


Architecture of MHA

當主庫發生崩潰,MHA通過以下方式修復
\


MHA Components

MHA由Manager節點和Node節點組成。
\


Manaer模塊:可以管理多套Master-Slave Replication

Masterha_manager:提供實現自動故障檢測和故障轉移的命令
其他幫助腳本:提供手工故障轉移,在線master切換,con 檢查等功能

Node模塊:部署在所有的MySQL Server上

Save_binary_logs:如有必要,複製master的二進制日誌
Apply_diff_relay_logs:從數據最新的slave上產生不同的relay log,並且將其應用到不同的binlog events中
Purge_relay_log:清除relay log
MHA manager節點上運行着這些程序:監控mysql狀態,master故障轉移等。
MHA node節點上有實現自動故障轉移的helper腳本,比如分析mysql binary/relay log,認出哪一個relay log應該應用到其他的slave,並識別出這個relay log的位置,並將events應用到目標slave上等。Node節點應該運行在每一個mysql server上。
如果MHA Manager掛掉了,MHA會嘗試通過SSH連接到node節點並執行node節點的命令




Advantages of MHA

這一節簡略介紹,大致內容在上面的敘述中已經有提到。
1 Masterfailover and slave promotion can be done very quickly
自動故障轉移快
2 Mastercrash does not result in data inconsistency
主庫崩潰不存在數據一致性問題
3 Noneed to modify current MySQL settings (MHA works with regular MySQL (5.0 orlater))
不需要對當前mysql環境做重大修改
4 Noneed to increase lots of servers
不需要添加額外的服務器(僅一臺manager就可管理上百個replication)
5 Noperformance penalty
性能優秀,可工作在半同步複製和異步複製,當監控mysql狀態時,僅需要每隔N秒向master發送ping包(默認3秒),所以對性能無影響。你可以理解爲MHA的性能和簡單的主從複製框架性能一樣。
6 Works with any storage engine
只要replication支持的存儲引擎,MHA都支持,不會侷限於innodb


Typical Use cases

怎麼部署Manager節點

◎設置一個專門的Manager Server和多個Replication環境

由於MHA manager僅僅使用了非常少的cpu和內存資源,所以你可以讓一個manager管理很多個replication,甚至超過100個replication

◎Manager節點和一個salve節點複用

假如你只有一個replication環境,而且你可能不喜歡爲配置一個專門的manager而花費了更多的硬件開銷,那麼你可以讓manager和一個slave節點複用。值得注意的是,如果這麼配置了,儘管manager和slave在同一臺機子上了,但是manger依舊通過SSH連接到slave,所以你依舊需要配置SSH無密碼登陸。


複製配置(這一部分簡略翻譯)

Singlemaster, multiple slaves

\
一主多從,這是最普遍的情況。


Singlemaster, multiple slaves (one on remote datacenter)



\
一主多從,將其中一個從配置成遠程數據中心,其永遠不會成爲master


Singlemaster, multiple slaves, one candidate master



\
一主多從,並只配置一個候選主節點


Multiplemasters, multiple slaves

\

Threetier replication

\


管理MasterIP地址

HA方案中,很多情況下人們會在master上綁定一個虛擬IP。當master崩潰的時候,軟件比如Keepalived會將虛擬IP重新指向正常的Server。
通用的方法就是創建一個全局的目錄庫,在庫中存放着所有應用和IP地址之間的映射關係,用以取代VIP。在這種方案下,如果master崩潰,那麼你就需要修改這個目錄庫。
兩種方案都各有優缺點,MHA不會強制使用哪一種。MHA可以調用其他的腳本來禁用\激活write ip地址,通過設置master_ip_failover_script 腳本的參數,該腳本可在manager節點中找到。你可以在該腳本中更新目錄庫,或者實現VIP漂移等任何你想幹的事。你同樣可以借用現有的HA方案的軟件實現IP故障轉移,比如Pacemaker,在這種情況下MHA將不會做IP故障轉移。


和MySQL半同步複製配合使用

儘管MHA試圖從崩潰的master上保存binarylog,但這並不總是可行的。例如,如果master是因爲H/W故障或者是SSH故障,則MHA無法保存binlog,從而無法應用僅存在master上的binlog進行故障轉移,這將會導致丟失最近的數據。
使用半同步複製可以極大地減少這種丟失數據的風險。由於它也是基於mysql的複製機制,所以MHA能夠配合半同步複製一起使用。值得一提的是,只要有一臺slave收到最新的binlog events,則MHA就會將它應用到所有的slave,從而保證了數據的一致性。




Tutorial

創建通用的複製環境

MHA不會自己創建replication環境,所以你需要自己手動搭建。換句話說,你可以將MHA部署在現有的複製環境中。舉個例子,假設有四臺主機:host1,host2,host3,host4.我們將host1配置成master,host2和host3配置成slave,而host4配置成manager


在host1-host4上安裝node節點

RHEL/Centos系統

 ## If you have not installed DBD::mysql, install it like below, or install from source.
  # yum install perl-DBD-MySQL

  ## Get MHA Node rpm package from "Downloads" section.
  # rpm -ivh mha4mysql-node-X.Y-0.noarch.rpm

Ubuntu/Debian系統

## If you have not installed DBD::mysql, install it like below, or install from source.
  # apt-get install libdbd-mysql-perl

  ## Get MHA Node deb package from "Downloads" section.
  # dpkg -i mha4mysql-node_X.Y_all.deb

源碼安裝

  ## Install DBD::mysql if not installed
  $ tar -zxf mha4mysql-node-X.Y.tar.gz
  $ perl Makefile.PL
  $ make
  $ sudo make install

在host4上安裝manager節點

MHA的manager節點提供masterha_manager,masterha_master_switch等命令行的功能,依賴與Perl模塊。在安裝manager節點之前,你需要安裝以下prel模塊,另外別忘了在manager節點安裝node節點。
MHA Node package DBD::mysql Config::Tiny Log::Dispatch Parallel::ForkManager Time::HiRes (included from Perl v5.7.3)

RHEL/Centos系統

## Install dependent Perl modules
  # yum install perl-DBD-MySQL
  # yum install perl-Config-Tiny
  # yum install perl-Log-Dispatch
  # yum install perl-Parallel-ForkManager

  ## Install MHA Node, since MHA Manager uses some modules provided by MHA Node.
  # rpm -ivh mha4mysql-node-X.Y-0.noarch.rpm

  ## Finally you can install MHA Manager
  # rpm -ivh mha4mysql-manager-X.Y-0.noarch.rpm

Ubuntu/Debian系統

  ## Install dependent Perl modules
  # apt-get install libdbd-mysql-perl
  # apt-get install libconfig-tiny-perl
  # apt-get install liblog-dispatch-perl
  # apt-get install libparallel-forkmanager-perl

  ## Install MHA Node, since MHA Manager uses some modules provided by MHA Node.
  # dpkg -i mha4mysql-node_X.Y_all.deb

  ## Finally you can install MHA Manager
  # dpkg -i mha4mysql-manager_X.Y_all.deb

源碼安裝

  ## Install dependent Perl modules
  # MHA Node (See above)
  # Config::Tiny
  ## perl -MCPAN -e "install Config::Tiny"
  # Log::Dispatch
  ## perl -MCPAN -e "install Log::Dispatch"
  # Parallel::ForkManager 
  ## perl -MCPAN -e "install Parallel::ForkManager"
  ## Installing MHA Manager
  $ tar -zxf mha4mysql-manager-X.Y.tar.gz
  $ perl Makefile.PL
  $ make
  $ sudo make install

創建配置文件

下一步就是創建manager的配置文件,參數主要包括mysql server的用戶名,密碼,複製賬戶的用戶名和密碼,工作目錄等。所有的參數列表詳見parameter表。

manager_host$ cat /etc/app1.cnf
  
  [server default]
  # mysql user and password
  user=root
  password=mysqlpass
  ssh_user=root
  # working directory on the manager
  manager_workdir=/var/log/masterha/app1
  # working directory on MySQL servers
  remote_workdir=/var/log/masterha/app1
  
  [server1]
  hostname=host1
  
  [server2]
  hostname=host2
  
  [server3]
  hostname=host3

注意到host1是當前的master,MHA會自動檢測到它。

檢查SSH連接

MHA manager通過SSH訪問所有的node節點,各個node節點也同樣需要通過SSH來相互發送不同的relay log 文件,所以有必要在每一個node和manager上配置SSH無密碼登陸。MHAmanager可通過masterha_check_ssh腳本檢測SSH連接是否配置正常。

# masterha_check_ssh --conf=/etc/app1.cnf
  
  Sat May 14 14:42:19 2011 - [warn] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  Sat May 14 14:42:19 2011 - [info] Reading application default configurations from /etc/app1.cnf..
  Sat May 14 14:42:19 2011 - [info] Reading server configurations from /etc/app1.cnf..
  Sat May 14 14:42:19 2011 - [info] Starting SSH connection tests..
  Sat May 14 14:42:19 2011 - [debug]  Connecting via SSH from root@host1(192.168.0.1) to root@host2(192.168.0.2)..
  Sat May 14 14:42:20 2011 - [debug]   ok.
  Sat May 14 14:42:20 2011 - [debug]  Connecting via SSH from root@host1(192.168.0.1) to root@host3(192.168.0.3)..
  Sat May 14 14:42:20 2011 - [debug]   ok.
  Sat May 14 14:42:21 2011 - [debug]  Connecting via SSH from root@host2(192.168.0.2) to root@host1(192.168.0.1)..
  Sat May 14 14:42:21 2011 - [debug]   ok.
  Sat May 14 14:42:21 2011 - [debug]  Connecting via SSH from root@host2(192.168.0.2) to root@host3(192.168.0.3)..
  Sat May 14 14:42:21 2011 - [debug]   ok.
  Sat May 14 14:42:22 2011 - [debug]  Connecting via SSH from root@host3(192.168.0.3) to root@host1(192.168.0.1)..
  Sat May 14 14:42:22 2011 - [debug]   ok.
  Sat May 14 14:42:22 2011 - [debug]  Connecting via SSH from root@host3(192.168.0.3) to root@host2(192.168.0.2)..
  Sat May 14 14:42:22 2011 - [debug]   ok.
  Sat May 14 14:42:22 2011 - [info] All SSH connection tests passed successfully.

如果有報錯,則表示SSH配置有問題,影響MHA工作。你需要修復它並重試,通常的錯誤都是SSH public key認證沒有正確配置。

檢查複製配置

爲了讓MHA正常工作,所有的master和slave必須在配置文件中正確配置,MHA可通過masterha_check_repl 腳本檢測複製是否正確配置。

  manager_host$ masterha_check_repl --conf=/etc/app1.cnf
  ...
  MySQL Replication Health is OK.

如果有報錯,可通過查看日誌修復它。當前的master一定不能是slave,其他所有的slave必須正確從master中複製。常見的錯誤可參考 TypicalErrors 頁。

開啓Manager

當你正確配置了mysql複製,正確安裝了manager和node節點,SSH配置也正確,那麼下一步就是開啓manager,可通過 masterha_manager 命令開啓

  manager_host$ masterha_manager --conf=/etc/app1.cnf
  ....
  Sat May 14 15:58:29 2011 - [info] Connecting to the master host1(192.168.0.1:3306) and sleeping until it doesn't respond..

如果所有的配置都正確,masterha_manager會檢查mastermaster是否可用直到master崩潰。如果在監控master之前masterha_manager報錯,你可以檢查下logs並修改配置。所有的日誌都會以標準錯誤的方式打印出來,也可以在manager配置文件中指定錯誤日誌位置。典型的錯誤有複製配置問題,ssh無訪問relay log的權限問題。默認情況下masterha_manager不是運行在後臺,按下crtl+c鍵就會終止masterha_manager。

檢查manager狀態

當MHA manager啓動監控以後,如果沒有異常則不會打印任何信息。我們可通過masterha_check_status命令檢查manager的狀態,以下是範例

manager_host$ masterha_check_status --conf=/etc/app1.cnf
  app1 (pid:5057) is running(0:PING_OK), master:host1

app1是MHA內部的應用名稱,該名稱可在manager配置文件中指定,如果manager終止或者配置得有錯誤,將會顯示以下信息

  manager_host$ masterha_check_status --conf=/etc/app1.cnf
  app1 is stopped(1:NOT_RUNNING).

終止manager

你可以通過 masterha_stop命令來停止manager

manager_host$ masterha_stop --conf=/etc/app1.cnf
  Stopped app1 successfully.

如果無法停止,嘗試加--abort參數,知道了怎麼停止,下面我們重新開啓manager。

 

測試master的自動故障轉移

現在master運行正常,manager監控也正常,下一步就是停止master,測試自動故障轉移,你可以簡單地停止master上的mysqld服務

 

  host1$  killall -9 mysqld mysqld_safe

這時候檢查manager的log日誌,看看host2是否成功成爲新的master,並且host3從host2中複製。

當完成一次正常的故障轉移後,manager進程將會終止。如果你需要將manager進程運行在後臺,可運行如下指令,或者通過安裝daemontools來實現(這裏略)

manager_host$ nohup masterha_manager --conf=/etc/app1.cnf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &

 

Writing an application configuration file

爲了MHA正常運行,你需要創建一個配置文件並設置參數,參數主要包括每個mysql進程所在的服務器的用戶名和密碼,mysql服務的用戶名和密碼,工作目錄等等。整個參數列表設置詳細請見Parameters 頁。

下面是一個配置文件的設置範例

manager_host$ cat /etc/app1.cnf



[server default]

# mysql user and password

user=root

password=mysqlpass

# working directory on the manager

manager_workdir=/var/log/masterha/app1

# manager log file

manager_log=/var/log/masterha/app1/app1.log

# working directory on MySQL servers

remote_workdir=/var/log/masterha/app1



[server1]

hostname=host1



[server2]

hostname=host2



[server3]

hostname=host3

所有的參數設置必須是"param=value"格式,打個比方,以下設置時錯誤的。

[server1]

hostname=host1

# incorrect: must be"no_master=1"

no_master

Application-scope參數必須寫在[server default]塊下,而在 [serverN]塊下,你需要設置的是local-scope參數,比如hostname是一個local-scope參數,所以必須寫在這個塊下面。塊名稱必須是字母”server”開頭。

 

Writing a global configuration file

如果你計劃只用一臺manager管理兩個或以上的master-slave對,那麼建議你創建一個全局配置文件,這樣你就不需要爲每一個複製都配置相同的參數。如果你創建了一個文件/etc/masterha_default.cnf,那麼它默認就是全局配置文件。

你可以在全局配置文件中設置application scope參數,例如,如果所有的mysql服務器的管理賬戶和密碼都是一樣的,你就可以在這裏設置user和password

以下是全局配置文件範例

Global configuration file (/etc/masterha_default.cnf)

[serverdefault]

user=root

password=rootpass

ssh_user=root

master_binlog_dir= /var/lib/mysql

remote_workdir=/data/log/masterha

secondary_check_script= masterha_secondary_check-s remote_host1 -s remote_host2

ping_interval=3

master_ip_failover_script=/script/masterha/master_ip_failover

shutdown_script= /script/masterha/power_manager

report_script= /script/masterha/send_master_failover_mail

以上這些參數可適用於所有的applications。

Application配置文件應該被單獨配置,以下是app1(host1-4)和app2(host11-14)的範例

app1:

manager_host$ cat /etc/app1.cnf



[server default]

manager_workdir=/var/log/masterha/app1

manager_log=/var/log/masterha/app1/app1.log



[server1]

hostname=host1

candidate_master=1



[server2]

hostname=host2

candidate_master=1



[server3]

hostname=host3



[server4]

hostname=host4

no_master=1

app2:

manager_host$ cat /etc/app2.cnf



[server default]

manager_workdir=/var/log/masterha/app2

manager_log=/var/log/masterha/app2/app2.log



[server1]

hostname=host11

candidate_master=1



[server2]

hostname=host12

candidate_master=1



[server3]

hostname=host13



[server4]

hostname=host14

no_master=1

Requirements and Limitations

1 這一部分做簡要翻譯,安裝MHA的依賴和限制

2 SSH public key認證

3 僅支持Liunx操作系統

4 只有一臺master能被設置成readonly=0,其他設置爲只讀

5如果是Master1 -> Master2-> Slave3這樣的三級複製框架,在配置文件中只需要設置master1和master2這樣的二級複製結構,並設置multi_tier_slave=1來支持三級複製結構。

6 MHA僅支持mysql 5.0及以後的版本

7 mysqlbinlog必須是3.3及以上版本

8 log-bin必須在每一個可稱爲master的mysql服務器上設置

9 所有mysql服務器的複製過濾規則必須一致

10 必須在能成爲master的服務器上設置複製賬戶

11所有Mysql服務器上必須設置relay_log_purge=1,使得能夠保存一段時間的relay log

12 基於語句的複製時,不要使用load datainfile命令

 

What MHA does on monitoring and failover

這一部分很多內容與上述重複,我只做簡要翻譯,在監控和故障轉移過程中,MHA主要做了以下幾項工作

Verifying replicationsettings and identifying the current master

覈實複製配置並識別出當前的master

Monitoring the masterserver

Detecting the masterserver failure

Verifying slaveconfigurations again

Shutting down failedmaster server (optional)

Recovering a newmaster

Activating the newmaster

Recovering the restslaves

Notifications(optional)

監控master server直到master崩潰,在這一步時manager不再監控slave的狀態。所以如果需要添加或刪除slave節點,最好重新修改manager配置文件並重啓MHA

檢測到master故障

重新掃描配置文件,各種重連,覈實master確實已經崩潰。如果最近一次的報錯和現在一樣並且時間相隔非常之短,MHA將會停止繼續報錯並進入下一步

關閉崩潰的主機(可選),防止錯誤繼續擴散

重新選舉出一個新的master。如果崩潰的主機能夠通過SSH連接,則複製崩潰主機的binlog到最新的slave上,並指向他的end_log_pos。在選擇新的master上遵守manager上的配置文件,如果某個slave能成爲master,則設置candidate_master=1。如果某個slave永遠不能成爲master,則設置no_master=1。識別出最新的slave並將其選舉爲新的master,最新的slave即接受到最新的relay log的那臺slave。

激活新的master

重新設置其餘的slave使其指向新選舉出來的master

發送通告(可選),比如發送郵件,禁用新master上backup工作等,可通過 report_script腳本設置

 

What MHA does on online(fast) master switch

簡要翻譯,在線master切換過程中,MHA主要做了以下工作

Verifying replication settings and identifying the current master

Identifying the new mater

Rejecting writes on the current master

Waiting for all slaves to catch up replication

Granting writes on the new master

Switching replication on all the rest slaves

覈實複製配置並識別出當前的master,這個過程還會檢測以下幾個條件是否滿足:

Slave上的IO線程is running

Salve上的SQL線程is running

Slave上所有的複製延遲少於2s

在master上的update操作沒有超過2秒的

識別出新的master

在當前master上執行FLUSHTABLES WITH READ LOCK阻塞寫操作防止數據一致性問題

等待所有的slave的複製跟上master

在新的master上執行SHOW MASTER STATUS,記錄下binlog文件名稱和pos,並執行SET GLOBAL read_only=0授權其寫操作

在其他salve上並行執行CHANGE MASTER, START SLAVE,指向新的master,並start slave

 

 

Parameters

MHA manager配置參數列表如下

Parameter Name

Required?

Parameter Scope

Default Value

Example

hostname

Yes

Local Only

-

hostname=mysql_server1, hostname=192.168.0.1, etc

ip

No

Local Only

gethostbyname($hostname)

ip=192.168.1.3

port

No

Local/App/Global

3306

port=3306

ssh_host

No

Local Only

same as hostname

ssh_host=mysql_server1, ssh_host=192.168.0.1, etc

ssh_ip

No

Local Only

gethostbyname($ssh_host)

ssh_ip=192.168.1.3

ssh_port

No

Local/App/Global

22

ssh_port=22

ssh_connection_timeout

No

Local/App/Global

5

ssh_connection_timeout=20

ssh_options

No

Local/App/Global

""(empty string)

ssh_options="-i /root/.ssh/id_dsa2"

candidate_master

No

Local Only

0

candidate_master=1

no_master

No

Local Only

0

no_master=1

ignore_fail

No

Local Only

0

ignore_fail=1

skip_init_ssh_check

No

Local Only

0

skip_init_ssh_check=1

skip_reset_slave

No

Local/App/Global

0

skip_reset_slave=1

user

No

Local/App/Global

root

user=mysql_root

password

No

Local/App/Global

""(empty string)

password=rootpass

repl_user

No

Local/App/Global

Master_User value from SHOW SLAVE STATUS

repl_user=repl

repl_password

No

Local/App/Global

- (current replication password)

repl_user=replpass

disable_log_bin

No

Local/App/Global

0

disable_log_bin=1

master_pid_file

No

Local/App/Global

""(empty string)

master_pid_file=/var/lib/mysql/master1.pid

ssh_user

No

Local/App/Global

current OS user

ssh_user=root

remote_workdir

No

Local/App/Global

/var/tmp

remote_workdir=/var/log/masterha/app1

master_binlog_dir

No

Local/App/Global

/var/lib/mysql

master_binlog_dir=/data/mysql1,/data/mysql2

log_level

No

App/Global

info

log_level=debug

manager_workdir

No

App

/var/tmp

manager_workdir=/var/log/masterha

client_bindir

No

App

-

client_bindir=/usr/mysql/bin

client_libdir

No

App

-

client_libdir=/usr/lib/mysql

manager_log

No

App

STDERR

manager_log=/var/log/masterha/app1.log

check_repl_delay

No

App/Global

1

check_repl_delay=0

check_repl_filter

No

App/Global

1

check_repl_filter=0

latest_priority

No

App/Global

1

latest_priority=0

multi_tier_slave

No

App/Global

0

multi_tier_slave=1

ping_interval

No

App/Global

3

ping_interval=5

ping_type

No

App/Global

SELECT

ping_type=CONNECT

secondary_check_script

No

App/Global

null

secondary_check_script= masterha_secondary_check -s remote_dc1 -s remote_dc2

master_ip_failover_script

No

App/Global

null

master_ip_failover_script=/usr/local/custom_script/master_ip_failover

master_ip_online_change_script

No

App/Global

null

master_ip_online_change_script= /usr/local/custom_script/master_ip_online_change

shutdown_script

No

App/Global

null

shutdown_script= /usr/local/custom_script/master_shutdown

report_script

No

App/Global

null

report_script= /usr/local/custom_script/report

init_conf_load_script

No

App/Global

null

report_script= /usr/local/custom_script/init_conf_loader

Local Scope: Per-server scope parameters. Local scope parameters should be set under[server_xxx] blocks within application configuration file. App Scope: Parameters for each {master, slaves} pair. These parameters should be set under a [server_default]block withinapplication configuration file. Global Scope: Parameters for all {master, slaves} pairs. Global scope parameters are useful only when you manage multiple {master, slaves} pairs from single manager server. These parameters should be set in a global configuration file.

hostname

Hostname or IP address of the target MySQL server. This parameteris mandatory, and must be configured under [server_xxx]blockswithin applicationconfiguration file.

MySQL服務器的主機名稱或IP地址,寫在[server_xxx]下,xxx相當於各個mysql服務器。

ip

IP address of the target MySQL server. Default isgethostbyname($hostname). MHA Manager and MHA Node internally uses this IPaddress to connect via MySQL and SSH. Normally you don't need to configure thisparameter because it's automatically resolved from hostname parameter.

通常不需要配置

port

Port number of the target MySQL server. Default is 3306. MHAconnects to MySQL servers by using IP address and port.

Mysql服務的端口號,默認3306.

ssh_host

Ssh所在服務器,默認和hostname一樣,不需要配置。

ssh_ip

(Supported from 0.53) IP address of the target MySQL server thatis used via SSH. Default is gethostbyname($ssh_host).通常不用配置

ssh_port

(Supported from 0.53) Port number of the target MySQL server usedvia SSH. Default is 22.

ssh_connection_timeout

(Supported from 0.54) Default is 5 seconds. Before adding thisparameter timeout was hard coded.

ssh_options

(Supported from 0.53) Additional SSH command line options.

candidate_master

[server_xxx]下配置,值爲1代表該mysql可以成爲master,如果有兩個以上mysql都設置爲1,那麼誰寫在前面,誰的優先級就高。

no_master

設置爲1代表該mysql永遠無法成爲master,通常在RAID0或者遠程數據中心設置該mysql的no_master爲1,或者manager和slave複用的主機上也這麼設置。

ignore_fail

默認情況下,manager在slave出現故障的時候不會自動故障轉移,比如SSH連接或者SQL線程有問題等。如果設置爲1則該slave出現故障時會自動切換

skip_init_ssh_check

跳過初始化過程中的ssh檢查

skip_reset_slave

0.56版本後支持當master崩潰,跳過執行resetslave

user

mysql管理賬戶,最好是root賬戶,默認也就是root賬戶

password

user對應的mysql賬戶密碼

repl_user

複製賬戶,通常不用設置

repl_password

複製賬戶對應的密碼,通常不用設置

disable_log_bin

如果這個選項被設置,那麼當將不同的relay log應用到各個slave的過程中,slave不產生binlog

master_pid_file

設置master的pid文件,通常不用設置

ssh_user

默認是當前登陸manager的OS的用戶,需要擁有讀取mysql binlog和relay log的權限

remote_workdir

每一個MHA node節點產生log文件的目錄,如果不存在MHA會自動創建,需要給出相應目錄的權限,默認在/var/tmp,最好自己指定

master_binlog_dir

master產生binlog文件的目錄,最好自己指定,因爲當master崩潰後,如果master還能連通SSH,就會複製其binlog,默認路徑爲/var/lib/mysql.

log_level

通常不用設置,表示日誌級別

manager_workdir

manager產生自身狀態的文件的目錄,默認/var/tmp

client_bindir

If MySQL command line utilities are installed under a non-standarddirectory, use this option to set the directory.

client_libdir

If MySQL libraries are installed under a non-standard directory,use this option to set the directory.

manager_log

Manager日誌的全路徑名稱,若未設置,默認輸出到STDOUT/STDERR;

如果手動故障切換時,MHA則忽略參數設置,而直接輸出到STDOUT/STDERR。

Full path file name that MHA Manager generates logs. If not set,MHA Manager prints to STDOUT/STDERR. When executing manual failover(interactive failover), MHA Manager ignores manager_log setting and alwaysprints to STDOUT/STDERR.

check_repl_delay

默認情況下,如果某個slave的複製延遲超過100MB,MHA則不會使其成爲新的master,因爲這需要很長的時間來恢復。如果設爲0,MHA在選舉新的master時會忽略複製延遲

若設置該參數爲0,MHA在選擇新的Master時,會忽略複製延遲。當某個mysql設置candidate_master=1時,再將check_repl_delay設置爲0就很有必要,確保它能成爲新的master

check_repl_filter

檢查複製過濾,默認情況下如果master和slave擁有不同的過濾規則就會報錯,通過設置爲0可以忽略複製過濾檢查,當然你得特別小心,確保沒有問題。

latest_priority

默認情況下MHA在master崩潰後,選舉複製延遲最低的slave爲新的master,但允許你自己控制每個slave成爲主節點的優先級和順序,通過設置該參數爲0,並由寫入candidate_master=1的mysql服務器順序決定。

multi_tier_slave

從0.52版本開始,MHA支持多級複製配置。默認情況下,不允許設置三層以上的複製結構,比如h2從h1複製,而h3又從h2複製,MHA將會報錯。通過設置multi_tier_slave參數,則h1崩潰後,h2被選舉爲新的master,而h3依舊從h2複製

ping_interval

這個參數指定了MHA manager應該多長時間執行ping SQL一次去連接master,當超過三次連接不上master,manager將判定master已經死亡。默認3秒ping一次,所以,總的檢測時間大概就是12秒。如果由於連接錯誤或者連接數過多而導致的錯誤不會計入master死亡統計。

ping_type

0.53版本默認連接到master並執行select 1,即ping_type=SELECT。但是在某些場合,更好地方式是通過創建連接後又斷開連接的方式,因爲這個更加嚴格,並且能更快地發現tcp連接問題,即ping_type=CONNECT。從5.6版本以後還支持ping_type=INSERT

secondary_check_script

通常情況下,我們建議使用兩個或以上的網絡路由來檢測master是否存活。但默認情況下,manager僅通過單個路由來檢查,即from Manager節點to Master節點。MHA實際上可以支持多個路由檢測,只要通過調用額外的腳本masterha_check_script即可,下面是範例。

  secondary_check_script = masterha_secondary_check -s remote_host1 -s remote_host2

masterha_secondary_check腳本在manager節點上,通常情況下能夠運行良好。

在這個範例中,MHA通過Manager-(A)->remote_host1-(B)->master_host

和Manager-(A)->remote_host2-(B)->master_host來檢測master狀態。如果在上述兩步中都是A連接成功而B連接不成功,則MHA能夠判斷是master確實已經死亡並返回0,進行故障切換。如果A連接不成功,該腳本會返回2,MHA認爲可能是自身的網絡問題而不進行故障轉移。如果此時B連接成功,則實際上master是存活的。通俗地說,remote_host1和remote_host2應該被設置在不同的網段上。

該腳本在通用場合中都適用,當然你也可以自己寫腳本來實現更多的功能。下面是該腳本的參數列表。

--user=(SSH username of the remote hosts. ssh_user parameter value will be passed)--master_host=(master's hostname) --master_ip=(master's ip address) --master_port=(master's port number)

注意該腳本需要依賴於IO::Socket::INET Perl包,Perl v5.6.0中默認已經包括。而該腳本允許連接任何一個遠程服務器,所以需要配置SSH public key。並且,該腳本嘗試建立遠程服務器到master的tcp連接,意味着如果tcp連接成功,則mysql配置文件中的max_connections設置不受影響,而aborts_connects的值會自動加1

master_ip_failover_script

HA方案中,很多情況下人們會在master上綁定一個虛擬IP。當master崩潰的時候,軟件比如Keepalived會將虛擬IP重新指向正常的Server。

通用的方法就是創建一個全局的目錄庫,在庫中存放着所有應用和IP地址之間的映射關係,用以取代VIP。在這種方案下,如果master崩潰,那麼你就需要修改這個目錄庫。

都各有優缺點,MHA不會強制使用哪一種,允許用戶使用任何的ip漂移技術。master_ip_failover_script 腳本能用於該目的。換句話說,你需要自己寫腳本實現應用層連接到新的master,並且必須定義master_ip_failover_script 腳本參數,下面是使用範例

  master_ip_failover_script= /usr/local/sample/bin/master_ip_failover

MHA Manager需要調用3次該腳本,第一次是在啓動監控master之前(檢查腳本是否可用),,第二次是在調用shutdown_script腳本之前,而第三次是在新的Master應用完所有的

relay logs之後。MHA Manager會傳遞如下參數(這些參數不需要你自己配置):

Checking phase
--command=status --ssh_user=(current master's ssh username) --orig_master_host=(current master's hostname) --orig_master_ip=(current master's ip address) --orig_master_port=(current master's port number)
Current master shutdown phase
--command=stop or stopssh --ssh_user=(dead master's ssh username, if reachable via ssh) --orig_master_host=(current(dead) master's hostname) --orig_master_ip=(current(dead) master's ip address) --orig_master_port=(current(dead) master's port number)
New master activation phase
--command=start --ssh_user=(new master's ssh username) --orig_master_host=(dead master's hostname) --orig_master_ip=(dead master's ip address) --orig_master_port=(dead master's port number) --new_master_host=(new master's hostname) --new_master_ip=(new master's ip address) --new_master_port(new master's port number) --new_master_user=(new master's user) --new_master_password(new master's password)

如果你在master上使用了VIP,當master關閉階段你可能不需要做任何事,只要你能夠讓VIP漂移到新的master。如果你使用的目錄庫方案,你可能需要刪除或更新在master上的記錄。在新的master激活階段,你可以在新的master上插入/更新一條記錄。並且,你可以做任何事使得應用層能夠向新master中插入數據,比如設置read_only=0,創建用戶的寫權限等。

MHA manager會檢查這個腳本返回的運行結果,如果返回0或10,則MHA manager繼續運行。如果返回的不是0或10,mangaer就會終止。默認參數空置,所以MHA manager不會做任何事。

master_ip_online_change_script

這個和master_ip_failover_script參數相似,但它並不是用在master故障切換上,而是用在master在線手動切換命令上,傳遞參數過程如下

 

Current master write freezing phase
--command=stop or stopssh --orig_master_host=(current master's hostname) --orig_master_ip=(current master's ip address) --orig_master_port=(current master's port number) --orig_master_user=(current master's user) --orig_master_password=(current master's password) --orig_master_ssh_user=(from 0.56, current master's ssh user) --orig_master_is_new_slave=(from 0.56, notifying whether the orig master will be new slave or not)
New master granting write phase
--command=start --orig_master_host=(orig master's hostname) --orig_master_ip=(orig master's ip address) --orig_master_port=(orig master's port number) --new_master_host=(new master's hostname) --new_master_ip=(new master's ip address) --new_master_port(new master's port number) --new_master_user=(new master's user) --new_master_password=(new master's password) --new_master_ssh_user=(from 0.56, new master's ssh user)

shutdown_script

你或許希望強制關閉master所在的服務器,這樣就可以防止災難擴散,以下是範例

  shutdown_script= /usr/local/sample/bin/power_manager

MHA manager包中有一個範例腳本,在調用該命令前,MHA內部會檢查master能否通過SSH連接。如果可連接(OS存活但是mysqld服務終止),MHA manager傳遞如下參數

--command=stopssh --ssh_user=(ssh username so that you can connect to the master) --host=(master's hostname) --ip=(master's ip address) --port=(master's port number) --pid_file=(master's pid file)

If the master is not reachable via SSH, MHA Manager passes thefollowing arguments.

--command=stop --host=(master's hostname) --ip=(master's ip address)

該腳本以如下方式運行。如果--command=stopssh被通過,則該腳本會通過ssh在mysqld和mysqld_safe進程上執行kill -9操作。如果—pid_file同樣被通過,該腳本就會嘗試只殺死代理的進程,而不是所有的mysql進程,這在單個master上運行多實例時是非常有用的。如果成功地通過SSH停止了該服務,則腳本運行結果返回10,並且後續manager會通過SSH連接到master並保存必要的binlog。如果該腳本無法通過SSH連接到master或者—command命令通過的話,那麼該腳本將會嘗試關閉機器電源。關閉電源依賴於H/W。如果電源關閉成功,該腳本返回0,否則返回1。當MHA接到返回的0時即開始故障切換。如果返回的代碼既不是0也不是10,MHA將會終止故障轉移工作。缺省參數爲空,所以默認情況下MHA不對其做任何事。

並且,MHA在開始監控之後就會調用該腳本,以下參數將會在這個時候被傳遞過去,你可以在這裏檢測腳本設置。是否控制電源很多程度上決定於H/W,所以非常簡易在這裏檢測電源狀態。如果你哪裏配置錯了,在啓動監控的時候你需要特別小心。

--command=status --host=(master's hostname) --ip=(master's ip address)

report_script

當故障切換完成或返回錯誤的時候,你或許希望可以發送一個報告給你,report_script參數可適用於這種場合。MHA manager將會傳遞如下參數

--orig_master_host=(dead master's hostname) --new_master_host=(new master's hostname) --new_slave_hosts=(new slaves' hostnames, delimited by commas) --subject=(mail subject) --body=(body)

默認情況下該參數爲空,即MHA不對其做任何事。在MHAmanager包的

Default parameter is empty, so MHA Manager does not invokeanything by default. /samples/scripts/send_report目錄下有使用範例。

init_conf_load_script

這個腳本能被應用於你不想在配置文件中填寫清楚的文本信息,比如密碼和複製賬戶的密碼。通過從這個腳本中返回name=value對,你可以重寫這個全局配置文件。範例如下

  #!/usr/bin/perl
  
  print "password=$ROOT_PASS\n";
  print "repl_password=$REPL_PASS\n";

缺省參數爲空,所以MHA不對其做任何事。

 

Command reference

這一部分不做翻譯,通常情況下只需要運行範例的命令,參數的詳細介紹請見官方文檔。

 

masterha_manager: 開啓MHA Manager

  # masterha_manager --conf=/etc/conf/masterha/app1.cnf

masterha_master_switch:切換master,分故障master切換和在線master切換兩種

交互式故障master切換

$ masterha_master_switch --master_state=dead --conf=/etc/app1.cnf--dead_master_host=host1

指定新的master

$ masterha_master_switch --master_state=dead --conf=/etc/app1.cnf --dead_master_host=host1 --new_master_host=host5

非交互式

$ masterha_master_switch --master_state=dead --conf=/etc/conf/masterha/app1.cnf --dead_master_host=host1 --new_master_host=host2 --interactive=0

 

在線master切換

$ masterha_master_switch --master_state=alive --conf=/etc/app1.cnf --new_master_host=host2

masterha_check_status:檢查MHA運行狀態

$ masterha_check_status --conf=/path/to/app1.cnf
  app1 (pid:8368) is running(0:PING_OK), master:host1
  $ echo $?
  0

Status Code(Exit code)

Status String

Description

0

PING_OK

Master is running and MHA Manager is monitoring. Master state is alive.

1

---

Unexpected error happened. For example, config file does not exist. If this error happens, check arguments are valid or not.

2

NOT_RUNNING

MHA Manager is not running. Master state is unknown.

3

PARTIALLY_RUNNING

MHA Manager main process is not running, but child processes are running. This should not happen and should be investigated. Master state is unknown.

10

INITIALIZING_MONITOR

MHA Manager is just after startup and initializing. Wait for a while and see how the status changes. Master state is unknown.

20

PING_FAILING

MHA Manager detects ping to master is failing. Master state is maybe down.

21

PING_FAILED

MHA Manager detects either a) ping to master failed three times, b) preparing for starting master failover. Master state is maybe down.

30

RETRYING_MONITOR

MHA Manager internal health check program detected that master was not reachable from manager, but after double check MHA Manager verified the master is alive, and currently waiting for retry. Master state is very likely alive.

31

CONFIG_ERROR

There are some configuration problems and MHA Manager can't monitor the target master. Check a logfile for detail. Master state is unknown.

32

TIMESTAMP_OLD

MHA Manager detects that ping to master is ok but status file is not updated for a long time. Check whether MHA Manager itself hangs or not. Master state is unknown.

50

FAILOVER_RUNNING

MHA Manager confirms that master is down and running failover. Master state is dead.

51

FAILOVER_ERROR

MHA Manager confirms that master is down and running failover, but failed during failover. Master state is dead.

 

masterha_check_repl:檢查複製健康狀態

manager_host$ masterha_check_repl --conf=/etc/app1.cnf
  ...
  MySQL Replication Health is OK.

 

masterha_stop:停止MHA manager運行

manager_host$ masterha_stop --conf=/etc/app1.cnf
  Stopped app1 successfully.

 

Masterha_conf_host:在配置文件中添加或移除host

# masterha_conf_host --command=add--conf=/etc/conf/masterha/app1.cnf --hostname=db101

Then the following lines will be added to theconf file.

[server_db101]

hostname=db101

You can add several parameters in the configfile by passing --param parameters, separated by semi-colon(;).

# masterha_conf_host --command=add--conf=/etc/conf/masterha/app1.cnf --hostname=db101 --block=server100--params="no_master=1;ignore_fail=1"

The following lines will be added to the conffile.

[server100]

hostname=db101

no_master=1

ignore_fail=1

You can also remove specified block. Thebelow command will remove the etire block server100.

# masterha_conf_host --command=delete--conf=/etc/conf/masterha/app1.cnf --block=server100

masterha_conf_host takes below arguments

master_check_ssh:ssh認證檢查

# masterha_check_ssh --conf=/etc/app1.cnf
  
  Sat May 14 14:42:19 2011 - [warn] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  Sat May 14 14:42:19 2011 - [info] Reading application default configurations from /etc/app1.cnf..
  Sat May 14 14:42:19 2011 - [info] Reading server configurations from /etc/app1.cnf..
  Sat May 14 14:42:19 2011 - [info] Starting SSH connection tests..
  Sat May 14 14:42:19 2011 - [debug]  Connecting via SSH from root@host1(192.168.0.1) to root@host2(192.168.0.2)..
  Sat May 14 14:42:20 2011 - [debug]   ok.
  Sat May 14 14:42:20 2011 - [debug]  Connecting via SSH from root@host1(192.168.0.1) to root@host3(192.168.0.3)..
  Sat May 14 14:42:20 2011 - [debug]   ok.
  Sat May 14 14:42:21 2011 - [debug]  Connecting via SSH from root@host2(192.168.0.2) to root@host1(192.168.0.1)..
  Sat May 14 14:42:21 2011 - [debug]   ok.
  Sat May 14 14:42:21 2011 - [debug]  Connecting via SSH from root@host2(192.168.0.2) to root@host3(192.168.0.3)..
  Sat May 14 14:42:21 2011 - [debug]   ok.
  Sat May 14 14:42:22 2011 - [debug]  Connecting via SSH from root@host3(192.168.0.3) to root@host1(192.168.0.1)..
  Sat May 14 14:42:22 2011 - [debug]   ok.
  Sat May 14 14:42:22 2011 - [debug]  Connecting via SSH from root@host3(192.168.0.3) to root@host2(192.168.0.2)..
  Sat May 14 14:42:22 2011 - [debug]   ok.
  Sat May 14 14:42:22 2011 - [info] All SSH connection tests passed successfully.
 

purge_relay_logs script:刪除舊的relay log

[app@slave_host1]$ cat /etc/cron.d/purge_relay_logs
  # purge relay logs at 5am
  0 5 * * * app /usr/bin/purge_relay_logs --user=root --password=PASSWORD --disable_relay_log_purge >> /var/log/masterha/purge_relay_logs.log 2>&1
 

Monitoring multiple applications

你或許在一臺機子上希望監控多套master-salve複製,這非常容易,只要爲application2創建一個新的配置文件並啓動manager

  # masterha_manager --conf=/etc/conf/masterha/app1.cnf
  # masterha_manager --conf=/etc/conf/masterha/app2.cnf

如果你在app1和app2上有一些共有的參數,可在全局配置文件中配置。

 

Using with clustering software

如果你在master上使用虛擬IP,你可能已經使用了類似於Pacemaker的集羣軟件。如果你使用了相似的工具,你或許需要使用它們來管理虛擬IP地址,而不是讓所有的事都由MHA完成。MHA僅用於故障切換,所以你需要使用配合使用其他集羣工具來實現高可用。

 

下面是一個簡要的Pacemaker配置(Heartbeat v1 模式)

# /etc/ha.d/haresources on host2

host2 failover_start IPaddr::192.168.0.3

# failover_start script example

 

start)

`masterha_master_switch --master_state=dead--interactive=0 --wait_on_failover_error=0 --dead_master_host=host1--new_master_host=host2`

exit

 

stop)

# do nothing

# Application configuration file:

[server1]

hostname=host1

candidate_master=1

 

[server2]

hostname=host2

candidate_master=1

 

[server3]

hostname=host3

no_master=1

因爲數據文件不是共享的,所以數據資源也不用被集羣工具或DRBD管理。處於這個目的,集羣工具僅僅實現一個執行masterha_master_switch腳本和虛擬IP漂移的功能,你也可以自己使用手工腳本實現這些功能。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章