Centos7 Mysql5.6.40 高可用架构--MHA-vip功能(应用透明)
master_ip_failover
配置VIP,以便在MHA集群中master主机宕机后,让客户端无需修改数据库地址,通过VIP进行无缝切换。
注意自带的VIP漂移脚本只能在同网段使用,跨网段不行
使用keeplived可以跨网段
- 上传源码包里的应用漂移脚本
master_ip_failover
(需要修改不然无法使用)
#!/usr/bin/env perl
# opyright (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;
use MHA::DBHelper;
my (
$command, $ssh_user, $orig_master_host,
$orig_master_ip, $orig_master_port, $new_master_host,
$new_master_ip, $new_master_port, $new_master_user,
$new_master_password
);
my $vip = '10.0.0.55/24';
my $key = "0";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
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,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
);
exit &main();
sub main {
if ( $command eq "stop" || $command eq "stopssh" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {
# updating global catalog, etc
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
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";
}
- 转换脚本字符预防中文字符导致错误
dos2unix master_ip_failover
- 编辑 master_ip_failover 脚本添加下面命令:
my $vip = '10.0.0.129/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
- 更改manager配置文件:
添加:
master_ip_failover_script=/usr/local/bin/master_ip_failover
授权:chmod +x master_ip_failover
添加脚本可执行权限
-
主库上,手工生成第一个vip地址
手工在主库上绑定vip,注意一定要和配置文件中ethN
一致
ifconfig eth0:0 10.0.0.55/24
-
重启mha
masterha_stop --conf=/etc/mha/app1.cnf
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
- 测试vip 漂移脚本是否有效
- 宕掉主库:
/etc/init.d/mysqld stop
- ifconfig 查看主库网卡配置(发现虚拟ip已经移除)
- 查看从库
故障修复
- 启动恢复宕掉的数据库
- 恢复主从环境:查看manager 日志,复制change master to,添加到宕掉的数据库里
- 恢复manager:修改 MHA 配置文件 ,把宕掉的数据库IP地址加上
- 加上后
- 检测ssh互信: masterha_check_ssh --conf=/etc/mha/app1.cnf
- 检测主从环境:masterha_check_repl --conf=/etc/mha/app1.cnf
- 重启MHA: nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
- 检测MHA是否启动成功:masterha_check_status --conf=/etc/mha/app1.cnf