MHA基礎知識
環境:
manager: 10.1.99.213(安裝mha manager mha node包)安裝依賴可以使用機器對應版本的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
關于MHA failover的處理過程:
①保存宕機master的未來得及傳輸的binlog
②檢查最新狀態的slave,應用差異中繼日志到各個slave
③應用master的binlog到各個slave
④提升一個slave為主
⑤其他slave指向新master
Manager工具包:
masterha_check_ssh 檢查MHA的SSH配置狀況
masterha_check_repl 檢查MySQL復制狀況
masterha_manger 啟動MHA
masterha_check_status 檢測當前MHA運行狀態
masterha_master_monitor 檢測master是否宕機
masterha_master_switch 控制故障轉移(自動或者手動)
masterha_conf_host 添加或刪除配置的server信息
Node工具包(自動調用 無需手動執行):
save_binary_logs 保存和復制master的二進制日志
apply_diff_relay_logs 識別差異的中繼日志事件并將其差異的事件應用于其他的slave
purge_relay_logs 清除中繼日志(不會阻塞SQL線程 )
配置過程(mha manager的配置文件在文尾app1.conf):
①主機間互相信任配置(sshkey-gen 以及 ssh-copy -i 等)
②master 以及node之間的主從配置 (都得設置復制賬戶repl(replication slave),以及管理賬戶mha(ALL),開啟log-bin參數(故障切換提神master只能切換到開啟該參數的機器),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)
⑥啟動mha manager(masterha_manager --conf=/etc/mha/app1.cnf )不要后臺啟動,會退出
⑦檢查manager狀態(masterha_check_status --conf=/etc/mha/app1.cnf)
⑧關閉mha manager(masterha_stop --conf=/etc/mha/app1.cnf)
參數:
--ignore_last_failover:在缺省情況下,如果MHA檢測到連續發生宕機,且兩次宕機間隔不足8小時的話,則不會進行Failover,之所以這樣限制是為了避免ping-pong效應。該參數代表忽略上次MHA觸發切換產生的文件,默認情況下,MHA發生切換后會在日志目錄,也就是上面我設置的/data產生app1.failover.complete文件,下次再次切換的時候如果發現該目錄下存在該文件將不允許觸發切換,除非在第一次切換后收到刪除該文件
--remove_dead_master_conf: 該參數代表當發生主從切換后,老的主庫將會從配置文件中移除,一般情況下不需要開啟
Failover之后的處理:
①啟動宕機master
②自己作為從指向新的master(指向內容查看日志來確定 cat app1.log | grep -i "change master")
③啟動mha manager
④若想還是用原來的實例作master,則使用該命令masterha_master_switch手動切換
附件 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; either version 2 of the License, or
-
# (at your option) any later version.
-
#
-
# This program is distributed in the hope that it will be useful,
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
# GNU General Public License for more details.
-
#
-
# You should have received a copy of the GNU General Public License
-
# along with this program; if not, write to the Free Software
-
# Foundation, Inc.,
-
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-
## Note: This is a sample script and is not complete. Modify the script based on your environment.
-
-
use strict;
-
use warnings FATAL => 'all';
-
-
use 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回復模式,更新鄰居。要是不加則服務器會自動等到vip緩存失效,期間VIP會有一定時間的不可用。
-
-
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\n
-
}
手動切換報錯:
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.
在進行MHA手動切換時,需要將負責自動切換的監控工具masterha_manager關閉,才可完成手動切換。
參考:MySQL MHA 搭建&測試
注意:slave要設置 read_only=1 relay_log_purge=1
定時任務刪除relay_log