MHA架构介绍
MHA是Master High Availability的缩写,它是目前MySQL高可用方面的一个相对成熟的解决方案,其核心是使用perl语言编写的一组脚本,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
基于MHA的架构不像MMM那样需要搭建主主复制,只需要搭建基本的主从复制架构即可。因为MHA在主库挂掉时,是在多个从库中选取出一个从库作为新的主库。MHA集群中的各节点彼此之间均需要基于ssh互信通信,以实现远程控制及数据管理功能。
MHA提供了什么功能:
1可以监控Master节点是否可用
2当Master不可用时,能在多个Slave中选举出新的Master
3提供了主从切换和故障转移功能,MHA会尝试在宕机的Master上保存binlog,在最大程度上保证事务不丢失。但如果是Master所在的服务器已经无法访问,或硬件层面出现了问题,则无法成功保存binlog
4MHA可以与半同步复制结合,避免从库之间出现数据不一致的情况
5支持MySQL基于GTID和基于日志点的两种复制方式
MHA故障转移过程:
1尝试使用ssh登录到宕机崩溃的Master节点上保存二进制日志事件(binlog events);
2从多个Slave中识别含有最新更新的Slave,将其作为备选的Master;
3然后基于该Slave同步差异的中继日志(relaylog)到其他的Slave上;
4接着同步从原Master上保存的二进制日志事件(binlog events);
5将备选的Master提升为新的Master;
6使其他的Slave连接新的Master进行复制;
7在新的Master启动vip地址,保证前端请求可以发送到新的Master。
MHA的架构图如下:与MMM相似
现模拟进行安装配置,略过Mysql相关安装步骤
设计为一主双从一监控四机结构
分别为 Master1 Slave1 Slave2 Monitor
1.新增用户所有库中新增同一用户
create user '${replicationUsername}'@'%' identified with mysql_native_password by '${replicationPassword}'
grant replication slave on *.* to '${replicationUsername}'@'%'
flush privileges
2.修改配置文件
Master1:
server_id=101
log_bin=mysql_bin
relay_log=relay_bin
log_slave_updates=on
gtid_mode=ON #开启GTID模式
enforce_gtid_consistency=1
Slave1:
...其他相同...
server_id=102
Slave2:
...其他相同...
server_id=103
3.主从设置
Slave1
change master to master_host=${master1IP},master_port=${masterPort},master_user=${replicationUsername},master_password=${replicationPassword}
start slave
Slave2
change master to master_host=${master1IP},master_port=${masterPort},master_user=${replicationUsername},master_password=${replicationPassword}
start slave
4.配置ssh免密登录
ssh-keygen
ssh-copy-id -i ${otherMachine}
5.节点安装Mha包(mha4mysql-node-0.58-0.el7.centos.noarch.rpm)
yum -y install epel-release
yum -y install perl-DBD-MySQL perl-DBI ncftp //安装依赖
5.1监控节点需安装mha4mysql-manager包
同样用yum安装依赖包 有:perl-Config-Tiny perl-Time-Hires perl-Parallel-ForkManager perl-Log-Dispatch perl-DBD-MySQL ncftp
6.配置MHAMonitor节点
user=${mhaUsername}
password=${mhaPassword}
指定工作目录
manager_workdir=/home/mysql_mha
日志存放路径
manager_log=/home/mysql_mha/manager.log
远程节点工作目录
remote_workdir=/home/mysql_mha
ssh登录用户名
ssh_user=root
repl_user=${replicationUsername}
repl_password=${replicationPassword}
检查时间间隔
ping_interval=1
指定MasterBinlog目录
master_binlog_dir=/var/lib/mysql
指定飘逸脚本
master_ip_failover_script=/usr/bin/master_ip_failover
指定作用二次检查脚本
secondary_check_script=/user/bin/masterha_secondary_check -s ${masterIp} -s ${slave1Ip} -s ${slave2Ip}
配置节点信息
[server1]
hostname=${masterIp}
candidate_master=1#运行选举
[server2]
hostname=${slave1Ip}
candidate_master=1#运行选举
[server3]
hostname=${slave2Ip}
candidate_master=1#运行选举
7.因VIP漂移脚本默认不提供,需手动改
use strict;
use warnings FATAL =>'all';
use Getopt::Long;
my($command,$orig_master_host,$orig_master_ip,$ssh_user,$orig_master_port,$new_master_host,$new_master_ip,$new_master_port,$orig_master_ssh_port,$new_master_ssh_port,$new_master_user,$new_master_password
);
my $vip='${IP}/24'; #设置网段
my $key='1';
#网卡名需根据实际 以下采用 networkName
my $ssh_start_vip ="sudo /sbin/ifconfig newworkName:$key $vip";
my $ssh_stop_vip ="sudo /sbin/ifconfig newworkName:$key down";
my $ssh_Bcast_arp ="sudo /sbin/arping -I bond0 -c 3 -A $vip";
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,
'orig_master_ssh_port=i' =>\$orig_master_ssh_port,
'new_master_ip=s' =>\$new_master_ip,
'new_master_port=1' =>\$new_master_port,
'new_master_ssh_port' =>\$new_master_ssh_port,
'new_master_user' =>\$new_master_user,
'new_master_password' =>\$new_master_password
);
exit &main();
sub main{
$ssh_user=defined $ssh_user?$ssh_user:${sshUserName};
print "\n\nIN SCRIPT TEST====$ssh_user|$ssh_top_vip==$ssh_user|$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;
}elseif($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(){
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \" `
}
sub start_arp(){
`ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp\" `
}
sub usage(){
print
"Usage:master_ip_failover --command=start|stop|stopssh|status --ssh_user=user --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";
}
}
-----------end---------------
chmod 设置权限
新建配置文件中用于监控的用户
create user '${mhaUsername}'@'%' identified with mysql_native_password by '${mhaPassword}'
grant all privileges on *.* to '${mhaUsername}'@'%'
flush privileges
8.检查链路是否正常并启动
masterha_check_ssh --conf=/${配置文件}
masterha_check_repl --conf=/${配置文件}
masterha_manager --conf=/${配置文件}
9.Master手动设置虚拟IP
ifconfig ${networkName}:${key} ${Vip}/24
10.自我测试
11.优缺点
优
使用Perl脚本语言开发并且完全开源,开发者可以根据自己的需求进行二次开发
能够支持基于GTID和基于日志点的复制模式
MHA在进行故障转移时更不易产生数据丢失
在一个监控节点上可以监控多个Replication集群
缺
MHA默认不提供虚拟IP功能,需要自行编写脚本或利用第三方工具来实现虚拟IP的配置
MHA启动后只会对Master进行监控,不会对Slave进行监控,也无法监控复制链路的情况
集群环境需要能够通过ssh免密登录,存在一定的安全隐患
MHA没有提供对Slave的读负载均衡功能,需要通过第三方工具来实现