MHA实践操作
1.MHA部署解读:
1.1MHA Manager可以部署在一台slave上.MHA Manager探测集群的node节点,当发现master出现故障的时候,它可以自动将具有最新数据的slave提升为新的master,然后将所有其它的slave导向新的master上.整个故障转移过程对应用程序是透明的。
1.2MHA node运行在每台MySQL服务器上(master/slave/manager),它通过监控具备解析和清理logs功能的脚本来加快故障转移的。
1.3 故障切换时,可以自行判断哪个从库与主库的数据最接近,就切换到上面,可以减少数据的丢失,保证数据的一致性
2.原理介绍
2.1MHA的目的在于维持MySQL Replication中Master库的高可用性,其最大特点是可以修复多个Slave之间的差异日志,最终使所有Slave保持数据一致,然后从中选择一个充当新的Master,并将其它Slave指向它。
2.2当master出现故障时,可以通过对比slave之间I/O thread 读取主库binlog的position号,选取最接近的slave做为备选主库(备胎)。其它的从库可以通过与备选主库对比生成差异的中继日志。在备选主库上应用从原来master保存的binlog,同时将备选主库提升为master。最后在其它slave上应用相应的差异中继日志并开始从新的master开始复制。
关于半同步复制原理各位自己进行查阅。(不是必须)
3.关键配置文件
cat /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=123456
ping_interval=1
repl_password=123456
repl_user=repl
ssh_user=root
user=manage #比较关键配置,在check_ssh的时候注意这个用户
[server2]
candidate_master=1
check_repl_delay=0
hostname=10.1.1.205
master_binlog_dir=/usr/local/mysql/data/
[server3]
hostname=10.1.1.202
master_binlog_dir=/usr/local/mysql/data/
port=3306
4.通过脚本的方式管理VIP
这里是修改/usr/local/bin/master_ip_failover,还需要手动在master服务器上绑定一个vip
[root@Master_node ~]
# ifconfig eth1:0 10.1.1.254/24
5..关键切换脚本
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.1.1.254/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth1:$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";
}
6.结果:
----- Failover Report -----
app1: MySQL Master failover 10.1.1.98 to 10.1.1.205 succeeded
Master 10.1.1.98 is down!
Check MHA Manager logs at slave02:/var/log/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 10.1.1.98.
The latest slave 10.1.1.205(10.1.1.205:3306) has all relay logs for recovery.
Selected 10.1.1.205 as a new master.
10.1.1.205: OK: Applying all logs succeeded.
10.1.1.205: OK: Activated master IP address.
10.1.1.202: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.1.1.202: OK: Applying all logs succeeded. Slave started, replicating from 10.1.1.205.
10.1.1.205: Resetting slave info succeeded.
Master failover to 10.1.1.205(10.1.1.205:3306) completed successfully.
参考:
MySQL高可用架构-MHA环境部署记录 - 散尽浮华 - 博客园
http://www.cnblogs.com/kevingrace/p/5662839.html