环境:
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