Mariadb高可用MHA


前言

高可用MySQL集群是指部署了多个MySQL实例的集群,可以在节点出现故障的情况下,自动切换到另一个可用节点,保证系统的高可用性和可靠性。

一、概述

(一)、概念

MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。

MHA 的出现就是解决MySQL 单点的问题。

MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。

MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。

(二)、组成

MHA manager 管理节点

MHA node 数据节点 每个节点上都需要安装

(三)、特点

自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失。

使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

目前MHA支持一主多从架构,最少三台服务,即一主两从。

(四)、工作原理

  1. 从宕机崩溃的master 保存二进制日志事件(binlog events)
  2. 识别含有最新的更新slave日志
  3. 应用差异的中继日志(relay log)到其他的slave
  4. 应用从master保存的二进制日志事件
  5. 提升一个slave为新的master
  6. 使其他的slave连接新的master进行复制

二、案例

192.168.238.134 manager(MHA) 即mgt

192.168.238.135 master master

192.168.238.136 slave slave1

192.168.238.137 slave slave2

(一)、构建MHA

1.所有节点ssh免密登录

192.168.238.134

ssh-keygen

for i in 135 136 137;do ssh-copy-id root@192.168.238.$i;done

yes确认指纹,和输入对应主机的密码

192.168.238.135

ssh-keygen

for i in 134 136 137;do ssh-copy-id root@192.168.238.$i;done

yes确认指纹,和输入对应主机的密码

192.168.238.136

ssh-keygen

for i in 134 135 137;do ssh-copy-id root@192.168.238.$i;done

yes确认指纹,和输入对应主机的密码

192.168.238.135

ssh-keygen

for i in 134 135 136;do ssh-copy-id root@192.168.238.$i;done

yes确认指纹,和输入对应主机的密码

进行验证看是否免密成功逐一登录登出,确保后续脚本正常运行

以192.168.238.134为例

2、MySQL主从复制

yum install -y mariadb mariadb-server安装至master、slave1、slave2

192.168.238.135-master

vim /etc/my.cnf

server-id=10

log-bin=master-bin

log-slave-updates=true

systemctl start mariadb

除了在下图MySQL中登录授权也可以在外部授权

mysql -e "grant replication slave on *.* to 'myslave'@'192.168.238.% identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.% identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.134 identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.136 identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.137 identified by '123.com';"

查看master状态

192.168.238.136

vim /etc/my.cnf

server-id=11

log-bin=master-bin

relay-log=relay-log-bin

relay-log-index=relay-log-bin.index

systemctl start mariadb

mysql -e "grant replication slave on *.* to 'myslave'@'192.168.238.% identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.% identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.134 identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.136 identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.137 identified by '123.com';"

mysql -e "stop slave;"

mysql -e "change master to master_host ='192.168.238.135', master_user='myslave',master _password='123.com',master_log_file='master-bin.000003',master_log_pos=1163;"

mysql -e "start slave;"

查看slave状态

192.168.238.137

vim /etc/my.cnf

server-id=12

log-bin=master-bin

relay-log=relay-log-bin

relay-log-index=relay-log-bin.index

systemctl start mariadb

mysql -e "grant replication slave on *.* to 'myslave'@'192.168.238.% identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.% identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.134 identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.136 identified by '123.com';"

mysql -e "grant all privileges on *.* to 'mha'@'192.168.238.137 identified by '123.com';"

mysql -e "stop slave;"

mysql -e "change master to master_host ='192.168.238.135', master_user='myslave',master _password='123.com',master_log_file='master-bin.000003',master_log_pos=1163;"

mysql -e "start slave;"

查看slave状态

验证主从关系

master create database class

slave1

slave2

(二)、MHA安装

基于主从复制模式之上

1.所有节点安装perl环境

yum install epel-release -y(一直更新到版本epel-release.noarch 0:7-14)

yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-CPAN

2.所有节点安装node

tar xf mha4mysql-node-0.57.tar.gz

cd /root/mha4mysql-noder-0.57

perl Makefile.PL && make && make install

验证

cd /usr/local/bin

看到脚本就OK

3.manager

tar xf mha4mysql-manager-0.57.tar.gz

cd /root/mha4mysql-manager-0.57

perl Makefile.PL && make && make install

cp sample/scripts/master_ip_failover /usr/local/bin/

cp sample/scripts/master_ip_online_change /usr/local/bin/

4.脚本说明

master_ip_failover 自动切换时 VIP 管理的脚本

master_ip_online_change 在线切换时 vip 的管理

power_manager 故障发生后关闭主机的脚本

send_report 因故障切换后发送报警的脚本

5.配置文件建立

mkdir /etc/masterha -p

vim /etc/masterha/app1.cnf

mkdir /var/log/masterha/app1 -p

6.测试MHA

vim /usr/local/bin/master_ip_failover

chown +x /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 = '192.168.238.200';								#指定vip的地址
my $brdc = '192.168.238.255';								#指定vip的广播地址
my $ifdev = 'ens33';										#指定vip绑定的网卡
my $key = '1';												#指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";		#代表此变量值为ifconfig ens33:1 192.168.238.100
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";		#代表此变量值为ifconfig ens33:1 192.168.238.200 down
my $exit_code = 0;											#指定退出状态码为0
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
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 \"`;
}
## A simple system call that disable the VIP on the old_master
sub stop_vip() {
`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";
}

masterha_check_ssh --conf=/etc/masterha/app1.cnf

masterha_check_repl --conf=/etc/masterha/app1.cnf

7.启动命令

nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &

启动时masterIP异常

需要工程手动配置VIP

(三)、测试

停用master的mariadb服务

去slave1中查看IP


总结

今天的内容要在免密和主从复制的基础之上才能运行,搭建基础就考验小伙伴们的基本功底了,在学新知识的同时又复习了学过的知识。

相关推荐
看山还是山,看水还是。17 分钟前
MySQL 管理
数据库·笔记·mysql·adb
fishmemory7sec23 分钟前
Koa2项目实战2(路由管理、项目结构优化)
数据库·mongodb·koa
momo小菜pa33 分钟前
【MySQL 09】表的内外连接
数据库·mysql
Jasonakeke42 分钟前
【重学 MySQL】四十九、阿里 MySQL 命名规范及 MySQL8 DDL 的原子化
数据库·mysql
程序猿小D43 分钟前
第二百六十九节 JPA教程 - JPA查询OrderBy两个属性示例
java·开发语言·数据库·windows·jpa
小宇成长录1 小时前
Mysql:数据库和表增删查改基本语句
数据库·mysql·数据库备份
团儿.2 小时前
解锁MySQL高可用新境界:深入探索MHA架构的无限魅力与实战部署
数据库·mysql·架构·mysql之mha架构
程序猿小D2 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
权^3 小时前
MySQL--聚合查询、联合查询、子查询、合并查询(上万字超详解!!!)
大数据·数据库·学习·mysql
Code成立3 小时前
1、深入理解Redis线程模型
数据库·redis·bootstrap