1. 程式人生 > >MySQL高可用方案 MHA之二 master_ip_failover

MySQL高可用方案 MHA之二 master_ip_failover

 

[[email protected] bin]# vi master_ip_failover

 1 cat master_ip_failover
 2 #!/usr/bin/env perl
 3 use strict;
 4 use warnings FATAL => 'all';
 5 use Getopt::Long;
 6 my (
 7     $command,$ssh_user,$orig_master_host,$orig_master_ip,
 8     $orig_master_port, $new_master_host, $new_master_ip,$new_master_port
9 ); 10 11 12 my $vip = '10.150.20.200'; 13 my $ssh_start_vip = "systemctl start keepalived"; 14 my $ssh_stop_vip = "systemctl stop keepalived"; 15 16 GetOptions( 17 'command=s' => \$command, 18 'ssh_user=s' => \$ssh_user, 19 'orig_master_host=s' => \$orig_master_host,
20 'orig_master_ip=s'=>\$orig_master_ip, 21 'orig_master_port=i'=>\$orig_master_port, 22 'new_master_host=s'=>\$new_master_host, 23 'new_master_ip=s'=>\$new_master_ip, 24 'new_master_port=i'=> \$new_master_port, 25 ); 26 exit &main(); 27 sub main { 28 print "
\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; 29 if ( $command eq "stop" || $command eq "stopssh" ) { 30 my $exit_code = 1; 31 eval { 32 print "Disabling the VIP on old master: $orig_master_host \n"; 33 &stop_vip(); 34 $exit_code = 0; 35 }; 36 if ([email protected]) { 37 warn "Got Error: [email protected]\n"; 38 exit $exit_code; 39 } 40 exit $exit_code; 41 } 42 elsif ( $command eq "start" ) { 43 44 45 my $exit_code = 10; 46 eval { 47 print "Enabling the VIP - $vip on the new master - $new_master_host \n"; 48 &start_vip(); 49 $exit_code = 0; 50 }; 51 if ([email protected]) { 52 warn [email protected]; 53 exit $exit_code; 54 } 55 exit $exit_code; 56 } 57 elsif ( $command eq "status" ) { 58 print "Checking the Status of the script.. OK \n"; 59 #`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`; 60 exit 0; 61 } 62 else { 63 &usage(); 64 exit 1; 65 } 66 } 67 68 69 # A simple system call that enable the VIP on the new master 70 sub start_vip() { 71 `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; 72 } 73 # A simple system call that disable the VIP on the old_master 74 sub stop_vip() { 75 return 0 unless ($ssh_user); 76 `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; 77 } 78 sub usage { 79 print 80 "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"; 81 }
mster_ip_failover