環(huán)境:
manager: 10.1.99.213(安裝mha manager mha node包)安裝依賴可以使用機(jī)器對(duì)應(yīng)版本的epel源
master: 10.1.99.216(安裝MySQL 以及 mha node包)
node1 : 10.1.99.214(安裝mysql 以及 mha node包)
node2 : 10.1.99.215(安裝mysql 以及 mha node包)
依賴:
yum install perl-DBD-MySQL
yum install perl-Config-Tiny
yum install perl-Log-Dispatch
yum install perl-Parallel-ForkManager
關(guān)于MHA failover的處理過程:
①保存宕機(jī)master的未來得及傳輸?shù)腷inlog
②檢查最新狀態(tài)的slave,應(yīng)用差異中繼日志到各個(gè)slave
③應(yīng)用master的binlog到各個(gè)slave
④提升一個(gè)slave為主
⑤其他slave指向新master
Manager工具包:
masterha_check_ssh 檢查MHA的SSH配置狀況
masterha_check_repl 檢查MySQL復(fù)制狀況
masterha_manger 啟動(dòng)MHA
masterha_check_status 檢測當(dāng)前MHA運(yùn)行狀態(tài)
masterha_master_monitor 檢測master是否宕機(jī)
masterha_master_switch 控制故障轉(zhuǎn)移(自動(dòng)或者手動(dòng))
masterha_conf_host 添加或刪除配置的server信息
十年的余慶網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整余慶建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“余慶網(wǎng)站設(shè)計(jì)”,“余慶網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
Node工具包(自動(dòng)調(diào)用 無需手動(dòng)執(zhí)行):
save_binary_logs 保存和復(fù)制master的二進(jìn)制日志
apply_diff_relay_logs 識(shí)別差異的中繼日志事件并將其差異的事件應(yīng)用于其他的slave
purge_relay_logs 清除中繼日志(不會(huì)阻塞SQL線程 )
配置過程(mha manager的配置文件在文尾app1.conf):
①主機(jī)間互相信任配置(sshkey-gen 以及 ssh-copy -i 等)
②master 以及node之間的主從配置 (都得設(shè)置復(fù)制賬戶repl(replication slave),以及管理賬戶mha(ALL),開啟log-bin參數(shù)(故障切換提神master只能切換到開啟該參數(shù)的機(jī)器),node得配置relay-log-purge=0,read_only=1等)
③manager 配置文件/etc/mha/app1.conf
④manager ssh檢查(masterha_check_ssh --conf=/etc/mha/app1.cnf)
⑤manager repl檢查(masterha_check_repl --conf=/etc/mha/app1.cnf)
⑥啟動(dòng)mha manager(masterha_manager --conf=/etc/mha/app1.cnf )不要后臺(tái)啟動(dòng),會(huì)退出
⑦檢查manager狀態(tài)(masterha_check_status --conf=/etc/mha/app1.cnf)
⑧關(guān)閉mha manager(masterha_stop --conf=/etc/mha/app1.cnf)
參數(shù):
--ignore_last_failover:在缺省情況下,如果MHA檢測到連續(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ā)切換,除非在第一次切換后收到刪除該文件
--remove_dead_master_conf: 該參數(shù)代表當(dāng)發(fā)生主從切換后,老的主庫將會(huì)從配置文件中移除,一般情況下不需要開啟
Failover之后的處理:
①啟動(dòng)宕機(jī)master
②自己作為從指向新的master(指向內(nèi)容查看日志來確定 cat app1.log | grep -i "change master")
③啟動(dòng)mha manager
④若想還是用原來的實(shí)例作master,則使用該命令masterha_master_switch手動(dòng)切換
附件 app1.conf
-
####################BEGIN#########
-
[server default]
-
manager_log=/tmp/app1.log
-
manager_workdir=/tmp
-
master_binlog_dir=/opt/mysql
-
user=mha
-
password=taomee
-
ping_interval=2
-
repl_password=taomee
-
repl_user=repl
-
ssh_user=root
-
-
master_ip_failover_script=/usr/bin/masterha_ip_failover
-
[server1]
-
hostname=10.1.99.216
-
port=3306
-
-
[server2]
-
hostname=10.1.99.215
-
port=3306
-
-
[server3]
-
hostname=10.1.99.214
-
port=3306
-
#######################END##########
cat masterha_ip_failover
-
#!/usr/bin/env perl
-
-
# Copyright(C) 2011 DeNA Co.,Ltd.
-
#
-
# This program is free software; you can redistribute it and/or modify
-
# it under the terms of the GNU General Public License as published by
-
# the Free Software Foundation; eitherversion 2 of the License, or
-
#(at youroption) any laterversion.
-
#
-
# This program is distributed in the hope that it will be useful,
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
-
# MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE. See the
-
# GNU General Public Licensefor more details.
-
#
-
# You should have receiveda copy of the GNU General Public License
-
# along with this program; if not, write to the Free Software
-
# Foundation, Inc.,
-
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-
## Note: This isa samplescript and is not complete. Modify thescript based on your environment.
-
-
use strict;
-
use warnings FATAL=> 'all';
-
-
use Getopt::Long;
-
-
my(
-
$command, $ssh_user, $orig_master_host, $orig_master_ip,
-
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
-
);
-
-
my $vip= '10.1.99.233/24';
-
my $key= '0';
-
my $ssh_start_vip= "/sbin/ifconfig eth0:$key $vip";
-
my $ssh_stop_vip= "/sbin/ifconfig eth0:$key down";
-
my $ssh_Bcast_arp= "/usr/bin/arping -c 3 -A 10.1.99.233"; #ARP回復(fù)模式,更新鄰居。要是不加則服務(wù)器會(huì)自動(dòng)等到vip緩存失效,期間VIP會(huì)有一定時(shí)間的不可用。
-
-
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();
-
#&start_arp();
-
$exit_code= 0;
-
};
-
if($@) {
-
warn $@;
-
exit $exit_code;
-
}
-
exit $exit_code;
-
}
-
elsif( $command eq"status" ) {
-
print"Checking the Status of the script.. OK \n";
-
exit 0;
-
}
-
-
else{
-
&usage();
-
exit 1;
-
}
-
}
-
-
sub start_vip() {
-
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
-
}
-
sub start_arp() {
-
`ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
-
}
-
sub stop_vip() {
-
`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
-
}
手動(dòng)切換報(bào)錯(cuò):
masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf
Fri Oct 14 18:22:58 2016 - [info] Checking MHA is not monitoring or doing failover..
Fri Oct 14 18:22:58 2016 - [error][/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm, ln142] Getting advisory lock failed on the current master. MHA Monitor runs on the current master. Stop MHA Manager/Monitor and try again.
Fri Oct 14 18:22:58 2016 - [error][/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm, ln177] Got ERROR: at /usr/bin/masterha_master_switch line 53.
在進(jìn)行MHA手動(dòng)切換時(shí),需要將負(fù)責(zé)自動(dòng)切換的監(jiān)控工具masterha_manager關(guān)閉,才可完成手動(dòng)切換。
參考:MySQL MHA 搭建&測試
注意:slave要設(shè)置 read_only=1 relay_log_purge=1
定時(shí)任務(wù)刪除relay_log
新聞標(biāo)題:MHA基礎(chǔ)知識(shí)
鏈接URL:
http://weahome.cn/article/jjpeii.html