Last active
May 18, 2016 13:39
-
-
Save nepaul/d6b08a94004be8609d72a5fb2be0022b to your computer and use it in GitHub Desktop.
MySQL HA MHA Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! Configuration File for keepalived | |
global_defs { | |
router_id LVS_DEVEL | |
} | |
vrrp_instance VI_1 { | |
state MASTER #candidate master则写BACKUP | |
interface eth2 | |
virtual_router_id 51 #同一个集群这个东西是一样的 | |
priority 100 #从254到1,master > candidate master | |
advert_int 1 | |
nopreempt | |
authentication { #认证账号,同一个集群一样 | |
auth_type PASS | |
auth_pass 1111 | |
} | |
virtual_ipaddress { | |
10.20.78.11 # 虚拟ip, 主挂了,飘给备 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.20.78.11'; | |
my $ssh_start_vip = "/etc/init.d/keepalived start"; | |
my $ssh_stop_vip = "/etc/init.d/keepalived stop"; | |
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"; | |
#`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() { | |
return 0 unless ($ssh_user); | |
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; | |
} | |
sub usage { | |
"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"; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[server default] | |
# 访问三台 MySQL 的用户名和密码, | |
# mha-manager 需要做一些诸如 Start Slave 等主从同步的操作 | |
user=repl_username | |
password=repl_password | |
ssh_user=root | |
# working directory on the manager | |
manager_workdir=/usr/local/mha | |
manager_workdir=/var/log/masterha | |
# working directory on MySQL servers | |
# 同步的用户名和密码 | |
remote_workdir=/usr/local/mha | |
repl_user=repl_username | |
repl_password=repl_password | |
# master 失效转移的处理脚本 | |
master_ip_failover_script=/usr/local/mha/master_ip_failover | |
[server1] | |
hostname=10.20.78.241 | |
[server2] | |
hostname=10.20.78.243 | |
candidate_master=1 # master 宕掉后只选择该服务为新的 master | |
[server3] | |
hostname=10.20.78.245 | |
no_master=1 # 该 MySQL 永远不做 master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment