MySQL高可用性之MHA

2018-06-08 17:42 更新

防偽碼:舉杯邀明月,對影成三人。

MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,它由日本DeNA公司youshimaton(現(xiàn)就職于Facebook公司)開發(fā),是一套優(yōu)秀的作為MySQL高可用性環(huán)境下故障切換和主從提升的高可用軟件。在MySQL故障切換過程中,MHA能做到在0~30秒之內(nèi)自動完成數(shù)據(jù)庫的故障切換操作,并且在進行故障切換的過程中,MHA能在最大程度上保證數(shù)據(jù)的一致性,以達到真正意義上的高可用。

MHA里有兩個角色一個是MHA Node(數(shù)據(jù)節(jié)點)另一個是MHA Manager(管理節(jié)點)。

MHA Manager可以單獨部署在一臺獨立的機器上管理多個master-slave集群,也可以部署在一臺slave節(jié)點上。MHA Node運行在每臺MySQL服務(wù)器上,MHA Manager會定時探測集群中的master節(jié)點,當master出現(xiàn)故障時,它可以自動將最新數(shù)據(jù)的slave提升為新的master,然后將所有其他的slave重新指向新的master。整個故障轉(zhuǎn)移過程對應(yīng)用程序完全透明。

在MHA自動故障切換過程中,MHA試圖從宕機的主服務(wù)器上保存二進制日志,最大程度的保證數(shù)據(jù)的不丟失,但這并不總是可行的。例如,如果主服務(wù)器硬件故障或無法通過ssh訪問,MHA沒法保存二進制日志,只進行故障轉(zhuǎn)移而丟失了最新的數(shù)據(jù)。使用MySQL 5.5的半同步復(fù)制,可以大大降低數(shù)據(jù)丟失的風(fēng)險。MHA可以與半同步復(fù)制結(jié)合起來。如果只有一個slave已經(jīng)收到了最新的二進制日志,MHA可以將最新的二進制日志應(yīng)用于其他所有的slave服務(wù)器上,因此可以保證所有節(jié)點的數(shù)據(jù)一致性。

注:從MySQL5.5開始,MySQL以插件的形式支持半同步復(fù)制。如何理解半同步呢?首先我們來看看異步,全同步的概念:

異步復(fù)制(Asynchronous replication)

MySQL默認的復(fù)制即是異步的,主庫在執(zhí)行完客戶端提交的事務(wù)后會立即將結(jié)果返給給客戶端,并不關(guān)心從庫是否已經(jīng)接收并處理,這樣就會有一個問題,主如果crash掉了,此時主上已經(jīng)提交的事務(wù)可能并沒有傳到從上,如果此時,強行將從提升為主,可能導(dǎo)致新主上的數(shù)據(jù)不完整。

全同步復(fù)制(Fully synchronous replication)

指當主庫執(zhí)行完一個事務(wù),所有的從庫都執(zhí)行了該事務(wù)才返回給客戶端。因為需要等待所有從庫執(zhí)行完該事務(wù)才能返回,所以全同步復(fù)制的性能必然會收到嚴重的影響。

半同步復(fù)制(Semisynchronous replication)

介于異步復(fù)制和全同步復(fù)制之間,主庫在執(zhí)行完客戶端提交的事務(wù)后不是立刻返回給客戶端,而是等待至少一個從庫接收到并寫到relay log中才返回給客戶端。相對于異步復(fù)制,半同步復(fù)制提高了數(shù)據(jù)的安全性,同時它也造成了一定程度的延遲,這個延遲最少是一個TCP/IP往返的時間。所以,半同步復(fù)制最好在低延時的網(wǎng)絡(luò)中使用。

下面來看看半同步復(fù)制的原理圖:

總結(jié):異步與半同步異同

默認情況下MySQL的復(fù)制是異步的,Master上所有的更新操作寫入Binlog之后并不確保所有的更新都被復(fù)制到Slave之上。異步操作雖然效率高,但是在Master/Slave出現(xiàn)問題的時候,存在很高數(shù)據(jù)不同步的風(fēng)險,甚至可能丟失數(shù)據(jù)。
 MySQL5.5引入半同步復(fù)制功能的目的是為了保證在master出問題的時候,至少有一臺Slave的數(shù)據(jù)是完整的。在超時的情況下也可以臨時轉(zhuǎn)入異步復(fù)制,保障業(yè)務(wù)的正常使用,直到一臺salve追趕上之后,繼續(xù)切換到半同步模式。

 

工作原理

相較于其它HA軟件,MHA的目的在于維持MySQL Replication中Master庫的高可用性,其最大特點是可以修復(fù)多個Slave之間的差異日志,最終使所有Slave保持數(shù)據(jù)一致,然后從中選擇一個充當新的Master,并將其它Slave指向它。

-從宕機崩潰的master保存二進制日志事件(binlogevents)。

-識別含有最新更新的slave。

-應(yīng)用差異的中繼日志(relay log)到其它slave。

-應(yīng)用從master保存的二進制日志事件(binlogevents)。

-提升一個slave為新master。

-使其它的slave連接新的master進行復(fù)制。

 

目前MHA主要支持一主多從的架構(gòu),要搭建MHA,要求一個復(fù)制集群中必須最少有三臺數(shù)據(jù)庫服務(wù)器,一主二從,即一臺充當master,一臺充當備用master,另外一臺充當從庫,因為至少需要三臺服務(wù)器。

 

接下來部署MHA,具體的搭建環(huán)境如下

角色

IP地址

主機名

Server id

類型

os

Manager

192.168.1.101

Centos1


管理節(jié)點

Centos6.5x86_64

Master

192.168.1.102

Centos2

1

主mysql(寫入)

Centos6.5x86_64

Candicate

master

192.168.1.103

Centos3

2

從mysql(讀)

Centos6.5x86_64

slave

192.168.1.104

Centos4

3

從mysql(讀)

Centos6.5x86_64


其中master對外提供寫服務(wù),備選master(實際的slave,主機名centos3)提供讀服務(wù),slave也提供相關(guān)的讀服務(wù),一旦master宕機,將會把備選master提升為新的master,slave指向新的master,manager作為管理服務(wù)器。

一、基礎(chǔ)環(huán)境準備

1、 在配置好IP地址后檢查selinux,iptables設(shè)置,關(guān)閉 selinux ,iptables  服務(wù)以便后期主從同步不出錯

注:時間要同步

2、在四臺機器都配置epel源

3、建立ssh無交互登錄環(huán)境

Manager主機:

Master主機:

同上。

Candicatemaster主機:

Slave主機:

測試ssh無交互登錄

在其他主機上執(zhí)行同樣的測試操作。

4、配置hosts環(huán)境

查看每臺主機的hosts文件

二、配置mysql半同步復(fù)制

為了盡可能的減少主庫硬件損壞宕機造成的數(shù)據(jù)丟失,因此在配置MHA的同時建議配置成MySQL的半同步復(fù)制。

注:mysql半同步插件是由谷歌提供,具體位置/usr/local/mysql/lib/plugin/下,一個是master用的semisync_master.so,一個是slave用的semisync_slave.so,下面我們就來具體配置一下。

如果不清楚Plugin的目錄,用如下查找:

1、分別在主從節(jié)點上安裝相關(guān)的插件(master,Candicatemaster,slave)

在MySQL上安裝插件需要數(shù)據(jù)庫支持動態(tài)載入。檢查是否支持,用如下檢測:

所有mysql數(shù)據(jù)庫服務(wù)器,安裝半同步插件(semisync_master.so,semisync_slave.so)

其他mysql主機采用同樣的方法安裝

檢查Plugin是否已正確安裝:
mysql> show plugins;


mysql> select * from information_schema.plugins;

查看半同步相關(guān)信息

上圖可以看到半同復(fù)制插件已經(jīng)安裝,只是還沒有啟用,所以是off

2、修改my.cnf文件,配置主從同步:

注:若主MYSQL服務(wù)器已經(jīng)存在,只是后期才搭建從MYSQL服務(wù)器,在置配數(shù)據(jù)同步前應(yīng)先將主MYSQL服務(wù)器的要同步的數(shù)據(jù)庫拷貝到從MYSQL服務(wù)器上(如先在主MYSQL上備份數(shù)據(jù)庫,再用備份在從MYSQL服務(wù)器上恢復(fù))

master mysql主機:

server-id = 1
log-bin=mysql-bin
binlog_format=mixed

log-bin-index=mysql-bin.index

rpl_semi_sync_master_enabled=1

rpl_semi_sync_master_timeout=1000

rpl_semi_sync_slave_enabled=1

relay_log_purge=0

relay-log = relay-bin

relay-log-index = slave-relay-bin.index

注:

rpl_semi_sync_master_enabled=1  1表是啟用,0表示關(guān)閉

rpl_semi_sync_master_timeout=10000:毫秒單位,該參數(shù)主服務(wù)器等待確認消息10秒后,不再等待,變?yōu)楫惒椒绞健?/span>

 

Candicate master主機:

server-id = 2
log-bin=mysql-bin
binlog_format=mixed

log-bin-index=mysql-bin.index

relay_log_purge=0

relay-log = relay-bin

relay-log-index = slave-relay-bin.index

rpl_semi_sync_master_enabled=1

rpl_semi_sync_master_timeout=10000

rpl_semi_sync_slave_enabled=1

注:relay_log_purge=0,禁止 SQL 線程在執(zhí)行完一個 relay log 后自動將其刪除,對于MHA場景下,對于某些滯后從庫的恢復(fù)依賴于其他從庫的relay log,因此采取禁用自動刪除功能

 

Slave主機:

Server-id   = 3

log-bin    = mysql-bin

relay-log = relay-bin

relay-log-index = slave-relay-bin.index
read_only   = 1

rpl_semi_sync_slave_enabled=1

 

查看半同步相關(guān)信息

mysql>show  variables  like  ‘%

查看半同步狀態(tài):

mysql>show  status  like ‘%rpl_semi_sync%’;

有幾個狀態(tài)參數(shù)值得關(guān)注的:

rpl_semi_sync_master_status :顯示主服務(wù)是異步復(fù)制模式還是半同步復(fù)制模式  

rpl_semi_sync_master_clients :顯示有多少個從服務(wù)器配置為半同步復(fù)制模式  

rpl_semi_sync_master_yes_tx :顯示從服務(wù)器確認成功提交的數(shù)量  

rpl_semi_sync_master_no_tx :顯示從服務(wù)器確認不成功提交的數(shù)量  

rpl_semi_sync_master_tx_avg_wait_time :事務(wù)因開啟 semi_sync ,平均需要額外等待的時間  

rpl_semi_sync_master_net_avg_wait_time :事務(wù)進入等待隊列后,到網(wǎng)絡(luò)平均等待時間  

 

master主機:

第一條grant命令是創(chuàng)建一個用于主從復(fù)制的帳號,在master和candicate master的主機上創(chuàng)建即可。

第二條grant命令是創(chuàng)建MHA管理賬號,所有mysql服務(wù)器上都需要執(zhí)行。MHA會在配置文件里要求能遠程登錄到數(shù)據(jù)庫,所以要進行必要的賦權(quán)。

Candicate master主機:

查看從的狀態(tài),以下兩個值必須為yes,代表從服務(wù)器能正常連接主服務(wù)器

Slave_IO_Running:Yes

Slave_SQL_Running:Yes

查看從的狀態(tài),以下兩個值必須為yes,代表從服務(wù)器能正常連接主服務(wù)器

Slave_IO_Running:Yes

Slave_SQL_Running:Yes

查看master服務(wù)器的半同步狀態(tài):

mysql>show  status  like ‘%rpl_semi_sync%’;

三、配置mysql-mha

mha包括manager節(jié)點和data節(jié)點,data節(jié)點包括原有的MySQL復(fù)制結(jié)構(gòu)中的主機,至少3臺,即1主2從,當masterfailover后,還能保證主從結(jié)構(gòu);只需安裝node包。manager server:運行監(jiān)控腳本,負責(zé)monitoring 和 auto-failover;需要安裝node包和manager包。

1、 在所有主機上安裝mha所依賴的軟件包

2、 以下操作管理節(jié)點需要兩個都安裝, 在3臺數(shù)據(jù)庫節(jié)點只要安裝MHA的node節(jié)點:

在所有數(shù)據(jù)庫節(jié)點上安裝mha4mysql-node-0.56.tar.gz


其他兩個數(shù)據(jù)節(jié)點也安裝mha4mysql-node-0.56.tar.gz(過程略)

在管理節(jié)點需要兩個都安裝:mha4mysql-node-0.56.tar.gz和mha4mysql-manager-0.56.tar.gz

安裝mha4mysql-node-0.56.tar.gz

make && make install

根據(jù)提示輸入。

3、 配置mha

與絕大多數(shù)Linux應(yīng)用程序類似,MHA的正確使用依賴于合理的配置文件。MHA的配置文件與mysql的my.cnf文件配置相似,采取的是param=value的方式來配置,配置文件位于管理節(jié)點,通常包括每一個mysql server的主機名,mysql用戶名,密碼,工作目錄等等。

編輯/etc/masterha/app1.conf,內(nèi)容如下:

此處補加一下:

[server3]

hostname=192.168.1.104

port=3306

master_binlog_dir=/usr/local/mysql/data/

candidate_master=1

保存退出

配關(guān)配置項的解釋:

manager_workdir=/masterha/app1 //設(shè)置manager的工作目錄

manager_log=/masterha/app1/manager.log //設(shè)置manager的日志

user=manager//設(shè)置監(jiān)控用戶manager

password=123456  //監(jiān)控用戶manager的密碼

ssh_user=root  //ssh連接用戶

repl_user=mharep  //主從復(fù)制用戶

repl_password=123.abc //主從復(fù)制用戶密碼

ping_interval=1   //設(shè)置監(jiān)控主庫,發(fā)送ping包的時間間隔,默認是3秒,嘗試三次沒有回應(yīng)的時候自動進行railover

master_binlog_dir=/usr/local/mysql/data   //設(shè)置master 保存binlog的位置,以便MHA可以找到master的日志,我這里的也就是mysql的數(shù)據(jù)目錄

candidate_master=1//設(shè)置為候選master,如果設(shè)置該參數(shù)以后,發(fā)生主從切換以后將會將此從庫提升為主庫。

 

SSH 有效性驗證:

[root@centos1 ~]# masterha_check_ssh --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf

Wed Sep 28 21:34:50 2016 - [info] Reading default configuration from /etc/masterha/masterha_default.cnf..

Wed Sep 28 21:34:50 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Wed Sep 28 21:34:50 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Wed Sep 28 21:34:50 2016 - [info] Starting SSH connection tests..

Wed Sep 28 21:34:50 2016 - [debug]

Wed Sep 28 21:34:50 2016 - [debug]  Connecting via SSH from root@192.168.1.102(192.168.1.102:22) to root@192.168.1.103(192.168.1.103:22)..

Wed Sep 28 21:34:50 2016 - [debug]   ok.

Wed Sep 28 21:34:50 2016 - [debug]  Connecting via SSH from root@192.168.1.102(192.168.1.102:22) to root@192.168.1.104(192.168.1.104:22)..

Wed Sep 28 21:34:50 2016 - [debug]   ok.

Wed Sep 28 21:34:51 2016 - [debug]

Wed Sep 28 21:34:50 2016 - [debug]  Connecting via SSH from root@192.168.1.103(192.168.1.103:22) to root@192.168.1.102(192.168.1.102:22)..

Wed Sep 28 21:34:50 2016 - [debug]   ok.

Wed Sep 28 21:34:50 2016 - [debug]  Connecting via SSH from root@192.168.1.103(192.168.1.103:22) to root@192.168.1.104(192.168.1.104:22)..

Wed Sep 28 21:34:51 2016 - [debug]   ok.

Wed Sep 28 21:34:51 2016 - [debug]

Wed Sep 28 21:34:51 2016 - [debug]  Connecting via SSH from root@192.168.1.104(192.168.1.104:22) to root@192.168.1.102(192.168.1.102:22)..

Wed Sep 28 21:34:51 2016 - [debug]   ok.

Wed Sep 28 21:34:51 2016 - [debug]  Connecting via SSH from root@192.168.1.104(192.168.1.104:22) to root@192.168.1.103(192.168.1.103:22)..

Wed Sep 28 21:34:51 2016 - [debug]   ok.

Wed Sep 28 21:34:51 2016 - [info] All SSH connection tests passed successfully.

集群復(fù)制的有效性驗證:

mysql必須都啟動

[root@centos1 ~]# masterha_check_repl --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf

Wed Sep 28 21:38:21 2016 - [info] Reading default configuration from /etc/masterha/masterha_default.cnf..

Wed Sep 28 21:38:21 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Wed Sep 28 21:38:21 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Wed Sep 28 21:38:21 2016 - [info] MHA::MasterMonitor version 0.56.

Wed Sep 28 21:38:21 2016 - [info] GTID failover mode = 0

Wed Sep 28 21:38:21 2016 - [info] Dead Servers:

Wed Sep 28 21:38:21 2016 - [info] Alive Servers:

Wed Sep 28 21:38:21 2016 - [info]   192.168.1.102(192.168.1.102:3306)

Wed Sep 28 21:38:21 2016 - [info]   192.168.1.103(192.168.1.103:3306)

Wed Sep 28 21:38:21 2016 - [info]   192.168.1.104(192.168.1.104:3306)

Wed Sep 28 21:38:21 2016 - [info] Alive Slaves:

Wed Sep 28 21:38:21 2016 - [info]   192.168.1.103(192.168.1.103:3306)  Version=5.5.38-log (oldest major version between slaves) log-bin:enabled

Wed Sep 28 21:38:21 2016 - [info]     Replicating from 192.168.1.102(192.168.1.102:3306)

Wed Sep 28 21:38:21 2016 - [info]     Primary candidate for the new Master (candidate_master is set)

Wed Sep 28 21:38:21 2016 - [info]   192.168.1.104(192.168.1.104:3306)  Version=5.5.38-log (oldest major version between slaves) log-bin:enabled

Wed Sep 28 21:38:21 2016 - [info]     Replicating from 192.168.1.102(192.168.1.102:3306)

Wed Sep 28 21:38:21 2016 - [info]     Not candidate for the new Master (no_master is set)

Wed Sep 28 21:38:21 2016 - [info] Current Alive Master: 192.168.1.102(192.168.1.102:3306)

Wed Sep 28 21:38:21 2016 - [info] Checking slave configurations..

Wed Sep 28 21:38:21 2016 - [info]  read_only=1 is not set on slave 192.168.1.103(192.168.1.103:3306).

Wed Sep 28 21:38:21 2016 - [warning]  relay_log_purge=0 is not set on slave 192.168.1.104(192.168.1.104:3306).

Wed Sep 28 21:38:21 2016 - [info] Checking replication filtering settings..

Wed Sep 28 21:38:21 2016 - [info]  binlog_do_db= , binlog_ignore_db=

Wed Sep 28 21:38:21 2016 - [info]  Replication filtering check ok.

Wed Sep 28 21:38:21 2016 - [info] GTID (with auto-pos) is not supported

Wed Sep 28 21:38:21 2016 - [info] Starting SSH connection tests..

Wed Sep 28 21:38:23 2016 - [info] All SSH connection tests passed successfully.

Wed Sep 28 21:38:23 2016 - [info] Checking MHA Node version..

Wed Sep 28 21:38:23 2016 - [info]  Version check ok.

Wed Sep 28 21:38:23 2016 - [info] Checking SSH publickey authentication settings on the current master..

Wed Sep 28 21:38:23 2016 - [info] HealthCheck: SSH to 192.168.1.102 is reachable.

Wed Sep 28 21:38:23 2016 - [info] Master MHA Node version is 0.56.

Wed Sep 28 21:38:23 2016 - [info] Checking recovery script configurations on 192.168.1.102(192.168.1.102:3306)..

Wed Sep 28 21:38:23 2016 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/var/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000010

Wed Sep 28 21:38:23 2016 - [info]   Connecting to root@192.168.1.102(192.168.1.102:22)..

  Creating /var/tmp if not exists..ok.

  Checking output directory is accessible or not..

ok.

Binlog found at /usr/local/mysql/data, up to mysql-bin.000010

Wed Sep 28 21:38:23 2016 - [info] Binlog setting check done.

Wed Sep 28 21:38:23 2016 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..

Wed Sep 28 21:38:23 2016 - [info]   Executing command :apply_diff_relay_logs --command=test --slave_user='manager' --slave_host=192.168.1.103 --slave_ip=192.168.1.103 --slave_port=3306 --workdir=/var/tmp --target_version=5.5.38-log --manager_version=0.56 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx

Wed Sep 28 21:38:23 2016 - [info]   Connecting to root@192.168.1.103(192.168.1.103:22)..

  Checking slave recovery environment settings..

Opening /usr/local/mysql/data/relay-log.info ... ok.

    Relay log found at /usr/local/mysql/data, up to relay-bin.000007

    Temporary relay log file is /usr/local/mysql/data/relay-bin.000007

    Testing mysql connection and privileges..done.

    Testing mysqlbinlog output..done.

    Cleaning up test file(s)..done.

Wed Sep 28 21:38:24 2016 - [info]   Executing command :apply_diff_relay_logs --command=test --slave_user='manager' --slave_host=192.168.1.104 --slave_ip=192.168.1.104 --slave_port=3306 --workdir=/var/tmp --target_version=5.5.38-log --manager_version=0.56 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx

Wed Sep 28 21:38:24 2016 - [info]   Connecting to root@192.168.1.104(192.168.1.104:22)..

  Checking slave recovery environment settings..

Opening /usr/local/mysql/data/relay-log.info ... ok.

    Relay log found at /usr/local/mysql/data, up to relay-bin.000007

    Temporary relay log file is /usr/local/mysql/data/relay-bin.000007

    Testing mysql connection and privileges..done.

    Testing mysqlbinlog output..done.

    Cleaning up test file(s)..done.

Wed Sep 28 21:38:24 2016 - [info] Slaves settings check done.

Wed Sep 28 21:38:24 2016 - [info]

192.168.1.102(192.168.1.102:3306) (current master)

 +--192.168.1.103(192.168.1.103:3306)

 +--192.168.1.104(192.168.1.104:3306)

 

Wed Sep 28 21:38:24 2016 - [info] Checking replication health on 192.168.1.103..

Wed Sep 28 21:38:24 2016 - [info]  ok.

Wed Sep 28 21:38:24 2016 - [info] Checking replication health on 192.168.1.104..

Wed Sep 28 21:38:24 2016 - [info]  ok.

Wed Sep 28 21:38:24 2016 - [warning] master_ip_failover_script is not defined.

Wed Sep 28 21:38:24 2016 - [warning] shutdown_script is not defined.

Wed Sep 28 21:38:24 2016 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

驗證成功的話會自動識別出所有服務(wù)器和主從狀況

注:驗證成功的話會自動識別出所有服務(wù)器和主從狀況

在驗證時,若遇到這個錯誤:Can't exec "mysqlbinlog" ......

解決方法是在所有服務(wù)器上執(zhí)行:

ln -s /usr/local/mysql/bin/* /usr/local/bin/

啟動 manager:

注:在應(yīng)用Unix/Linux時,我們一般想讓某個程序在后臺運行,于是我們將常會用&在程序結(jié)尾來讓程序自動運行。比如我們要運行mysql在后臺: /usr/local/mysql/bin/mysqld_safe –user=mysql&??墒怯泻芏喑绦虿⒉幌雖ysqld一樣,這樣我們就需要nohup命令,

狀態(tài)檢查:

nohup masterha_check_status --conf=/etc/masterha/app1.cnf

故障轉(zhuǎn)移驗證:(自動failover)

master dead后,MHA當時已經(jīng)開啟,候選Master庫(Slave)會自動failover為Master.

驗證的方式是先停掉 master(centos2),因為之前的配置文件中,把Candicatemaster(centos3)為了候選人,那么就到 slave(centos4) 上查看 master 的 IP 是否變?yōu)榱? centos3 的 IP

1)停掉 master

在 master(192.168.1.102)上把mysql停掉

2)查看 MHA 日志

上面的配置文件中指定了日志位置為 /masterha/app1/manager.log

[root@centos1 ~]# cat /masterha/app1/manager.log

----- Failover Report -----

 

app1: MySQL Master failover 192.168.1.102(192.168.1.102:3306) to 192.168.1.103(192.168.1.103:3306) succeeded

 

Master 192.168.1.102(192.168.1.102:3306) is down!

 

Check MHA Manager logs at centos1.benet.com:/masterha/app1/manager.log for details.

 

Started automated(non-interactive) failover.

The latest slave 192.168.1.103(192.168.1.103:3306) has all relay logs for recovery.

Selected 192.168.1.103(192.168.1.103:3306) as a new master.

192.168.1.103(192.168.1.103:3306): OK: Applying all logs succeeded.

192.168.1.104(192.168.1.104:3306): This host has the latest relay log events.

Generating relay diff files from the latest slave succeeded.

192.168.1.104(192.168.1.104:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.1.103(192.168.1.103:3306)

192.168.1.103(192.168.1.103:3306): Resetting slave info succeeded.

Master failover to 192.168.1.103(192.168.1.103:3306) completed successfully.

從日志信息中可以看到 master failover 已經(jīng)成功了,并可以看出故障轉(zhuǎn)移的大體流程

3)檢查 slave2 的復(fù)制

登錄 slave(192.168.1.104)的Mysql,查看 slave 狀態(tài)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.103

Master_User: mharep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000009

Read_Master_Log_Pos: 107

Relay_Log_File: relay-bin.000002

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000009

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

可以看到 master 的 IP 現(xiàn)在為 192.168.1.103,已經(jīng)切換到和192.168.1.103同步了,本來是和192.168.1.102同步的,說明 MHA 已經(jīng)把Candicatemaster(centos3)提升為了新的 master,IO線程和SQL線程也正確運行,MHA 搭建成功

MHA Manager 端日常主要操作步驟

1)檢查是否有下列文件,有則刪除。

發(fā)生主從切換后,MHAmanager服務(wù)會自動停掉,且在manager_workdir(/masterha/app1)目錄下面生成文件app1.failover.complete,若要啟動MHA,必須先確保無此文件)

如果有這個提示,那么刪除此文件/masterha/app1/app1.failover.complete

[error][/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm, ln298] Last failover was done at 2015/01/09 10:00:47. Current time is too early to do failover again. If you want to do failover, manually remove /masterha/app1/app1.failover.complete and run this script again.

 

   # ll /masterha/app1/app1.failover.complete

   # ll /masterha/app1/app1.failover.error

 

2)檢查MHA當前置:

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

  

3)啟動MHA:

  #nohup masterha_manager  --conf=/etc/masterha/app1.cnf&>/tmp/mha_manager.log  &

當有slave 節(jié)點宕掉時,默認是啟動不了的,加上 --ignore_fail_on_start即使有節(jié)點宕掉也能啟動MHA,如下:

 #nohupmasterha_manager --conf=/etc/masterha/app1.cnf--ignore_fail_on_start&>/tmp/mha_manager.log  &

 

4)停止MHA:  masterha_stop  --conf=/etc/masterha/app1.cnf

 

5)檢查狀態(tài):

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

 

6)檢查日志:

#tail -f /masterha/app1/manager.log

  

7)主從切換后續(xù)工作

重構(gòu):

重構(gòu)就是你的主掛了,切換到Candicate master上,Candicate master變成了主,因此重構(gòu)的一種方案原主庫修復(fù)成一個新的slave 

主庫切換后,把原主庫修復(fù)成新從庫,然后重新執(zhí)行以上5步。原主庫數(shù)據(jù)文件完整的情況下,可通過以下方式找出最后執(zhí)行的CHANGE MASTER命令:

[root@centos1 ~]# grep "CHANGE MASTER TO MASTER"  /masterha/app1/manager.log | tail -1

Wed Sep 28 22:36:41 2016 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.103', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=107, MASTER_USER='mharep', MASTER_PASSWORD='xxx';

 

[root@centos2 ~]# mysql -uroot -ppwd123

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.103', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=107, MASTER_USER='mharep', MASTER_PASSWORD='123.abc';

Query OK, 0 rows affected (0.06 sec)

 

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.103

Master_User: mharep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000011

Read_Master_Log_Pos: 107

Relay_Log_File: relay-bin.000005

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000011

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

啟動manager


注意:如果正常,會顯示"PING_OK",否則會顯示"NOT_RUNNING",這代表MHA監(jiān)控沒有開啟。

 

定期刪除中繼日志

在配置主從復(fù)制中,slave上設(shè)置了參數(shù)relay_log_purge=0,所以slave節(jié)點需要定期刪除中繼日志,建議每個slave節(jié)點刪除中繼日志的時間錯開。

corntab -e

0 5 * * *  /usr/local/bin/purge_relay_logs  - -user=root  --password=pwd123 --port=3306   --disable_relay_log_purge>> /var/log/purge_relay.log  2>&1

 

四、配置VIP:

vip配置可以采用兩種方式,一種通過keepalived的方式管理虛擬ip的浮動;另外一種通過腳本方式啟動虛擬ip的方式(即不需要keepalived或者heartbeat類似的軟件)。

1、keepalived方式管理虛擬ip,keepalived配置方法如下:

下載軟件進行并進行安裝(兩臺master,準確的說一臺是master,另外一臺是備選master,在沒有切換以前是slave)

在centos2和centos3上安裝軟件包keepalived

安裝keepalived軟件包與服務(wù)控制

在編譯安裝Keepalived之前,必須先安裝內(nèi)核開發(fā)包kernel-devel以及openssl-devel、popt-devel等支持庫。

若沒有安裝則通過rpm或yum工具進行安裝

編譯安裝Keepalived

使用指定的linux內(nèi)核位置對keepalived進行配置,并將安裝路徑指定為根目錄,這樣就無需額外創(chuàng)建鏈接文件了,配置完成后,依次執(zhí)行make、make install進行安裝。

使用keepalived服務(wù)

執(zhí)行make install操作之后,會自動生成/etc/init.d/keepalived腳本文件,但還需要手動添加為系統(tǒng)服務(wù),這樣就可以使用service、chkconfig工具來對keepalived服務(wù)程序進行管理了。

Centos3主機也完成keepalived安裝,與master1一樣,安裝過程略

注:若開啟了防火墻,需要關(guān)閉防火墻或創(chuàng)建規(guī)則。

修改Keepalived的配置文件(在master上配置)

在候選master上配置

啟動keepalived服務(wù),在master上啟動并查看日志

#/etc/init.d/keepalivedstart ; tail -f /var/log/messages

發(fā)現(xiàn)已經(jīng)將虛擬ip 192.168.1.100綁定了網(wǎng)卡eth0上


# tail -f /var/log/messages

查看eth0網(wǎng)卡是否綁定了VIP

 

在另外一臺服務(wù)器,候選master上啟動keepalived服務(wù),并觀察

#/etc/init.d/keepalivedstart ; tail -f /var/log/messages

查看eth0網(wǎng)卡綁定情況

從上面的信息可以看到keepalived已經(jīng)配置成功

注意:

上面兩臺服務(wù)器的keepalived都設(shè)置為了BACKUP模式,在keepalived中2種模式,分別是master->backup模式和backup->backup模式。這兩種模式有很大區(qū)別。在master->backup模式下,一旦主庫宕機,虛擬ip會自動漂移到從庫,當主庫修復(fù)后,keepalived啟動后,還會把虛擬ip搶占過來,即使設(shè)置了非搶占模式(nopreempt)搶占ip的動作也會發(fā)生。在backup->backup模式下,當主庫宕機后虛擬ip會自動漂移到從庫上,當原主庫恢復(fù)和keepalived服務(wù)啟動后,并不會搶占新主的虛擬ip,即使是優(yōu)先級高于從庫的優(yōu)先級別,也不會發(fā)生搶占。為了減少ip漂移次數(shù),通常是把修復(fù)好的主庫當做新的備庫。

 

2、MHA引入keepalived(MySQL服務(wù)進程掛掉時通過MHA 停止keepalived):

要想把keepalived服務(wù)引入MHA,我們只需要修改切換時觸發(fā)的腳本文件master_ip_failover即可,在該腳本中添加在master發(fā)生宕機時對keepalived的處理。

編輯腳本/scripts/master_ip_failover,修改后如下。

[root@centos1 ~]# cat /scripts/master_ip_failover

#!/usr/bin/envperl

 

use strict;

use warnings FATAL => 'all';

 

useGetopt::Long;

 

my (

    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,

    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port

);

 

my $vip = '192.168.1.100';

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

substart_vip() {

    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;

}

# A simple system call that disable the VIP on the old_master

substop_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";

}

現(xiàn)在已經(jīng)修改這個腳本了,接下來我們在/etc/masterha/app1.cnf 中調(diào)用故障切換腳本

停止MHA: 

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

在配置文件/etc/masterha/app1.cnf 中啟用下面的參數(shù)(在[server  default下面添加])

master_ip_failover_script=/scripts/master_ip_failover

啟動MHA:

#nohupmasterha_manager --conf=/etc/masterha/app1.cnf&>/tmp/mha_manager.log  &

檢查狀態(tài):

[root@centos1 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf

app1 (pid:12047) is running(0:PING_OK), master:192.168.1.103

再檢查集群狀態(tài),看是否會報錯。

[root@centos1 ~]# masterha_check_repl  --conf=/etc/masterha/app1.cnf

……

Thu Sep 29 23:29:30 2016 - [info] Slaves settings check done.

Thu Sep 29 23:29:30 2016 - [info]

192.168.1.103(192.168.1.103:3306) (current master)

 +--192.168.1.102(192.168.1.102:3306)

 +--192.168.1.104(192.168.1.104:3306)

 

Thu Sep 29 23:29:30 2016 - [info] Checking replication health on 192.168.1.102..

Thu Sep 29 23:29:30 2016 - [info]  ok.

Thu Sep 29 23:29:30 2016 - [info] Checking replication health on 192.168.1.104..

Thu Sep 29 23:29:30 2016 - [info]  ok.

Thu Sep 29 23:29:30 2016 - [info] Checking master_ip_failover_script status:

Thu Sep 29 23:29:30 2016 - [info]   /scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.103 --orig_master_ip=192.168.1.103 --orig_master_port=3306

IN SCRIPT TEST====/etc/init.d/keepalived stop==/etc/init.d/keepalived start===

Checking the Status of the script.. OK

Thu Sep 29 23:29:30 2016 - [info]  OK.

Thu Sep 29 23:29:30 2016 - [warning] shutdown_script is not defined.

Thu Sep 29 23:29:30 2016 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

 

可以看見已經(jīng)沒有報錯了。
 /scripts/master_ip_failover添加或者修改的內(nèi)容意思是當主庫數(shù)據(jù)庫發(fā)生故障時,會觸發(fā)MHA切換,MHA Manager會停掉主庫上的keepalived服務(wù),觸發(fā)虛擬ip漂移到備選從庫,從而完成切換。

當然可以在keepalived里面引入腳本,這個腳本監(jiān)控mysql是否正常運行,如果不正常,則調(diào)用該腳本殺掉keepalived進程(參考MySQL 高可用性keepalived+mysql雙主)。

測試:

在master上停止mysqld服務(wù)

到slave(192.168.1.104)查看slave的狀態(tài):

從上圖可以看出slave指向了新的master服務(wù)器192.168.1.102(在故障切換前指向的是192.168.1.103)

查看VIP綁定:

在192.168.1.103上查看vip綁定

在192.168.1.102上查看vip綁定

從上面的顯示結(jié)果可以看出vip地址漂移到了192.168.1.102

主從切換后續(xù)工作

重構(gòu):

重構(gòu)就是你的主掛了,切換到Candicate master上,Candicate master變成了主,因此重構(gòu)的一種方案原主庫修復(fù)成一個新的slave

主庫切換后,把原主庫修復(fù)成新從庫,原主庫數(shù)據(jù)文件完整的情況下,可通過以下方式找出最后執(zhí)行的CHANGE MASTER命令:

[root@centos1 ~]# grep "CHANGE MASTER TO MASTER"  /masterha/app1/manager.log | tail -1

Thu Sep 29 23:38:16 2016 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.102', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=107, MASTER_USER='mharep', MASTER_PASSWORD='xxx';

將192.168.1.103(原主庫)修復(fù)成從庫

[root@centos3 ~]# servicemysqld start

Starting MySQL..[  OK  ]

[root@centos3 ~]# mysql -uroot -ppwd123

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.38-log Source distribution

 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help.Type '\c' to clear the current input statement.

 

mysql>CHANGE MASTER TO MASTER_HOST='192.168.1.102', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=107, MASTER_USER='mharep', MASTER_PASSWORD='123.abc';

Query OK, 0 rows affected (0.06 sec)

 

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.102

Master_User: mharep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000013

Read_Master_Log_Pos: 107

Relay_Log_File: relay-bin.000002

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000013

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

 

[root@centos3 ~]# /etc/init.d/keepalived start

Starting keepalived:                                       [  OK  ]

[root@centos3 ~]# /etc/init.d/keepalived status

keepalived (pid  6436) is running...

 

啟動mha manager:

[root@centos1 ~]# rm -fr /masterha/app1/app1.failover.complete

[root@centos1 ~]# nohupmasterha_manager --conf=/etc/masterha/app1.cnf  --ignore_fail_on_start&>/tmp/mha_manager.log  &

[1] 13010

[root@centos1 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf

app1 (pid:13010) is running(0:PING_OK), master:192.168.1.102

 

[root@centos1 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf

Fri Sep 30 00:02:39 2016 - [info] Slaves settings check done.

Fri Sep 30 00:02:39 2016 - [info]

192.168.1.102(192.168.1.102:3306) (current master)

 +--192.168.1.103(192.168.1.103:3306)

 +--192.168.1.104(192.168.1.104:3306)

 

Fri Sep 30 00:02:39 2016 - [info] Checking replication health on 192.168.1.103..

Fri Sep 30 00:02:39 2016 - [info]  ok.

Fri Sep 30 00:02:39 2016 - [info] Checking replication health on 192.168.1.104..

Fri Sep 30 00:02:39 2016 - [info]  ok.

Fri Sep 30 00:02:39 2016 - [info] Checking master_ip_failover_script status:

Fri Sep 30 00:02:39 2016 - [info]   /scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.102 --orig_master_ip=192.168.1.102 --orig_master_port=3306

 

2、通過腳本實現(xiàn)VIP切換

通過腳本的方式管理VIP。這里是修改/scripts/master_ip_failover,也可以使用其他的語言完成,比如php語言。使用php腳本編寫的failover這里就不介紹了。修改完成后內(nèi)容如下,而且如果使用腳本管理vip的話,需要手動在master服務(wù)器上綁定一個vip

[root@centos2 ~]# /sbin/ifconfig eth0:0 192.168.1.100/24

在mha-manager上修改/scripts/master_ip_failover,內(nèi)容如下

[root@centos1 ~]# cat /scripts/master_ip_failover

#!/usr/bin/envperl

 

use strict;

use warnings FATAL => 'all';

 

useGetopt::Long;

 

my (

    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,

    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port

);

 

my $vip = '192.168.1.100/24';

my $key = '0';

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;

    }

}

 

substart_vip() {

    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;

}

substop_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";

}

 

4)停止MHA:  masterha_stop  --conf=/etc/masterha/app1.cnf

 

[root@centos1 ~]# grep "master_ip_failover_script" /etc/masterha/app1.cnf

master_ip_failover_script=/scripts/master_ip_failover

啟動MHA:

  #nohupmasterha_manager --conf=/etc/masterha/app1.cnf&>/tmp/mha_manager.log  &

 

5)檢查狀態(tài):f

[root@centos1 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf

app1 (pid:2818) is running(0:PING_OK), master:192.168.1.102

 

再檢查集群狀態(tài),看是否會報錯。

[root@centos1 ~]# masterha_check_repl--conf=/etc/masterha/app1.cnf

Fri Sep 30 23:05:10 2016 - [info] Slaves settings check done.

Fri Sep 30 23:05:10 2016 - [info]

192.168.1.102(192.168.1.102:3306) (current master)

 +--192.168.1.103(192.168.1.103:3306)

 +--192.168.1.104(192.168.1.104:3306)

 

Fri Sep 30 23:05:10 2016 - [info] Checking replication health on 192.168.1.103..

Fri Sep 30 23:05:10 2016 - [info]  ok.

Fri Sep 30 23:05:10 2016 - [info] Checking replication health on 192.168.1.104..

Fri Sep 30 23:05:10 2016 - [info]  ok.

Fri Sep 30 23:05:10 2016 - [info] Checking master_ip_failover_script status:

Fri Sep 30 23:05:10 2016 - [info]   /scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.102 --orig_master_ip=192.168.1.102 --orig_master_port=3306

 

IN SCRIPT TEST====/sbin/ifconfig eth0:0 down==/sbin/ifconfig eth0:0 192.168.1.100/24===

 

Checking the Status of the script.. OK

Fri Sep 30 23:05:10 2016 - [info]  OK.

Fri Sep 30 23:05:10 2016 - [warning] shutdown_script is not defined.

Fri Sep 30 23:05:10 2016 - [info] Got exit code 0 (Not master dead).

 

MySQL Replication Health is OK.

測試:

在master上停掉mysql服務(wù)

[root@centos2 ~]# /etc/init.d/mysqld stop

Shutting down MySQL...                                     [  OK  ]

到slave(192.168.1.104)查看slave的狀態(tài):

從上圖可以看出slave指向了新的master服務(wù)器(192.168.1.103)

查看VIP

從上圖可以看到centos2(原來的master)釋放了VIP,centos03(新的master)接管了VIP地址

主從切換后續(xù)工作

主庫切換后,把原主庫修復(fù)成新從庫,相關(guān)操作請參考前面相關(guān)操作。

為了防止腦裂發(fā)生,推薦生產(chǎn)環(huán)境采用腳本的方式來管理虛擬ip,而不是使用keepalived來完成。到此為止,基本MHA集群已經(jīng)配置完畢。

 

總結(jié):

MHA軟件由兩部分組成,Manager工具包和Node工具包,具體的說明如下。

Manager工具包主要包括以下幾個工具:

masterha_check_ssh檢查MHA的SSH配置狀況

masterha_check_repl檢查MySQL復(fù)制狀況

masterha_manger啟動MHA

masterha_check_status檢測當前MHA運行狀態(tài)

masterha_master_monitor檢測master是否宕機

masterha_master_switch控制故障轉(zhuǎn)移(自動或者手動)

masterha_conf_host添加或刪除配置的server信息

Node工具包(這些工具通常由MHA Manager的腳本觸發(fā),無需人為操作)主要包括以下幾個工具:

save_binary_logs保存和復(fù)制master的二進制日志

apply_diff_relay_logs識別差異的中繼日志事件并將其差異的事件應(yīng)用于其他的slave

filter_mysqlbinlog去除不必要的ROLLBACK事件(MHA已不再使用這個工具)

purge_relay_logs清除中繼日志(不會阻塞SQL線程)

 

mysql必備技能掌握:

1、MySQL架構(gòu):對mysql的架構(gòu),整體有個印象,才能不斷的加深對mysql的理解和后繼的學(xué)習(xí)。

2、用各種姿勢備份MySQL數(shù)據(jù)庫

數(shù)據(jù)備份是DBA或運維工程師日常工作之一,如果讓你來備份,你會用什么方式備份,在時間時間備份,使用什么策略備份

3、mysql主從復(fù)制及讀寫分離

mysql的主從復(fù)制及讀寫分離是DBA必備技能之一

4、MySQL/MariaDB數(shù)據(jù)庫基于SSL實現(xiàn)主從復(fù)制

加強主從復(fù)制的安全性

5、MySQL高可用

數(shù)據(jù)的高可用如何保證

6、數(shù)據(jù)庫Sharding的基本思想和切分策略

隨著數(shù)據(jù)量的不斷攀升,從性能和可維護的角度,需要進行一些Sharding,也就是數(shù)據(jù)庫的切分,有垂直切分和水平切分

7、MySQL/MariaDB性能調(diào)整和優(yōu)化技巧

掌握優(yōu)化思路和技巧,對數(shù)據(jù)庫的不斷優(yōu)化是一項長期工程

 

謝謝觀看,真心的希望能幫到您!

本文出自 “一盞燭光” 博客,謝絕轉(zhuǎn)載!

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號