【Mysql】MHA配置虚拟ip
虚拟ip飘逸
-
说到Failover,通常有两种方式:一种是虚拟IP地址,一种是全局配置文件。
- MHA并没有限定使用哪一种方式,而是让用户自己选择,虚拟IP地址的方式会牵扯到其它的软件,这里就不赘述了
-
上篇文章搭建mha+keepalive就是通过keepalive来控制虚拟ip,本片文章在介绍一种虚拟ip的方式
配置虚拟ip
- 采用ifconfig的方式
-
/sbin/ifconfig eth0:1 192.168.6.66/24
- /sbin/ifconfig eth0:1 down
-
[root@localhost app1]# more /usr/local/bin/master_ip_online_change_script
-
#!/usr/bin/env perl
-
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 = '192.168.6.66/24'; # Virtual IP
-
my $key = "1";
-
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,
-
);
-
-
exit &main();
-
-
sub main {
-
-
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
-
-
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 {
-
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" ) {
-
-
# 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();
-
$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\@cluster1 \" $ssh_start_vip \"`;
-
exit 0;
-
}
-
else {
-
&usage();
-
exit 1;
-
}
-
}
-
-
# A simple system call that enable the VIP on the new master
-
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=po
-
rt\n";
- }
将此脚本复制两次到/usr/local/bin, 分别命名为master_ip_failover 和master_ip_online_change_script
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比德令哈网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式德令哈网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖德令哈地区。费用合理售后完善,十载实体公司更值得信赖。
然后将/etc/app1.cnf 中下面两行注释去掉:
master_ip_failover_script=/usr/local/bin/master_ip_failover master_ip_online_change_script=/usr/local/bin/master_ip_online_change_script
实验
-
1.主库(115)添加一个vip
-
[root@node2 .ssh]# /sbin/ifconfig eth0:1 192.168.6.66/24
[root@node2 .ssh]# ip a
1: lo:mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
inet 192.168.6.66/24brd 192.168.6.255 scope global secondary eth0:1
inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
valid_lft forever preferred_lft forever -
-
2.关闭主库MySQL
-
[root@node2 .ssh]# /etc/init.d/mysqld stop
Stopping mysqld: [ OK ]
[root@node2 .ssh]# ip a
1: lo:mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0 ---vip飘走了
inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
valid_lft forever preferred_lft forever
-
3 查看newmaster(114)的ip ----vip已飘到new master上了
-
[root@node1 ~]# ip a
1: lo:mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
inet 192.168.6.66/24brd 192.168.6.255 scope global secondary eth0:1
inet6 fe80::a00:27ff:fe7a:c69c/64 scope link
valid_lft forever preferred_lft forever
- 4 重构mysql.重启mha监控
- 5 关闭114mysql,vip飘回115上去了
本文名称:【Mysql】MHA配置虚拟ip
链接地址:http://azwzsj.com/article/gdsdgd.html