数据库备份与恢复--06---MySQL集群高可用架构之MHA

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


MySQL集群高可用架构之MHA

1.什么是MHA

MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件 ,mha用于解决mysql的单点故障问题
  • 出现故障时,mha能在0~30秒内自动完成故障切换;
  • 并且能在故障切换过程中,最大程度保证数据的一致性
  • 从而达到高可用

MHA的架构图如下:与MMM相似

在这里插入图片描述

2.MHA提供了什么功能:

  1. 可以监控Master节点是否可用
  2. 当Master不可用时,能在多个Slave中选举出新的Master
  3. 提供了主从切换和故障转移功能,MHA会尝试在宕机的Master上保存binlog,在最大程度上保证事务不丢失。但如果是Master所在的服务器已经无法访问,或硬件层面出现了问题,则无法成功保存binlog
  4. MHA可以与半同步复制结合,避免从库之间出现数据不一致的情况
  5. 支持MySQL基于GTID和基于日志点的两种复制方式

3. MHA 的组成

MHA由两部分组成: MHAManager (管理节点) 和 MHA Node (数据库节点),

MHA Node(数据节点)

  • MHA Node 运行在每台 MySQL 服务器上。

MHA Manager(管理节点)

  • MHA Manager 可以单独部署在一台独立的机器上,管理多个 master-slave 集群;也可以部署在一台 slave 节点上。

  • MHA Manager 会定时探测集群中的 master 节点。

  • 当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master, 然后将所有其他的 slave
    重新指向新的master。整个故障转移过程对应用程序完全透明。

4. MHA工作原理

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群必须最少有3台数据库服务器,一主二从,即一台充当Master,台充当备用Master,另一台充当从库。

  1. MHA Node 运行在每台 MySQL 服务器上
  2. MHAManager 会定时探测集群中的master 节点
  3. 当master 出现故障时,它可以自动将最新数据的slave 提升为新的master
  4. 然后将所有其他的slave 重新指向新的master,VIP自动漂移到新的master。

整个故障转移过程对应用程序完全透明。

5.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。

案例1

现模拟进行安装配置,略过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的读负载均衡功能,需要通过第三方工具来实现

  • 15
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值