环境: manager: 10.1.99.213(安装mhamanagermhanode包)安装依赖可以使用机器对应版本的epel源 master:10.1.99.216(安装mysql以及mhanode包) node1:10.1.99.214(安装mysql以及mhanode包) node2:10.1.99.215(安装mysql以及mhanode包)
依赖: yuminstallperl-DBD-MySQLyuminstallperl-Config-Tinyyuminstallperl-Log-Dispatchyuminstallperl-Parallel-ForkManager
关于MHAfailover的处理过程: ①保存宕机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线程)
配置过程(mhamanager的配置文件在文尾app1.conf): ①主机间互相信任配置(sshkey-gen以及ssh-copy-i等) ②master以及node之间的主从配置(都得设置复制账户repl(replicationslave),以及管理账户mha(ALL),开启log-bin参数(故障切换提神master只能切换到开启该参数的机器),node得配置relay-log-purge=0,read_only=1等) ③manager配置文件/etc/mha/app1.conf ④managerssh检查(masterha_check_ssh--conf=/etc/mha/app1.cnf) ⑤managerrepl检查(masterha_check_repl--conf=/etc/mha/app1.cnf) ⑥启动mhamanager(masterha_manager--conf=/etc/mha/app1.cnf)不要后台启动,会退出 ⑦检查manager状态(masterha_check_status--conf=/etc/mha/app1.cnf) ⑧关闭mhamanager(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(指向内容查看日志来确定catapp1.log|grep-i"changemaster") ③启动mhamanager ④若想还是用原来的实例作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, MA02110-1301USA
## 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
FriOct1418:22:582016-[info]CheckingMHAisnotmonitoringordoingfailover.. FriOct1418:22:582016-[error][/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm,ln142]Gettingadvisorylockfailedonthecurrentmaster.MHAMonitorrunsonthecurrentmaster.StopMHAManager/Monitorandtryagain. FriOct1418:22:582016-[error][/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm,ln177]GotERROR:at/usr/bin/masterha_master_switchline53.
在进行MHA手动切换时,需要将负责自动切换的监控工具masterha_manager关闭,才可完成手动切换。
参考:MySQL MHA 搭建&测试
注意:slave要设置read_only=1relay_log_purge=1 定时任务删除relay_log