一、概述
(一)概念
MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件
MHA 的出现就是解决MySQL 单点的问题。
MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。
MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。
(二)组成
MHA manager 管理节点
MHA node 数据节点
(三)特点
自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
目前MHA支持一主多从架构,最少三台服务,即一主两从
(四)工作原理
从宕机崩溃的master 保存二进制日志事件(binlog events);
识别含有最新的更新slave日志
应用差异的中继日志(relay log)到其他的slave
应用从master保存的二进制日志事件
提升一个slave为新的master
使其他的slave连接新的master进行复制
二、构建MHA
先准备4台机器(192.168.115.3;192.168.115.4;192.168.115.5;192.168.115.6)
案例
192.168.115.3 manager(MHA) 主机名:mgt
192.168.115.4 master 主机名:master
192.168.115.5 slave 主机名:slave1
192.168.115.6 slave 主机名:slave2
(一)ssh免密登录
所有节点配置hosts
192.168.115.3 mgt
192.168.115.4 master
192.168.115.5 slave1
192.168.115.6 slave2
vim /etc/hosts
(1)192.168.115.3
ssh-keygen
for i in 4 5 6;do ssh-copy-id root@192.168.115.$i;done
(2)192.168.115.4
ssh-keygen
for i in 3 5 6;do ssh-copy-id root@192.168.115.$i;done
(3)192.168.115.5
ssh-keygen
for i in 4 3 6;do ssh-copy-id root@192.168.115.$i;done
(4)192.168.115.6
ssh-keyge
for i in 4 5 3;do ssh-copy-id root@192.168.115.$i;done
(5)测试
mgt
master
slave1
slave2
(二)主从复制
(1)安装mariadb数据库并启动
yum install -y mariadb mariadb-server
systemctl start mariadb
(2)配置主从复制
1、master
vim /etc/my.cnf
2、slave1
vim /etc/my.cnf
3、slave2
vim /etc/my.cnf
(3)启动
1、master
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.115.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.5' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.6' identified by '123.com';"
mysql -e "show master status";
2、slave1
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.115.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.4' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.6' identified by '123.com';"
mysql -e "change master to master_host='192.168.115.4',master_user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1007;"
3、slave2
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.115.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.4' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.5' identified by '123.com';"
mysql -e "change master to master_host='192.168.115.4',master_user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1007;"
(4)验证
1、master
mysql -e "create database ea;"
mysql -e "show databases;"
2、slave1
mysql -e "show databases;"
3、slave2
mysql -e "show databases;"
(三)MHA安装
(1)所有节点安装perl环境
1、mgt
yum install epel-release -y
yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-CPAN
2、master
yum install epel-release -y
yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-CPAN
3、slave1
yum install epel-release -y
yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-CPAN
4、slave2
yum install epel-release -y
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
1、mgt
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL && make && make install
验证:cd /usr/local/bin 看到脚本就OK
2、master
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL && make && make install
验证:cd /usr/local/bin 看到脚本就OK
3、slave1
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.5
perl Makefile.PL && make && make install
验证:cd /usr/local/bin 看到脚本就OK
4、slave2
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL && make && make install
验证:cd /usr/local/bin 看到脚本就OK
(3)mgt安装manager
tar xf mha4mysql-manager-0.57.tar.gz
cd /root/mha4mysql-manager-0.57
perl Makefile.PL && make && make install
cp samples/scripts/master_ip_failover /usr/local/bin/
cp samples/scripts/master_ip_online_change /usr/local/bin/
脚本说明
master_ip_failover 自动切换时 VIP 管理的脚本
master_ip_online_change 在线切换时 vip 的管理
power_manager 故障发生后关闭主机的脚本
send_report 因故障切换后发送报警的脚本
配置文件建立
mkdir /etc/masterha
vim /etc/masterha/app1.cnf
创建目录
mkdir -p /var/log/masterha/app1
测试MHA
更改 master_ip_failover 自动切换时 VIP 管理的脚本
vim /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.115.200'; #指定vip的地址
my $brdc = '192.168.115.255'; #指定vip的广播地址
my $ifdev = 'ens33'; #指定vip绑定的网卡
my $key = '1'; #指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #代表此变量值为ifconfig ens33:1 192.168.184.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #代表此变量值为ifconfig ens33:1 192.168.184.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
启动命令
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 &
(四)测试
停用master的mariadb服务
切换到slave1上 文章来源:https://www.toymoban.com/news/detail-656686.html
文章来源地址https://www.toymoban.com/news/detail-656686.html
到了这里,关于MySQL高可用MHA的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!