MHA高可用搭建

教程6个月前发布 spoony
4 0 0

简介

MHA(Master HA)是一款开源的MySQL的高可用程序,它为MySQL主从复制架构提供了automating master failover 功能。MHA在监控到master节点故障时,会提升其中拥有最新数据的slave节点成为新的master节点,在此期间,MHA会通过与其它从节点获取额外信息来避免一致性方面的问题。MHA还提供了master节点的在线切换功能,即按需切换master/slave节点。

MHA 是由日本人 yoshinorim(原就职于DeNA现就职于FaceBook)开发的比较成熟的 MySQL 高可用方案。MHA 能够在30秒内实现故障切换,并能在故障切换中,最大可能的保证数据一致性。目前淘宝也正在开发相似产品 TMHA, 目前已支持一主一从。

工作原理

MHA高可用搭建
mha.jpg

当Master出现故障时,通过对比slave之间I/O线程读取master binlog的位置,选取最接近的slave做为latestslave。 其它slave通过与latest slave对比生成差异中继日志。在latest slave上应用从master保存的binlog,同时将latest slave提升为master。最后在其它slave上应用相应的差异中继日志并开始从新的master开始复制。
在MHA实现Master故障切换过程中,MHA Node会试图访问故障的master(通过SSH),如果可以访问(不是硬件故障,比如InnoDB数据文件损坏等),会保存二进制文件,以最大程度保 证数据不丢失。MHA和半同步复制一起使用会大大降低数据丢失的危险。

MHA工作原理总结为以下几条:
(1) 从宕机崩溃的 master 保存二进制日志事件(binlog events);
(2) 识别含有最新更新的 slave ;
(3) 应用差异的中继日志(relay log) 到其他 slave ;
(4) 应用从 master 保存的二进制日志事件(binlog events);
(5) 提升一个 slave 为新 master ;
(6) 使用其他的 slave 连接新的 master 进行复制。

1.准备环境

主机名 IP地址 角色
db01 172.16.1.51 master
db02 172.16.1.52 slave1、candidate_master
db03 172.16.1.53 slave2、manager

2.安装数据库

[root@db01 ~]# yum install mariadb-server -y
[root@db02 ~]# yum install mariadb-server -y
[root@db03 ~]# yum install mariadb-server -y

3.修改数据库配置文件

#master节点配置
[root@db01 ~]# vim /etc/my.cnf
skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 1               #复制集群中的各节点的id均必须唯一
log-bin = mysql-bin         #开启二进制日志
relay-log = relay-log       #开启中继日志

#slave节点配置
[root@db02 ~]# vim /etc/my.cnf
skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 2             
log-bin = mysql-bin       
relay-log = relay-log     
#read_only = ON          #不在配置文件中限定只读,但是要记得在slave上限制只读
relay_log_purge = 0       #禁用自动清空不再需要中继日志
log_slave_updates = 1     #使得更新的数据写进二进制日志中

[root@db03 ~]# vim /etc/my.cnf
skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 3            
log-bin = mysql-bin       
relay-log = relay-log     
#read_only = ON         
relay_log_purge = 0       
log_slave_updates = 1     


#配置完成后,启动数据库(3个节点)
[root@db01 ~]# systemctl restart mariadb

4.配置一主多从复制架构

#master节点
MariaDB [(none)]> grant replication slave on *.* to 'slave'@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all on *.* to 'mhaadmin'@'172.16.1.%' identified by 'mhapass';   #很重要
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      397 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


#slave节点(2个)
MariaDB [(none)]> set global read_only=1;       #设置只读(防止意外被写数据,很重要)
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> change master to
    -> master_host='172.16.1.51',
    -> master_user='slave',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000003',
    -> master_log_pos=397;
Query OK, 0 rows affected (0.10 sec)

change master to master_host='172.16.1.51',master_user='slave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=397;

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.51
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 397
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 397
              Relay_Log_Space: 817
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

5.MHA基本环境准备

1)配置三台机器的ssh互信(三台都要操作)

ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.1.51
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.1.52
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.1.53
#或者使用免密、免验证登录
sshpass -p1  ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@172.16.1.51

2)安装依赖

yum install -y perl perl-DBI perl-DBD-MySQL perl-IO-Socket-SSL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

3)安装MHA

#三个节点都需安装 mha4mysql-node
yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm

#manager(db03)节点需要在安装 mha4mysql-manager
yum install -y mha4mysql-manager-0.56-0.el6.noarch.rpm

6.配置MHA(在manager节点上操作,即db03)

1)创建目录

mkdir -p /etc/mha/scripts
mkdir -p /var/log/mha/app1

2)配置mha配置文件

[root@db03 ~]# vim /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager.log       #设置manager的日志
manager_workdir=/var/log/mha/app1               #设置manager的工作目录
master_binlog_dir=/var/lib/mysql                #设置master 保存binlog的位置
master_ip_failover_script= /etc/mha/scripts/master_ip_failover    #设置自动failover时候的切换脚本
master_ip_online_change_script=/etc/mha/scripts/master_ip_online_change 
                                                                       #设置手动切换时候的切换脚本
user=mhaadmin
password=mhapass
ssh_user=root
repl_password=123456
repl_user=slave                     #设置复制环境中的复制用户名
ping_interval=1                     #发送ping包的时间间隔

[server1]
hostname=172.16.1.51
port=3306

[server2]
hostname=172.16.1.52
port=3306
candidate_master=1              #设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为                                主库
check_repl_delay=0              #默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master

[server3]
hostname=172.16.1.53
port=3306

7.配置VIP(manager节点)

1)自动failover脚本 “/etc/mha/scripts/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 = '172.16.1.59/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth1:$key down";

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 "nnIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===nn";

    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 "`;
}
sub stop_vip() {
     return 0  unless  ($ssh_user);
    `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=portn";
}

2)手动 failover “/etc/mha/scripts/master_ip_online_change”

#!/bin/bash
source /root/.bash_profile
vip=`echo '172.16.1.59/24'`  #设置VIP
key=`echo '1'`

command=`echo "$1" | awk -F = '{print $2}'`
orig_master_host=`echo "$2" | awk -F = '{print $2}'`
new_master_host=`echo "$7" | awk -F = '{print $2}'`
orig_master_ssh_user=`echo "${12}" | awk -F = '{print $2}'`
new_master_ssh_user=`echo "${13}" | awk -F = '{print $2}'`

#要求服务的网卡识别名一样,都为eth1(这里是)
stop_vip=`echo "ssh root@$orig_master_host /usr/sbin/ifconfig eth1:$key down"`
start_vip=`echo "ssh root@$new_master_host /usr/sbin/ifconfig eth1:$key $vip"`

if [ $command = 'stop' ]
     then
      echo -e "nnn****************************n"
       echo -e "Disabled thi VIP - $vip on old master: $orig_master_host n"
       $stop_vip
       if [ $? -eq 0 ]
         then
        echo "Disabled the VIP successfully"
          else
        echo "Disabled the VIP failed"
        fi
        echo -e "***************************nnn"
      fi

if [ $command = 'start' -o $command = 'status' ]
      then
        echo -e "nnn*************************n"
        echo -e "Enabling the VIP - $vip on new master: $new_master_host n"
        $start_vip
        if [ $? -eq 0 ]
          then
        echo "Enabled the VIP successfully"
          else
        echo "Enabled the VIP failed"
       fi
       echo -e "***************************nnn"
fi

3)将脚本赋予可执行权限

chmod +x /etc/mha/scripts/master_ip_failover 
chmod +x /etc/mha/scripts/master_ip_online_change

8.通过 masterha_check_ssh 验证 ssh 信任登录是否成功

[root@db03 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Wed Apr 16 23:17:58 2020 -  All SSH connection tests passed successfully.       #表示所有都成功

9.通过 masterha_check_repl 验证 mysql 主从复制是否成功(下面输出表示测试通过)

[root@db03 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
IN SCRIPT TEST====/sbin/ifconfig eth1:1 down==/sbin/ifconfig eth1:1 172.16.1.59/24===
Checking the Status of the script.. OK 
Wed Apr 16 27:15:58 2020 -   OK.
Wed Apr 16 27:15:58 2020 -  shutdown_script is not defined.
Wed Apr 16 27:15:58 2020 -  Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
注意:
#报错一:
Wed Apr  1 17:47:34 2020 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 172.16.1.53(172.16.1.53:3306) :1130:Host '172.16.1.53' is not allowed to connect to this MariaDB server, but this is not a MySQL crash. Check MySQL server settings.
解决方案:
去master节点,给mhaadmin重新授权
grant all on *.* to 'mhaadmin'@'172.16.1.%' identified by 'mhapass';

#报错二:
Wed Apr  1 17:48:05 2020 - [error][/usr/share/perl5/vendor_perl/MHA/Server.pm, ln393] 172.16.1.52(172.16.1.52:3306): User slave does not exist or does not have REPLICATION SLAVE privilege! Other slaves can not start replication from this host.
解决方案:
去master节点,给slave重新授权
grant replication slave on *.* to 'slave'@'172.16.1.%' identified by '123456';

#报错三:
Wed Apr  1 22:32:16 2020 - [info]   /etc/mha/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=172.16.1.51 --orig_master_ip=172.16.1.51 --orig_master_port=3306 
Wed Apr  1 22:32:16 2020 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln424] Error happened on checking configurations. Can't exec "/etc/mha/scripts/master_ip_failover": Permission denied at /usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm line 68.
解决方案:
可以看到,上面提示脚本文件/etc/mha/scripts/master_ip_failover权限不足,
chmod +x /etc/mha/scripts/*

10.启动MHA(注意:MHA监控脚本切换一次就会退出,需要再次启动)

1)先在master上绑定vip,(只需要在master绑定这一次,以后会自动切换)

[root@db01 ~]# /usr/sbin/ifconfig eth1:1 172.16.1.59/24

2)然后通过 masterha_manager 启动 MHA 监控

#启动mha
[root@db03 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover  /var/log/mha/app1/manager.log 2>&1 &

#关闭mha
[root@db03 ~]# masterha_stop --conf=/etc/masterha/app1.cnf
#检查MHA的启动状态
[root@db03 ~]# tailf /var/log/mha/app1/manager.log

#如果最后一行是如下,表明启动成功
Ping(SELECT) succeeded, waiting until MySQL doesn’t respond..

3)检查集群状态

[root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
mha (pid:7598) is running(0:PING_OK), master:172.16.1.51

#注:上面的信息中“mha (pid:7598) is running(0:PING_OK)”表示MHA服务运行OK,否则, 则会显示为类似“mha is stopped(1:NOT_RUNNING).”

11.切换测试

1)自动切换(模拟master宕了)(实验测试当物理故障时,没有指定shutdown_script是没用的,不切换)

#第一:要实现自动 Failover,必须先启动 MHA Manager,否则无法自动切换

A、杀掉主库 mysql 进程,模拟主库发生故障,进行自动 failover 操作。

B、看 MHA 切换日志,了解整个切换过程


#第二:从上面的输出可以看出整个 MHA 的切换过程,共包括以下的步骤:

1).配置文件检查阶段,这个阶段会检查整个集群配置文件配置;

2).宕机的 master 处理,这个阶段包括虚拟 ip 摘除操作,主机关机操作(由于没有定义power_manager脚本,不会关机);

3).复制 dead maste 和最新 slave 相差的 relay log,并保存到 MHA Manger 具体的目录下;

4).识别含有最新更新的 slave;

5).应用从 master 保存的二进制日志事件(binlog events)(这点信息对于将故障master修复后加入集群很重要);

6).提升一个 slave 为新的 master 进行复制;

7).使其他的 slave 连接新的 master 进行复制。


#第三:切换完成后,关注如下变化:

1、vip 自动从原来的 master 切换到新的 master,同时,manager 节点的监控进程自动退出。

2、在日志目录(/var/log/mha/app1)产生一个 app1.failover.complete 文件

3、/etc/mha/app1.cnf 配置文件中原来老的 master 配置被删除。

2)在线切换(用于硬件升级)(很好使)

###第一:注意点:前提,mha监控没有运行的情况下,才能进行

A、老master上的vip已经正确生效了

B、各个salve节点数据库的IO线程和SQL线程都正常(即YES)
    show slave statusG;

C、MHA脚本不能运行,若已处于监控状态,需要停掉它

masterha_stop --conf=/etc/mha/app1.cnf
#若是mha监控进程在运行,会报如下错误

Sat May 19 03:40:00 2018 - [error][/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm, ln143] Getting advisory lock failed on the current master. MHA Monitor runs on the current master. Stop MHA Manager/Monitor and try again.
Sat May 19 03:40:00 2018 - [error][/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/bin/masterha_master_switch line 53.


###第二:执行切换

#需要填写新的master的IP

masterha_master_switch --conf=/etc/mha/app1.cnf --master_state=alive --new_master_host=192.168.1.57 --orig_master_is_new_slave --running_updates_limit=10000 --interactive=0


###第三:MHA 在线切换基本步骤:

a、检测 MHA 配置置及确认当前 master;

b、决定新的 master;

c、阻塞写入到当前 master;

d、等待所有从服务器与现有 master 完成同步;

e、在新 master 授予写权限,以及并行切换从库;

f、重置原 master 为新 master 的 slave;

g、在线切换不会删除/etc/mha/app1.cnf 配置文件中原来老的 master 配置;

12.如何将故障节点重新加入集群

通常情况下自动切换以后,原 master 可能已经废弃掉,待原 master 主机修复后,如果数据完整的情况下,可能想把原来 master 重新作为新主库的 slave,这时我们可以借助当时自动切换时刻的 MHA 日志来完成对原 master 的修复。(若是下面第二步:出现的是第二种,可以不用借助日志,定位binlog日志点,可以自动定位)

1)修改 manager 配置文件(只针对自动切换的,在线切换不会删除配置)

#将如下内容添加到/etc/mha/app1.conf 中
[server1]
hostname=172.16.1.51
port=3306
candidate_master=1
master_binlog_dir="/var/lib/mysql"

2)修复老的 master,然后设置为 slave

从自动切换时刻的 MHA 日志上可以发现类似如下信息:意思是说,如果 Master 主机修复好了,可以在修复好后的 Master 上执行 CHANGE MASTER操作,作为新的 slave 库。

cat /var/log/mha/app1/manager.log
Sat May 27 14:59:17 2017 -  All other slaves should start replication from here. Statement
should be: CHANGE MASTER TO MASTER_HOST='172.16.213.232', MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=120, MASTER_USER='repl_user',
MASTER_PASSWORD='xxx';
#或则
Sat May 19 05:00:48 2018 -  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.58', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl_user', MASTER_PASSWORD='xxx';

在老的 master 执行如下命令:(具体执行哪条,根据上面输出来确定,区别是一个有日志的定位,一个是自动定位)

mysql> change master to master_host='192.168.1.57',master_user='repl_user',master_password='123456',master_log_file='mysql-bin.0000124',master_log_pos=234;
#或则
mysql> change master to master_host='192.168.1.57',master_user='repl_user',master_password='123456',MASTER_AUTO_POSITION=1;
mysql> start slave;
mysql> show slave statusG;

这样,数据就开始同步到老的 master 上了。此时老的 master 已经重新加入集群,变成 mha集群中的一个 slave 角色了。

3)在 manger 节点上重新启动监控进程

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover  /var/log/mha/app1/manager.log 2>&1 &

文章来源于互联网:MHA高可用搭建

相关推荐: MongoDB 教程

MongoDB 教程 MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的…

© 版权声明

相关文章

暂无评论

暂无评论...