mariadb高可用MHA

一、概念
    MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。
    MHA 的出现就是解决MySQL 单点的问题。
    MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。
    MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。

二、特点
    自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
    使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
    目前MHA支持一主多从架构,最少三台服务,即一主两从

三、工作原理
    从宕机崩溃的master 保存二进制日志事件(binlog events);
    识别含有最新的更新slave日志
    应用差异的中继日志(relay log)到其他的slave
    应用从master保存的二进制日志事件
    提升一个slave为新的master
    使其他的slave连接新的master进行复制

四、组成
    MHA manager
        管理节点
    MHA node
        数据节点
        每个节点上都需要安装

一、构建MHA

192.168.115.150 manager(MHA) 更改主机名为:mgt 

192.168.115.151master  更改主机名为:master
192.168.115.152 slave  更改主机名为:slave
192.168.115.153 slave  更改主机名为:slave1

151 152 153都需要安装node

所有节点配置hosts    #  vim /etc/hosts
    192.168.115.150  mgt
    192.168.115.151    master
    192.168.115.152    slave
    192.168.115.153    slave1

 二、设置ssh免密登录

192.168.115.150
    ssh-keygen
    for i in 151 152 153;do ssh-copy-id root@192.168.115.$i;done
192.168.115.151
    ssh-keygen
    for i in 150 152 153;do ssh-copy-id root@192.168.115.$i;done
192.168.115.152
    ssh-keygen
    for i in 150 151 153;do ssh-copy-id root@192.168.115.$i;done
192.168.115.153
    ssh-keygen
    for i in 150 151 152;do ssh-copy-id root@192.168.115.$i;done

在150上测试
    for i in 151 152 153;do ssh 192.168.115.$i hostname;done

免密登录成功

三、安装mariadb数据库并启动
    for i in 151 152 153;do ssh 192.168.115.$i yum install -y mariadb mariadb-server mariadb;done

四、配置主从复制

master  #在master主机上
	vim  /etc/my.cnf  #配置主服务
	[mysqld]
server-id = 20
log-bin = master-bin
log-slave-updates = true   #保存退出
	systemctl restart mariadb   #启动服务
      #mysql -e 执行外部命令创建授权用户
	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.150' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.152' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.153' identified by '123.com';"

	mysql -e "show master status"; #查看
slave #在slave从服务器上
vim /etc/my.cnf
	server-id=30
log-bin=master-bin
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index #保存退出

	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.150' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.152' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.153' identified by '123.com';"
	mysql -e "change master to master_host='192.168.115.151',master_
user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1166;"

slave1  #在slave1从服务器上
	server-id=40
log-bin=master-bin
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index  #保存退出

	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.150' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.152' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.115.153' identified by '123.com';"
	mysql -e "change master to master_host='192.168.115.151',master_
user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1166;"

验证
    master
        mysql -uroot -p980926 -e "create database yangyang;"


    slave
        mysql -uroot -p123.com -e "show databases;"

slave
        mysql -uroot -p1234 -e "show databases;"

主从服务器配置完成

五、MHA安装

所有节点安装perl环境
    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

所有节点安装node
    tar xf mha4mysql-node-0.57.tar.gz
    cd mha4mysql-node-0.57
    perl Makefile.PL && make && make install

 验证
    cd /usr/local/bin
    看到脚本就OK

mgt主机安装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/
    脚本说明
        master_ip_failover    自动切换时 VIP 管理的脚本

  #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";
}


        master_ip_online_change    在线切换时 vip 的管理
        power_manager    故障发生后关闭主机的脚本
        send_report    因故障切换后发送报警的脚本
    配置文件建立
        mkdir /etc/masterha
        vim /etc/masterha/app1.cnf

  #app1.cnf 文件内容
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/var/lib/mysql/
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=123.com
ping_interval=1
remote_workdir=/tmp
repl_password=123.com
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.115.152 -s 192.168.115.153
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.115.151  #主服务器
port=3306

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.115.152   #从服务器1
port=3306

[server3]
hostname=192.168.115.153   #从服务器2
port=3306


    mkdir /var/log/masterha/app1


    测试MHA
        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服务即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值