VIP漂移的两种方式

1)通过keepalived的方式,管理虚拟IP的漂移

2)通过MHA自带脚本方式,管理虚拟IP的漂移

MHA脚本方式

虚拟ip漂移的脚本下载地址 -> wget http://download.driverzeng.com/master_ip_failover

如果是wget下载的脚本,需要转换格式:   [root@db03 mha]#  dos2unix master_ip_failover

脚本内容如下

[root@db03 ~]# cat /usr/local/bin/master_ip_failover
#!/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 = '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,
); 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();
$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 stop_vip() {
return 0 unless ($ssh_user);
`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";
}

编辑脚本

#根据配置文件中脚本路径编辑
[root@mysql-db03 ~]# vim /etc/mha/master_ip_failover #修改以下几行内容
my $vip = '10.0.0.55/24';
my $key = '';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down"; #添加执行权限,否则mha无法启动
[root@mysql-db03 ~]# chmod +x /etc/mha/master_ip_failover

修改配置文件

#编辑配置文件
[root@mysql-db03 ~]# vim /etc/mha/app1.cnf #在[server default]标签下添加
[server default]
master_ip_failover_script=/usr/local/bin/master_ip_failover //添加使用MHA自带脚本

手动绑定VIP

#绑定vip
[root@mysql-db01 ~]# ifconfig eth0:0 10.0.0.55/24
[root@db01 bin]# ifconfig eth0:0 down //解绑vip #查看vip
[root@mysql-db01 ~]# ip a |grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.0.0.51/24 brd 10.0.0.255 scope global eth0
inet 10.0.0.55/24 brd 10.0.0.255 scope global secondary eth0:0

  

测试ip漂移

#登录db02
[root@mysql-db02 ~]# mysql #查看slave信息
mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.51 //主库是51
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-db02-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #停掉主库
[root@mysql-db01 ~]# systemctl stop mysqld //停掉主库Mysql测试 #在db03上查看从库slave信息
mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.52 //主库切换到52
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-db03-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #在db01上查看vip信息
[root@mysql-db01 ~]# ip a |grep eth0 //vip没有了
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
inet 10.0.0.51/ brd 10.0.0.255 scope global eth0 #在db02上查看vip信息
[root@mysql-db02 ~]# ip a |grep eth0 //db02出现vip55
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
inet 10.0.0.52/ brd 10.0.0.255 scope global eth0
inet 10.0.0.55/ brd 10.0.0.255 scope global secondary eth0:

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log >& &
//后台运行mha masterha_check_repl --conf=/etc/mha/app1.cnf //测试mha复制 [root@db03 mha]# masterha_check_status --conf=/etc/mha/app1.cnf //检测mha状态,db03查看主库是否切换52
app1 (pid:) is running(:PING_OK), master:10.0.0.52

最新文章

  1. php常见的面试题目
  2. android support的作用及其常见错误的解决
  3. 使用MVVM框架(avalonJS)进行快速开发
  4. flex 遍历Object或者JSON对象内容的实现代码
  5. django shell 集合
  6. 隐藏index.php - ThinkPHP完全开发手册 - 3.1
  7. vi或vim快捷键
  8. linux下网络配置 命令
  9. iOS开发UI篇-懒加载、重写setter方法赋值
  10. [UIKit学习]07.关于如何选择UIButton、UILable、UIImageView
  11. js 数组去重复的方法
  12. Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式
  13. CNN:人工智能之神经网络算法进阶优化,六种不同优化算法实现手写数字识别逐步提高,应用案例自动驾驶之捕捉并识别周围车牌号—Jason niu
  14. 杂谈3.py
  15. em,px,rem的区别
  16. vCenter简单查看多少虚拟机在开机状态和一共多少虚拟机
  17. RelativeLayout 布局参数
  18. linux环境下的c++编程
  19. C/S与B/S区别
  20. linux通过wget直接下载jdk

热门文章

  1. UILabel居中显示的方法
  2. Linux内核设计第六周学习总结 分析Linux内核创建一个新进程的过程
  3. python模块之 paramiko
  4. WARNING: pgstat wait timeout
  5. web项目中日志管理工具的使用
  6. Chapter 4(栈与队列)
  7. SVN报错:Node remains in conflict显示冲突的解决办法
  8. 通过ORM模型看python对象创建过程
  9. group by实现原理及其作用
  10. 已经菜到不行了 PAT 1010. Radix (25)