創(chuàng)新互聯(lián)公司公司2013年成立,先為邢臺(tái)等服務(wù)建站,邢臺(tái)等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為邢臺(tái)企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
本文主要給大家介紹MySQL之MHA分享,其所涉及的東西,從理論知識(shí)來(lái)獲悉,有很多書(shū)籍、文獻(xiàn)可供大家參考,從現(xiàn)實(shí)意義角度出發(fā),創(chuàng)新互聯(lián)累計(jì)多年的實(shí)踐經(jīng)驗(yàn)可分享給大家。
簡(jiǎn)介:
MHA(Master High Availability)目前在MySQL高可用方面是一個(gè)相對(duì)成熟的解決方案,它由日本DeNA公司youshimaton(現(xiàn)就職于Facebook公司)開(kāi)發(fā),是一套優(yōu)秀的作為MySQL高可用性環(huán)境下故障切換和主從提升的高可用軟件。在MySQL故障切換過(guò)程中,MHA能做到在0~30秒之內(nèi)自動(dòng)完成數(shù)據(jù)庫(kù)的故障切換操作,并且在進(jìn)行故障切換的過(guò)程中,MHA能在最大程度上保證數(shù)據(jù)的一致性,以達(dá)到真正意義上的高可用。
該軟件由兩部分組成:MHA Manager(管理節(jié)點(diǎn))和MHA Node(數(shù)據(jù)節(jié)點(diǎn))。MHA Manager可以單獨(dú)部署在一臺(tái)獨(dú)立的機(jī)器上管理多個(gè)master-slave集群,也可以部署在一臺(tái)slave節(jié)點(diǎn)上。MHA Node運(yùn)行在每臺(tái)MySQL云服務(wù)器上,MHA Manager會(huì)定時(shí)探測(cè)集群中的master節(jié)點(diǎn),當(dāng)master出現(xiàn)故障時(shí),它可以自動(dòng)將最新數(shù)據(jù)的slave提升為新的master,然后將所有其他的slave重新指向新的master。整個(gè)故障轉(zhuǎn)移過(guò)程對(duì)應(yīng)用程序完全透明。
在MHA自動(dòng)故障切換過(guò)程中,MHA試圖從宕機(jī)的主服務(wù)器上保存二進(jìn)制日志,最大程度的保證數(shù)據(jù)的不丟失,但這并不總是可行的。例如,如果主服務(wù)器硬件故障或無(wú)法通過(guò)ssh訪問(wèn),MHA沒(méi)法保存二進(jìn)制日志,只進(jìn)行故障轉(zhuǎn)移而丟失了最新的數(shù)據(jù)。使用MySQL 5.5的半同步復(fù)制,可以大大降低數(shù)據(jù)丟失的風(fēng)險(xiǎn)。MHA可以與半同步復(fù)制結(jié)合起來(lái)。如果只有一個(gè)slave已經(jīng)收到了最新的二進(jìn)制日志,MHA可以將最新的二進(jìn)制日志應(yīng)用于其他所有的slave服務(wù)器上,因此可以保證所有節(jié)點(diǎn)的數(shù)據(jù)一致性。
目前MHA主要支持一主多從的架構(gòu),要搭建MHA,要求一個(gè)復(fù)制集群中必須最少有三臺(tái)數(shù)據(jù)庫(kù)服務(wù)器,一主二從,即一臺(tái)充當(dāng)master,一臺(tái)充當(dāng)備用master,另外一臺(tái)充當(dāng)從庫(kù),因?yàn)橹辽傩枰_(tái)服務(wù)器,出于機(jī)器成本的考慮,淘寶也在該基礎(chǔ)上進(jìn)行了改造,目前淘寶TMHA已經(jīng)支持一主一從。另外對(duì)于想快速搭建的可以參考:MHA快速搭建
我們自己使用其實(shí)也可以使用1主1從,但是master主機(jī)宕機(jī)后無(wú)法切換,以及無(wú)法補(bǔ)全binlog。master的mysqld進(jìn)程crash后,還是可以切換成功,以及補(bǔ)全binlog的。
官方介紹:https://code.google.com/p/mysql-master-ha/
圖01展示了如何通過(guò)MHA Manager管理多組主從復(fù)制??梢詫HA工作原理總結(jié)為如下:
( 圖01 )
(1)從宕機(jī)崩潰的master保存二進(jìn)制日志事件(binlog events);
(2)識(shí)別含有最新更新的slave;
(3)應(yīng)用差異的中繼日志(relay log)到其他的slave;
(4)應(yīng)用從master保存的二進(jìn)制日志事件(binlog events);
(5)提升一個(gè)slave為新的master;
(6)使其他的slave連接新的master進(jìn)行復(fù)制;
MHA軟件由兩部分組成,Manager工具包和Node工具包,具體的說(shuō)明如下。
Manager工具包主要包括以下幾個(gè)工具:
masterha_check_ssh 檢查MHA的SSH配置狀況 masterha_check_repl 檢查MySQL復(fù)制狀況 masterha_manger 啟動(dòng)MHA masterha_check_status 檢測(cè)當(dāng)前MHA運(yùn)行狀態(tài) masterha_master_monitor 檢測(cè)master是否宕機(jī) masterha_master_switch 控制故障轉(zhuǎn)移(自動(dòng)或者手動(dòng)) masterha_conf_host 添加或刪除配置的server信息
Node工具包(這些工具通常由MHA Manager的腳本觸發(fā),無(wú)需人為操作)主要包括以下幾個(gè)工具:
save_binary_logs 保存和復(fù)制master的二進(jìn)制日志 apply_diff_relay_logs 識(shí)別差異的中繼日志事件并將其差異的事件應(yīng)用于其他的slave filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用這個(gè)工具) purge_relay_logs 清除中繼日志(不會(huì)阻塞SQL線程)
注意:
為了盡可能的減少主庫(kù)硬件損壞宕機(jī)造成的數(shù)據(jù)丟失,因此在配置MHA的同時(shí)建議配置成MySQL 5.5的半同步復(fù)制。關(guān)于半同步復(fù)制原理各位自己進(jìn)行查閱。(不是必須)
1.部署MHA
接下來(lái)部署MHA,具體的搭建環(huán)境如下(所有操作系統(tǒng)均為centos 6.2 64bit,不是必須,server03和server04是server02的從,復(fù)制環(huán)境搭建后面會(huì)簡(jiǎn)單演示,但是相關(guān)的安全復(fù)制不會(huì)詳細(xì)說(shuō)明,需要的童鞋請(qǐng)參考前面的文章,MySQL Replication需要注意的問(wèn)題):
角色 ip地址 主機(jī)名 server_id 類(lèi)型 Monitor host 192.168.0.20 server01 - 監(jiān)控復(fù)制組 Master 192.168.0.50 server02 1 寫(xiě)入 Candicate master 192.168.0.60 server03 2 讀 Slave 192.168.0.70 server04 3 讀
其中master對(duì)外提供寫(xiě)服務(wù),備選master(實(shí)際的slave,主機(jī)名server03)提供讀服務(wù),slave也提供相關(guān)的讀服務(wù),一旦master宕機(jī),將會(huì)把備選master提升為新的master,slave指向新的master
(1)在所有節(jié)點(diǎn)安裝MHA node所需的perl模塊(DBD:mysql),安裝腳本如下:
[root@192.168.0.50 ~]# cat install.sh #!/bin/bashwget http://xrl.us/cpanm --no-check-certificatemv cpanm /usr/binchmod 755 /usr/bin/cpanmcat > /root/list << EOFinstall DBD::mysql EOFfor package in `cat /root/list`do cpanm $packagedone[root@192.168.0.50 ~]#
如果有安裝epel源,也可以使用yum安裝
yum install perl-DBD-MySQL -y
(2)在所有的節(jié)點(diǎn)安裝mha node:
wget http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.53.tar.gztar xf mha4mysql-node-0.53.tar.gz cd mha4mysql-node-0.53perl Makefile.PLmake && make install
安裝完成后會(huì)在/usr/local/bin目錄下生成以下腳本文件:
[root@192.168.0.50 bin]# pwd/usr/local/bin [root@192.168.0.50 bin]# ll total 40-r-xr-xr-x 1 root root 15498 Apr 20 10:05 apply_diff_relay_logs-r-xr-xr-x 1 root root 4807 Apr 20 10:05 filter_mysqlbinlog-r-xr-xr-x 1 root root 7401 Apr 20 10:05 purge_relay_logs-r-xr-xr-x 1 root root 7263 Apr 20 10:05 save_binary_logs [root@192.168.0.50 bin]#
關(guān)于上面腳本的功能,上面已經(jīng)介紹過(guò)了,這里不再重復(fù)了。
2.安裝MHA Manager
MHA Manager中主要包括了幾個(gè)管理員的命令行工具,例如master_manger,master_master_switch等。MHA Manger也依賴(lài)于perl模塊,具體如下:
(1)安裝MHA Node軟件包之前需要安裝依賴(lài)。我這里使用yum完成,沒(méi)有epel源的可以使用上面提到的腳本(epel源安裝也簡(jiǎn)單)。注意:在MHA Manager的主機(jī)也是需要安裝MHA Node。
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install perl-DBD-MySQL -y
安裝MHA Node軟件包,和上面的方法一樣,如下:
wget http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.53.tar.gztar xf mha4mysql-node-0.53.tar.gz cd mha4mysql-node-0.53perl Makefile.PLmake && make install
(2)安裝MHA Manager。首先安裝MHA Manger依賴(lài)的perl模塊(我這里使用yum安裝):
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
安裝MHA Manager軟件包:
wget http://mysql-master-ha.googlecode.com/files/mha4mysql-manager-0.53.tar.gztar xf mha4mysql-manager-0.53.tar.gz cd mha4mysql-manager-0.53perl Makefile.PLmake && make install
安裝完成后會(huì)在/usr/local/bin目錄下面生成以下腳本文件,前面已經(jīng)說(shuō)過(guò)這些腳本的作用,這里不再重復(fù)
[root@192.168.0.20 bin]# pwd/usr/local/bin [root@192.168.0.20 bin]# ll total 76-r-xr-xr-x 1 root root 15498 Apr 20 10:58 apply_diff_relay_logs-r-xr-xr-x 1 root root 4807 Apr 20 10:58 filter_mysqlbinlog-r-xr-xr-x 1 root root 1995 Apr 20 11:33 masterha_check_repl-r-xr-xr-x 1 root root 1779 Apr 20 11:33 masterha_check_ssh-r-xr-xr-x 1 root root 1865 Apr 20 11:33 masterha_check_status-r-xr-xr-x 1 root root 3201 Apr 20 11:33 masterha_conf_host-r-xr-xr-x 1 root root 2517 Apr 20 11:33 masterha_manager-r-xr-xr-x 1 root root 2165 Apr 20 11:33 masterha_master_monitor-r-xr-xr-x 1 root root 2373 Apr 20 11:33 masterha_master_switch-r-xr-xr-x 1 root root 3749 Apr 20 11:33 masterha_secondary_check-r-xr-xr-x 1 root root 1739 Apr 20 11:33 masterha_stop-r-xr-xr-x 1 root root 7401 Apr 20 10:58 purge_relay_logs-r-xr-xr-x 1 root root 7263 Apr 20 10:58 save_binary_logs [root@192.168.0.20 bin]#
復(fù)制相關(guān)腳本到/usr/local/bin目錄(軟件包解壓縮后就有了,不是必須,因?yàn)檫@些腳本不完整,需要自己修改,這是軟件開(kāi)發(fā)著留給我們自己發(fā)揮的,如果開(kāi)啟下面的任何一個(gè)腳本對(duì)應(yīng)的參數(shù),而對(duì)應(yīng)這里的腳本又沒(méi)有修改,則會(huì)拋錯(cuò),自己被坑的很慘)
[root@192.168.0.20 scripts]# pwd/root/mha4mysql-manager-0.53/samples/scripts [root@192.168.0.20 scripts]# ll total 32-rwxr-xr-x 1 root root 3443 Jan 8 2012 master_ip_failover #自動(dòng)切換時(shí)vip管理的腳本,不是必須,如果我們使用keepalived的,我們可以自己編寫(xiě)腳本完成對(duì)vip的管理,比如監(jiān)控mysql,如果mysql異常,我們停止keepalived就行,這樣vip就會(huì)自動(dòng)漂移-rwxr-xr-x 1 root root 9186 Jan 8 2012 master_ip_online_change #在線切換時(shí)vip的管理,不是必須,同樣可以可以自行編寫(xiě)簡(jiǎn)單的shell完成-rwxr-xr-x 1 root root 11867 Jan 8 2012 power_manager #故障發(fā)生后關(guān)閉主機(jī)的腳本,不是必須-rwxr-xr-x 1 root root 1360 Jan 8 2012 send_report #因故障切換后發(fā)送報(bào)警的腳本,不是必須,可自行編寫(xiě)簡(jiǎn)單的shell完成。 [root@192.168.0.20 scripts]# cp * /usr/local/bin/[root@192.168.0.20 scripts]#
3.配置SSH登錄無(wú)密碼驗(yàn)證(使用key登錄,工作中常用)我的測(cè)試環(huán)境已經(jīng)是使用key登錄,服務(wù)器之間無(wú)需密碼驗(yàn)證的。關(guān)于配置使用key登錄,我想我不再重復(fù)。但是有一點(diǎn)需要注意:不能禁止 password 登陸,否則會(huì)出現(xiàn)錯(cuò)誤
4.搭建主從復(fù)制環(huán)境
注意:binlog-do-db 和 replicate-ignore-db 設(shè)置必須相同。 MHA 在啟動(dòng)時(shí)候會(huì)檢測(cè)過(guò)濾規(guī)則,如果過(guò)濾規(guī)則不同,MHA 不啟動(dòng)監(jiān)控和故障轉(zhuǎn)移。
(1)在server02上執(zhí)行備份(192.168.0.50)
[root@192.168.0.50 ~]# mysqldump --master-data=2 --single-transaction -R --triggers -A > all.sql
其中--master-data=2代表備份時(shí)刻記錄master的Binlog位置和Position,--single-transaction意思是獲取一致性快照,-R意思是備份存儲(chǔ)過(guò)程和函數(shù),--triggres的意思是備份觸發(fā)器,-A代表備份所有的庫(kù)。更多信息請(qǐng)自行mysqldump --help查看。
(2)在server02上創(chuàng)建復(fù)制用戶(hù):
mysql> grant replication slave on *.* to 'repl'@'192.168.0.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
(3)查看主庫(kù)備份時(shí)的binlog名稱(chēng)和位置,MASTER_LOG_FILE和MASTER_LOG_POS:
[root@192.168.0.50 ~]# head -n 30 all.sql | grep 'CHANGE MASTER TO'-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=112; [root@192.168.0.50 ~]#
(4)把備份復(fù)制到server03和server04,也就是192.168.0.60和192.168.0.70
scp all.sql server03:/data/scp all.sql server04:/data/
(5)導(dǎo)入備份到server03,執(zhí)行復(fù)制相關(guān)命令
mysql < /data/all.sql
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.50',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=112; Query OK, 0 rows affected (0.02 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql>
查看復(fù)制狀態(tài)(可以看見(jiàn)復(fù)制成功):
[root@192.168.0.60 ~]# mysql -e 'show slave status\G' | egrep 'Slave_IO|Slave_SQL' Slave_IO_State: Waiting for master to send event Slave_IO_Running: Yes Slave_SQL_Running: Yes [root@192.168.0.60 ~]#
(6)在server04(192.168.0.70)上搭建復(fù)制環(huán)境,操作和上面一樣。
mysql < /data/all.sql
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.50',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=112; Query OK, 0 rows affected (0.07 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql>
查看復(fù)制狀態(tài):
[root@192.168.0.70 ~]# mysql -e 'show slave status\G' | egrep 'Slave_IO|Slave_SQL' Slave_IO_State: Waiting for master to send event Slave_IO_Running: Yes Slave_SQL_Running: Yes [root@192.168.0.70 ~]#
(7)兩臺(tái)slave服務(wù)器設(shè)置read_only(從庫(kù)對(duì)外提供讀服務(wù),只所以沒(méi)有寫(xiě)進(jìn)配置文件,是因?yàn)?strong>隨時(shí)slave會(huì)提升為master)
[root@192.168.0.60 ~]# mysql -e 'set global read_only=1'[root@192.168.0.60 ~]#
[root@192.168.0.70 ~]# mysql -e 'set global read_only=1'[root@192.168.0.70 ~]#
(8)創(chuàng)建監(jiān)控用戶(hù)(在master上執(zhí)行,也就是192.168.0.50):
mysql> grant all privileges on *.* to 'root'@'192.168.0.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql>
到這里整個(gè)集群環(huán)境已經(jīng)搭建完畢,剩下的就是配置MHA軟件了。
5.配置MHA
(1)創(chuàng)建MHA的工作目錄,并且創(chuàng)建相關(guān)配置文件(在軟件包解壓后的目錄里面有樣例配置文件)。
[root@192.168.0.20 ~]# mkdir -p /etc/masterha [root@192.168.0.20 ~]# cp mha4mysql-manager-0.53/samples/conf/app1.cnf /etc/masterha/[root@192.168.0.20 ~]#
修改app1.cnf配置文件,修改后的文件內(nèi)容如下(注意,配置文件中的注釋需要去掉,我這里是為了解釋清楚):
[root@. ~]# /etc/masterha/=/var/log/masterha/app1.log manager_log=/var/log/masterha/app1/manager.log master_binlog_dir=/data/mysql master_ip_failover_script= /usr/local/bin/master_ip_failover master_ip_online_change_script= /usr/local/bin/master_ip_online_change password= user== remote_workdir=/tmp repl_password= repl_user=repl report_script=/usr/local/send_report secondary_check_script= /usr/local/bin/masterha_secondary_check -s server03 -s server02 shutdown_script= ssh_user=root =.==.== check_repl_delay= =.=. ~]#
(2)設(shè)置relay log的清除方式(在每個(gè)slave節(jié)點(diǎn)上):
[root@192.168.0.60 ~]# mysql -e 'set global relay_log_purge=0'[root@192.168.0.70 ~]# mysql -e 'set global relay_log_purge=0'
注意:
MHA在發(fā)生切換的過(guò)程中,從庫(kù)的恢復(fù)過(guò)程中依賴(lài)于relay log的相關(guān)信息,所以這里要將relay log的自動(dòng)清除設(shè)置為OFF,采用手動(dòng)清除relay log的方式。在默認(rèn)情況下,從服務(wù)器上的中繼日志會(huì)在SQL線程執(zhí)行完畢后被自動(dòng)刪除。但是在MHA環(huán)境中,這些中繼日志在恢復(fù)其他從服務(wù)器時(shí)可能會(huì)被用到,因此需要禁用中繼日志的自動(dòng)刪除功能。定期清除中繼日志需要考慮到復(fù)制延時(shí)的問(wèn)題。在ext3的文件系統(tǒng)下,刪除大的文件需要一定的時(shí)間,會(huì)導(dǎo)致嚴(yán)重的復(fù)制延時(shí)。為了避免復(fù)制延時(shí),需要暫時(shí)為中繼日志創(chuàng)建硬鏈接,因?yàn)樵趌inux系統(tǒng)中通過(guò)硬鏈接刪除大文件速度會(huì)很快。(在mysql數(shù)據(jù)庫(kù)中,刪除大表時(shí),通常也采用建立硬鏈接的方式)
MHA節(jié)點(diǎn)中包含了pure_relay_logs命令工具,它可以為中繼日志創(chuàng)建硬鏈接,執(zhí)行SET GLOBAL relay_log_purge=1,等待幾秒鐘以便SQL線程切換到新的中繼日志,再執(zhí)行SET GLOBAL relay_log_purge=0。
pure_relay_logs腳本參數(shù)如下所示:
--user mysql 用戶(hù)名--password mysql 密碼--port 端口號(hào)--workdir 指定創(chuàng)建relay log的硬鏈接的位置,默認(rèn)是/var/tmp,由于系統(tǒng)不同分區(qū)創(chuàng)建硬鏈接文件會(huì)失敗,故需要執(zhí)行硬鏈接具體位置,成功執(zhí)行腳本后,硬鏈接的中繼日志文件被刪除--disable_relay_log_purge 默認(rèn)情況下,如果relay_log_purge=1,腳本會(huì)什么都不清理,自動(dòng)退出,通過(guò)設(shè)定這個(gè)參數(shù),當(dāng)relay_log_purge=1的情況下會(huì)將relay_log_purge設(shè)置為0。清理relay log之后,最后將參數(shù)設(shè)置為OFF。
(3)設(shè)置定期清理relay腳本(兩臺(tái)slave服務(wù)器)
[root@192.168.0.60 ~]# cat purge_relay_log.sh #!/bin/bash user=rootpasswd=123456port=3306log_dir='/data/masterha/log'work_dir='/data'purge='/usr/local/bin/purge_relay_logs'if [ ! -d $log_dir ]then mkdir $log_dir -pfi$purge --user=$user --password=$passwd --disable_relay_log_purge --port=$port --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1[root@192.168.0.60 ~]#
添加到crontab定期執(zhí)行
[root@192.168.0.60 ~]# crontab -l0 4 * * * /bin/bash /root/purge_relay_log.sh[root@192.168.0.60 ~]#
purge_relay_logs腳本刪除中繼日志不會(huì)阻塞SQL線程。下面我們手動(dòng)執(zhí)行看看什么情況。
[root@192.168.0.60 ~]# purge_relay_logs --user=root --password=123456 --port=3306 -disable_relay_log_purge --workdir=/data/2014-04-20 15:47:24: purge_relay_logs script started. Found relay_log.info: /data/mysql/relay-log.info Removing hard linked relay log files server03-relay-bin* under /data/.. done. Current relay log file: /data/mysql/server03-relay-bin.000002 Archiving unused relay log files (up to /data/mysql/server03-relay-bin.000001) ... Creating hard link for /data/mysql/server03-relay-bin.000001 under /data//server03-relay-bin.000001 .. ok. Creating hard links for unused relay log files completed. 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. Removing hard linked relay log files server03-relay-bin* under /data/.. done.2014-04-20 15:47:27: All relay log purging operations succeeded. [root@192.168.0.60 ~]#
6.檢查SSH配置
檢查MHA Manger到所有MHA Node的SSH連接狀態(tài):
[root@192.168.0.20 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf Sun Apr 20 17:17:39 2014 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Sun Apr 20 17:17:39 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf.. Sun Apr 20 17:17:39 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf.. Sun Apr 20 17:17:39 2014 - [info] Starting SSH connection tests.. Sun Apr 20 17:17:40 2014 - [debug] Sun Apr 20 17:17:39 2014 - [debug] Connecting via SSH from root@192.168.0.50(192.168.0.50:22) to root@192.168.0.60(192.168.0.60:22).. Sun Apr 20 17:17:39 2014 - [debug] ok. Sun Apr 20 17:17:39 2014 - [debug] Connecting via SSH from root@192.168.0.50(192.168.0.50:22) to root@192.168.0.70(192.168.0.70:22).. Sun Apr 20 17:17:39 2014 - [debug] ok. Sun Apr 20 17:17:40 2014 - [debug] Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.60(192.168.0.60:22) to root@192.168.0.50(192.168.0.50:22).. Sun Apr 20 17:17:40 2014 - [debug] ok. Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.60(192.168.0.60:22) to root@192.168.0.70(192.168.0.70:22).. Sun Apr 20 17:17:40 2014 - [debug] ok. Sun Apr 20 17:17:41 2014 - [debug] Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.70(192.168.0.70:22) to root@192.168.0.50(192.168.0.50:22).. Sun Apr 20 17:17:40 2014 - [debug] ok. Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.70(192.168.0.70:22) to root@192.168.0.60(192.168.0.60:22).. Sun Apr 20 17:17:41 2014 - [debug] ok. Sun Apr 20 17:17:41 2014 - [info] All SSH connection tests passed successfully.
可以看見(jiàn)各個(gè)節(jié)點(diǎn)ssh驗(yàn)證都是ok的。
7.檢查整個(gè)復(fù)制環(huán)境狀況。
通過(guò)masterha_check_repl腳本查看整個(gè)集群的狀態(tài)
[root@192.168.0.20 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf Sun Apr 20 18:36:55 2014 - [info] Checking replication health on 192.168.0.60.. Sun Apr 20 18:36:55 2014 - [info] ok. Sun Apr 20 18:36:55 2014 - [info] Checking replication health on 192.168.0.70.. Sun Apr 20 18:36:55 2014 - [info] ok. Sun Apr 20 18:36:55 2014 - [info] Checking master_ip_failover_script status: Sun Apr 20 18:36:55 2014 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 Bareword "FIXME_xxx" not allowed while "strict subs" in use at /usr/local/bin/master_ip_failover line 88. Execution of /usr/local/bin/master_ip_failover aborted due to compilation errors. Sun Apr 20 18:36:55 2014 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln214] Failed to get master_ip_failover_script status with return code 255:0. Sun Apr 20 18:36:55 2014 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln383] Error happend on checking configurations. at /usr/local/bin/masterha_check_repl line 48Sun Apr 20 18:36:55 2014 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln478] Error happened on monitoring servers. Sun Apr 20 18:36:55 2014 - [info] Got exit code 1 (Not master dead). MySQL Replication Health is NOT OK!
發(fā)現(xiàn)最后的結(jié)論說(shuō)我的復(fù)制不是ok的。但是上面的信息明明說(shuō)是正常的,自己也進(jìn)數(shù)據(jù)庫(kù)查看了。這里一直踩坑。一直糾結(jié),后來(lái)無(wú)意中發(fā)現(xiàn)火丁筆記的博客,這才知道了原因,原來(lái)Failover兩種方式:一種是虛擬IP地址,一種是全局配置文件。MHA并沒(méi)有限定使用哪一種方式,而是讓用戶(hù)自己選擇,虛擬IP地址的方式會(huì)牽扯到其它的軟件,比如keepalive軟件,而且還要修改腳本master_ip_failover。(最后修改腳本后才沒(méi)有這個(gè)報(bào)錯(cuò),自己不懂perl也是折騰的半死,去年買(mǎi)了塊表)
如果發(fā)現(xiàn)如下錯(cuò)誤:
Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 99.mysqlbinlog version not found!
Testing mysql connection and privileges..sh: mysql: command not found
解決方法如下,添加軟連接(所有節(jié)點(diǎn))
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
所以先暫時(shí)注釋master_ip_failover_script= /usr/local/bin/master_ip_failover這個(gè)選項(xiàng)。后面引入keepalived后和修改該腳本以后再開(kāi)啟該選項(xiàng)。
[root@192.168.0.20 ~]# grep master_ip_failover /etc/masterha/app1.cnf #master_ip_failover_script= /usr/local/bin/master_ip_failover [root@192.168.0.20 ~]#
再次進(jìn)行狀態(tài)查看:
Sun Apr 20 18:46:08 2014 - [info] Checking replication health on 192.168.0.60.. Sun Apr 20 18:46:08 2014 - [info] ok. Sun Apr 20 18:46:08 2014 - [info] Checking replication health on 192.168.0.70.. Sun Apr 20 18:46:08 2014 - [info] ok. Sun Apr 20 18:46:08 2014 - [warning] master_ip_failover_script is not defined. Sun Apr 20 18:46:08 2014 - [warning] shutdown_script is not defined. Sun Apr 20 18:46:08 2014 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
已經(jīng)沒(méi)有明顯報(bào)錯(cuò),只有兩個(gè)警告而已,復(fù)制也顯示正常了。
8.檢查MHA Manager的狀態(tài):
通過(guò)master_check_status腳本查看Manager的狀態(tài):
[root@192.168.0.20 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf app1 is stopped(2:NOT_RUNNING). [root@192.168.0.20 ~]#
注意:如果正常,會(huì)顯示"PING_OK",否則會(huì)顯示"NOT_RUNNING",這代表MHA監(jiān)控沒(méi)有開(kāi)啟。
9.開(kāi)啟MHA Manager監(jiān)控
[root@192.168.0.20 ~]# 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 & [1] 30867[root@192.168.0.20 ~]#
啟動(dòng)參數(shù)介紹:
--remove_dead_master_conf 該參數(shù)代表當(dāng)發(fā)生主從切換后,老的主庫(kù)的ip將會(huì)從配置文件中移除。
--manger_log 日志存放位置
--ignore_last_failover 在缺省情況下,如果MHA檢測(cè)到連續(xù)發(fā)生宕機(jī),且兩次宕機(jī)間隔不足8小時(shí)的話,則不會(huì)進(jìn)行Failover,之所以這樣限制是為了避免ping-pong效應(yīng)。該參數(shù)代表忽略上次MHA觸發(fā)切換產(chǎn)生的文件,默認(rèn)情況下,MHA發(fā)生切換后會(huì)在日志目錄,也就是上面我設(shè)置的/data產(chǎn)生app1.failover.complete文件,下次再次切換的時(shí)候如果發(fā)現(xiàn)該目錄下存在該文件將不允許觸發(fā)切換,除非在第一次切換后收到刪除該文件,為了方便,這里設(shè)置為--ignore_last_failover。
查看MHA Manager監(jiān)控是否正常:
[root@192.168.0.20 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf app1 (pid:20386) is running(0:PING_OK), master:192.168.0.50[root@192.168.0.20 ~]#
可以看見(jiàn)已經(jīng)在監(jiān)控了,而且master的主機(jī)為192.168.0.50
10.查看啟動(dòng)日志
[root@192.168.0.20 ~]# tail -n20 /var/log/masterha/app1/manager.logSun Apr 20 19:12:01 2014 - [info] Connecting to root@192.168.0.70(192.168.0.70:22).. Checking slave recovery environment settings.. Opening /data/mysql/relay-log.info ... ok. Relay log found at /data/mysql, up to server04-relay-bin.000002 Temporary relay log file is /data/mysql/server04-relay-bin.000002 Testing mysql connection and privileges.. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Sun Apr 20 19:12:01 2014 - [info] Slaves settings check done. Sun Apr 20 19:12:01 2014 - [info] 192.168.0.50 (current master) +--192.168.0.60 +--192.168.0.70Sun Apr 20 19:12:01 2014 - [warning] master_ip_failover_script is not defined. Sun Apr 20 19:12:01 2014 - [warning] shutdown_script is not defined. Sun Apr 20 19:12:01 2014 - [info] Set master ping interval 1 seconds. Sun Apr 20 19:12:01 2014 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s server03 -s server02 --user=root --master_host=server02 --master_ip=192.168.0.50 --master_port=3306Sun Apr 20 19:12:01 2014 - [info] Starting ping health check on 192.168.0.50(192.168.0.50:3306).. Sun Apr 20 19:12:01 2014 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..[root@192.168.0.20 ~]#
其中"Ping(SELECT) succeeded, waiting until MySQL doesn't respond.."說(shuō)明整個(gè)系統(tǒng)已經(jīng)開(kāi)始監(jiān)控了。
11.關(guān)閉MHA Manage監(jiān)控
關(guān)閉很簡(jiǎn)單,使用masterha_stop命令完成。
[root@192.168.0.20 ~]# 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 --manager_log=/data/mamanager.log [root@192.168.0.20 ~]#
12.配置VIP
vip配置可以采用兩種方式,一種通過(guò)keepalived的方式管理虛擬ip的浮動(dòng);另外一種通過(guò)腳本方式啟動(dòng)虛擬ip的方式(即不需要keepalived或者h(yuǎn)eartbeat類(lèi)似的軟件)。
1.keepalived方式管理虛擬ip,keepalived配置方法如下:
(1)下載軟件進(jìn)行并進(jìn)行安裝(兩臺(tái)master,準(zhǔn)確的說(shuō)一臺(tái)是master,另外一臺(tái)是備選master,在沒(méi)有切換以前是slave):
[root@192.168.0.50 ~]# wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
tar xf keepalived-1.2.12.tar.gz cd keepalived-1.2.12./configure --prefix=/usr/local/keepalivedmake && make installcp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/mkdir /etc/keepalivedcp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
(2)配置keepalived的配置文件,在master上配置(192.168.0.50)
[root@192.168.0.50 ~]# cat /etc/keepalived/keepalived.conf! Configuration File for keepalived global_defs { notification_email { saltstack@163.com } notification_email_from dba@dbserver.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-HA } vrrp_instance VI_1 { state BACKUP interface eth2 virtual_router_id 51 priority 150 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.88 } } [root@192.168.0.50 ~]#
其中router_id MySQL HA表示設(shè)定keepalived組的名稱(chēng),將192.168.0.88這個(gè)虛擬ip綁定到該主機(jī)的eth2網(wǎng)卡上,并且設(shè)置了狀態(tài)為backup模式,將keepalived的模式設(shè)置為非搶占模式(nopreempt),priority 150表示設(shè)置的優(yōu)先級(jí)為150。下面的配置略有不同,但是都是一個(gè)意思。
在候選master上配置(192.168.0.60)
[root@192.168.0.60 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { saltstack@163.com } notification_email_from dba@dbserver.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-HA } vrrp_instance VI_1 { state BACKUP interface eth2 virtual_router_id 51 priority 120 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.88 } } [root@192.168.0.60 ~]#
(3)啟動(dòng)keepalived服務(wù),在master上啟動(dòng)并查看日志
[root@192.168.0.50 ~]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@192.168.0.50 ~]# tail -f /var/log/messages Apr 20 20:22:16 192 Keepalived_healthcheckers[15334]: Opening file '/etc/keepalived/keepalived.conf'. Apr 20 20:22:16 192 Keepalived_healthcheckers[15334]: Configuration is using : 7231 Bytes Apr 20 20:22:16 192 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes) Apr 20 20:22:16 192 kernel: IPVS: ipvs loaded. Apr 20 20:22:16 192 Keepalived_healthcheckers[15334]: Using LinkWatch kernel netlink reflector... Apr 20 20:22:19 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Transition to MASTER STATE Apr 20 20:22:20 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Entering MASTER STATE Apr 20 20:22:20 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) setting protocol VIPs. Apr 20 20:22:20 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.0.88Apr 20 20:22:20 192 Keepalived_healthcheckers[15334]: Netlink reflector reports IP 192.168.0.88 added Apr 20 20:22:25 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.0.88
發(fā)現(xiàn)已經(jīng)將虛擬ip 192.168.0.88綁定了網(wǎng)卡eth2上。
(4)查看綁定情況
[root@192.168.0.50 ~]# ip addr | grep eth23: eth2:mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 192.168.0.50/24 brd 192.168.0.255 scope global eth2 inet 192.168.0.88/32 scope global eth2 [root@192.168.0.50 ~]#
在另外一臺(tái)服務(wù)器,候選master上啟動(dòng)keepalived服務(wù),并觀察
[root@192.168.0.60 ~]# /etc/init.d/keepalived start ; tail -f /var/log/messages Starting keepalived: [ OK ] Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Registering gratuitous ARP shared channel Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Opening file '/etc/keepalived/keepalived.conf'. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Configuration is using : 62976 Bytes Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Using LinkWatch kernel netlink reflector... Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP_Instance(VI_1) Entering BACKUP STATEApr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)] Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.80.138 added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.0.60 added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6a9e added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6aa8 added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink reflector Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink command channel Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Opening file '/etc/keepalived/keepalived.conf'. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Configuration is using : 7231 Bytes Apr 20 20:26:18 192 kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP) Apr 20 20:26:18 192 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes) Apr 20 20:26:18 192 kernel: IPVS: ipvs loaded. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Using LinkWatch kernel netlink reflector...
從上面的信息可以看到keepalived已經(jīng)配置成功。
注意:
上面兩臺(tái)服務(wù)器的keepalived都設(shè)置為了BACKUP模式,在keepalived中2種模式,分別是master->backup模式和backup->backup模式。這兩種模式有很大區(qū)別。在master->backup模式下,一旦主庫(kù)宕機(jī),虛擬ip會(huì)自動(dòng)漂移到從庫(kù),當(dāng)主庫(kù)修復(fù)后,keepalived啟動(dòng)后,還會(huì)把虛擬ip搶占過(guò)來(lái),即使設(shè)置了非搶占模式(nopreempt)搶占ip的動(dòng)作也會(huì)發(fā)生。在backup->backup模式下,當(dāng)主庫(kù)宕機(jī)后虛擬ip會(huì)自動(dòng)漂移到從庫(kù)上,當(dāng)原主庫(kù)恢復(fù)和keepalived服務(wù)啟動(dòng)后,并不會(huì)搶占新主的虛擬ip,即使是優(yōu)先級(jí)高于從庫(kù)的優(yōu)先級(jí)別,也不會(huì)發(fā)生搶占。為了減少ip漂移次數(shù),通常是把修復(fù)好的主庫(kù)當(dāng)做新的備庫(kù)。
(5)MHA引入keepalived(MySQL服務(wù)進(jìn)程掛掉時(shí)通過(guò)MHA 停止keepalived):
要想把keepalived服務(wù)引入MHA,我們只需要修改切換是觸發(fā)的腳本文件master_ip_failover即可,在該腳本中添加在master發(fā)生宕機(jī)時(shí)對(duì)keepalived的處理。
編輯腳本/usr/local/bin/master_ip_failover,修改后如下,我對(duì)perl不熟悉,所以我這里完整貼出該腳本(主庫(kù)上操作,192.168.0.50)。
在MHA Manager修改腳本修改后的內(nèi)容如下(參考資料比較少):
warnings FATAL => Getopt::, , , , , , , = = = => \, => \, => \, => \, => \, => \, => \, => \, & ( eq || eq = & = ( eq = & = ( eq & \@ \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)修改這個(gè)腳本了,我們現(xiàn)在打開(kāi)在上面提到過(guò)的參數(shù),再檢查集群狀態(tài),看是否會(huì)報(bào)錯(cuò)。
[root@192.168.0.20 ~]# grep 'master_ip_failover_script' /etc/masterha/app1.cnf master_ip_failover_script= /usr/local/bin/master_ip_failover [root@192.168.0.20 ~]#
[root@192.168.0.20 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf Sun Apr 20 23:10:01 2014 - [info] Slaves settings check done. Sun Apr 20 23:10:01 2014 - [info] 192.168.0.50 (current master) +--192.168.0.60 +--192.168.0.70Sun Apr 20 23:10:01 2014 - [info] Checking replication health on 192.168.0.60.. Sun Apr 20 23:10:01 2014 - [info] ok. Sun Apr 20 23:10:01 2014 - [info] Checking replication health on 192.168.0.70.. Sun Apr 20 23:10:01 2014 - [info] ok. Sun Apr 20 23:10:01 2014 - [info] Checking master_ip_failover_script status: Sun Apr 20 23:10:01 2014 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 Sun Apr 20 23:10:01 2014 - [info] OK. Sun Apr 20 23:10:01 2014 - [warning] shutdown_script is not defined. Sun Apr 20 23:10:01 2014 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
可以看見(jiàn)已經(jīng)沒(méi)有報(bào)錯(cuò)了。哈哈
/usr/local/bin/master_ip_failover添加或者修改的內(nèi)容意思是當(dāng)主庫(kù)數(shù)據(jù)庫(kù)發(fā)生故障時(shí),會(huì)觸發(fā)MHA切換,MHA Manager會(huì)停掉主庫(kù)上的keepalived服務(wù),觸發(fā)虛擬ip漂移到備選從庫(kù),從而完成切換。當(dāng)然可以在keepalived里面引入腳本,這個(gè)腳本監(jiān)控mysql是否正常運(yùn)行,如果不正常,則調(diào)用該腳本殺掉keepalived進(jìn)程。
2.通過(guò)腳本的方式管理VIP。這里是修改/usr/local/bin/master_ip_failover,也可以使用其他的語(yǔ)言完成,比如php語(yǔ)言。使用php腳本編寫(xiě)的failover這里就不介紹了。修改完成后內(nèi)容如下,而且如果使用腳本管理vip的話,需要手動(dòng)在master服務(wù)器上綁定一個(gè)vip(發(fā)現(xiàn)修改修改對(duì)perl竟然有感覺(jué)了。難道我適合學(xué)Perl?^_^)
[root@192.168.0.50 ~]# /sbin/ifconfig eth2:1 192.168.0.88/24
通過(guò)腳本來(lái)維護(hù)vip的測(cè)試我這里就不說(shuō)明了,童鞋們自行測(cè)試,腳本如下(測(cè)試通過(guò))
warnings FATAL => Getopt::, , , , , , , = = = = => \, => \, => \, => \, => \, => \, => \, => \, & ( eq || eq = & = ( eq = & = ( eq & \@ \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
為了防止腦裂發(fā)生,推薦生產(chǎn)環(huán)境采用腳本的方式來(lái)管理虛擬ip,而不是使用keepalived來(lái)完成。到此為止,基本MHA集群已經(jīng)配置完畢。接下來(lái)就是實(shí)際的測(cè)試環(huán)節(jié)了。通過(guò)一些測(cè)試來(lái)看一下MHA到底是如何進(jìn)行工作的。下面將從MHA自動(dòng)failover,我們手動(dòng)failover,在線切換三種方式來(lái)介紹MHA的工作情況。
一.自動(dòng)Failover(必須先啟動(dòng)MHA Manager,否則無(wú)法自動(dòng)切換,當(dāng)然手動(dòng)切換不需要開(kāi)啟MHA Manager監(jiān)控。各位童鞋請(qǐng)參考前面啟動(dòng)MHA Manager)
測(cè)試環(huán)境再次貼一下,文章太長(zhǎng),自己都搞暈了。
角色 ip地址 主機(jī)名 server_id 類(lèi)型 Monitor host 192.168.0.20 server01 -
當(dāng)前題目:MySQL之MHA分享
URL鏈接:http://weahome.cn/article/psocoh.html