一、数据库VIP漂移
1.VIP漂移的两种方式
1.通过keepalived的方式
2.使用MHA自带的脚本
2.配置MHA脚本
[root@db03 ~]# vim /service/mha/app1.cnf
[server default]
master_ip_failover_script=/service/mha/master_ip_failover
3.编写脚本
1)上传脚本
#解压manager节点的tar包有脚本
[root@db01 ~/mha4mysql-manager-0.56/samples/scripts]# ll
总用量 32
-rwxr-xr-x 1 4984 users 3648 4月 1 2014 master_ip_failover
#上传现成的脚本
[root@db03 ~]# cd /service/mha/
[root@db03 /service/mha]# rz master_ip_failover
#授权执行权限
[root@db03 /service/mha]# chmod +x master_ip_failover
2)配置脚本
[root@db03 /service/mha]# vim master_ip_failover
... ...
my $vip = '172.16.1.55/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth1:$key down";
... ...
4.启动前手动绑定VIP
#绑定子网卡
[root@db02 ~]# ifconfig eth1:1 172.16.1.55/24
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:41:0e:db brd ff:ff:ff:ff:ff:ff
inet 172.16.1.52/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet 172.16.1.55/24 brd 172.16.1.255 scope global secondary eth1:1
valid_lft forever preferred_lft forever
#解绑网卡
[root@db02 ~]# ifconfig eth1:1 down
5.启动MHA
[root@db03 ~]# masterha_stop --conf=/service/mha/app1.cnf
Stopped app1 successfully.
#启动mha
[root@db03 ~]# nohup masterha_manager --conf=/service/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /service/mha/app1/manager.log 2>&1 &
#启动失败
1.检查是否有执行权限
[root@db03 ~]# chmod +x /service/mha/master_ip_failover
2.语法是否正确
3.文件格式
[root@db03 ~]# yum install -y dos2unix
[root@db03 ~]# dos2unix /service/mha/master_ip_failover
dos2unix: converting file /service/mha/master_ip_failover to Unix format ...
6.测试VIP漂移
#从库查看主库信息
[root@db03 ~]# mysql
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.52
Master_User: rep
Master_Port: 3306
#主库查看IP
[root@db02 ~]# ip addr
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:41:0e:db brd ff:ff:ff:ff:ff:ff
inet 172.16.1.52/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet 172.16.1.55/24 brd 172.16.1.255 scope global secondary eth1:1
valid_lft forever preferred_lft forever
#停止主库
[root@db02 ~]# systemctl stop mysqld
#再次查看主库信息
[root@db03 ~]# mysql
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.51
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
#查看新主库的IP
[root@db01 ~]# ip addr
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:6c:a6:c6 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.51/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet 172.16.1.55/24 brd 172.16.1.255 scope global secondary eth1:1
valid_lft forever preferred_lft forever