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 = '0'; 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 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.51 //主庫是51 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 191 Relay_Log_File: mysql-db02-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes #停掉主庫 [root@mysql-db01 ~]# systemctl stop mysqld //停掉主庫Mysql測試 #在db03上查看從庫slave信息 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.52 //主庫切換到52 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 191 Relay_Log_File: mysql-db03-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes #在db01上查看vip信息 [root@mysql-db01 ~]# ip a |grep eth0 //vip沒有了 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 #在db02上查看vip信息 [root@mysql-db02 ~]# ip a |grep eth0 //db02出現vip55 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 10.0.0.52/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
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_repl --conf=/etc/mha/app1.cnf //測試mha復制 [root@db03 mha]# masterha_check_status --conf=/etc/mha/app1.cnf //檢測mha狀態,db03查看主庫是否切換52 app1 (pid:31788) is running(0:PING_OK), master:10.0.0.52