linux MySQL基于MHA的高可用架构

**

MHA简介:

**

MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于 Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在 0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其 他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器 硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最 新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。另外对于想快速搭建的可以参考:MHA快速搭建

我们自己使用其实也可以使用1主1从,但是master主机宕机后无法切换,以及无法补全binlog。master的mysqld进程crash后,还是可以切换成功,以及补全binlog的。

官方介绍:https://code.google.com/p/mysql-master-ha/

**

1.环境准备

**

四台虚拟机

bobo为监控MHA端

server3为主机

server2 与server4为slave

server4 为备用主机
这里写图片描述

每个主机都安装相关rpm包
监控段自己安装yum install -y mha4mysql-manager-0.56-0.el6.noarch.rpm
其他不用 安装node
这里写图片描述

**

2.ssh免密登陆

**
三台主机配置免密码ssh登陆 不免密可登陆监控主机 而监控主机可以登陆其他主机
其他主机可以互相登陆

ssh-keygen -t rsa

ssh-copy-id  -i  /root/.ssh/id_rsa.pub  root@172.25.15.12####  想要无密码登陆的主机

其他相互无密码登陆即可 监控主机不需要被其他主机登陆

这里写图片描述
**

3.MHA控制脚本

**
配置MHA脚本
在rpm包中已经下载好脚本与rpm包 复制到目录下即可
这里写图片描述

**

4.编辑MHA配置文件

**

cd /etc/masterha/

vim app.cnf

[server default]
manager_workdir=/etc/masterha
manager_log=/etc/masterha/mha.log
master_binlog_dir=/var/lib/mysql

master_ip_failover_script=/etc/masterha/master_ip_failover
master_ip_online_change_script=/etc/masterha/master_ip_online_change
password=Hh~802119323 监控用户密码
user=root###########  监控用户
ping_interval=1
remote_workdir=/tmp
repl_password=Hh~802119323   复制用密码
repl_user=bobo
report_script=/usr/local/send_report
secondary_check_script=/usr/bin/masterha_secondary_check -s 172.25.15.11 -s 172.25.15.13
shutdown_script=""
ssh_user=root

[server1] #### 注意server排序 
hostname=172.25.15.11
port=3306
[server2]
hostname=172.25.15.12
port=3306
[server3]
hostname=172.25.15.13
port=3306
candidate_master=1
check_repl_delay=0

**

5.主机 slave的mysql设置

**
两台slave主机设置readonly

 mysql -uroot  -pHh~802119323 -e 'set global read_only=1'

三台主机都需要创建监控用户

grant all privileges on *.* to 'root'@'172.25.15.%' identified  by 'Hh~802119323';

一台slave(备用主机) 需要设置复制用户 和主机要为统一用户
关于slave的配置
https://blog.csdn.net/iaMay_____/article/details/81489860

 grant replication slave on *.* to 'bobo'@'192.168.0.%' identified by 'Hh~802119323';

配置两台slave如下图即可
这里写图片描述

**

6.MHA检查SSH配置

**
检查MHA Manger到所有MHA Node的SSH连接状态:
这里写图片描述

**

7.检查整个复制环境状况

**
通过masterha_check_repl脚本查看整个集群的状态

 masterha_check_repl --conf=/etc/masterha/app.cnf
Thu Aug  9 12:41:16 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Aug  9 12:41:16 2018 - [info] Reading application default configuration from /etc/masterha/app.cnf..
Thu Aug  9 12:41:16 2018 - [info] Reading server configuration from /etc/masterha/app.cnf..
Thu Aug  9 12:41:16 2018 - [info] MHA::MasterMonitor version 0.56.
Thu Aug  9 12:41:16 2018 - [info] GTID failover mode = 0
Thu Aug  9 12:41:16 2018 - [info] Dead Servers:
Thu Aug  9 12:41:16 2018 - [info] Alive Servers:
Thu Aug  9 12:41:16 2018 - [info]   172.25.15.11(172.25.15.11:3306)
Thu Aug  9 12:41:16 2018 - [info]   172.25.15.12(172.25.15.12:3306)
Thu Aug  9 12:41:16 2018 - [info]   172.25.15.13(172.25.15.13:3306)
Thu Aug  9 12:41:16 2018 - [info] Alive Slaves:
Thu Aug  9 12:41:16 2018 - [info]   172.25.15.11(172.25.15.11:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Aug  9 12:41:16 2018 - [info]     Replicating from 172.25.15.12(172.25.15.12:3306)
Thu Aug  9 12:41:16 2018 - [info]   172.25.15.13(172.25.15.13:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Aug  9 12:41:16 2018 - [info]     Replicating from 172.25.15.12(172.25.15.12:3306)
Thu Aug  9 12:41:16 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Aug  9 12:41:16 2018 - [info] Current Alive Master: 172.25.15.12(172.25.15.12:3306)
Thu Aug  9 12:41:16 2018 - [info] Checking slave configurations..
Thu Aug  9 12:41:16 2018 - [info]  read_only=1 is not set on slave 172.25.15.11(172.25.15.11:3306).
Thu Aug  9 12:41:16 2018 - [warning]  relay_log_purge=0 is not set on slave 172.25.15.11(172.25.15.11:3306).
Thu Aug  9 12:41:16 2018 - [info]  read_only=1 is not set on slave 172.25.15.13(172.25.15.13:3306).
Thu Aug  9 12:41:16 2018 - [warning]  relay_log_purge=0 is not set on slave 172.25.15.13(172.25.15.13:3306).
Thu Aug  9 12:41:16 2018 - [info] Checking replication filtering settings..
Thu Aug  9 12:41:16 2018 - [info]  binlog_do_db= , binlog_ignore_db= 
Thu Aug  9 12:41:16 2018 - [info]  Replication filtering check ok.
Thu Aug  9 12:41:16 2018 - [info] GTID (with auto-pos) is not supported
Thu Aug  9 12:41:16 2018 - [info] Starting SSH connection tests..
Thu Aug  9 12:41:17 2018 - [info] All SSH connection tests passed successfully.
Thu Aug  9 12:41:17 2018 - [info] Checking MHA Node version..
Thu Aug  9 12:41:18 2018 - [info]  Version check ok.
Thu Aug  9 12:41:18 2018 - [info] Checking SSH publickey authentication settings on the current master..
Thu Aug  9 12:41:18 2018 - [info] HealthCheck: SSH to 172.25.15.12 is reachable.
Thu Aug  9 12:41:18 2018 - [info] Master MHA Node version is 0.56.
Thu Aug  9 12:41:18 2018 - [info] Checking recovery script configurations on 172.25.15.12(172.25.15.12:3306)..
Thu Aug  9 12:41:18 2018 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000002 
Thu Aug  9 12:41:18 2018 - [info]   Connecting to root@172.25.15.12(172.25.15.12:22).. 
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to mysql-bin.000002
Thu Aug  9 12:41:18 2018 - [info] Binlog setting check done.
Thu Aug  9 12:41:18 2018 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Thu Aug  9 12:41:18 2018 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=172.25.15.11 --slave_ip=172.25.15.11 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Thu Aug  9 12:41:18 2018 - [info]   Connecting to root@172.25.15.11(172.25.15.11:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to server2-relay-bin.000005
    Temporary relay log file is /var/lib/mysql/server2-relay-bin.000005
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Thu Aug  9 12:41:18 2018 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=172.25.15.13 --slave_ip=172.25.15.13 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Thu Aug  9 12:41:18 2018 - [info]   Connecting to root@172.25.15.13(172.25.15.13:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to server4-relay-bin.000004
    Temporary relay log file is /var/lib/mysql/server4-relay-bin.000004
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Thu Aug  9 12:41:19 2018 - [info] Slaves settings check done.
Thu Aug  9 12:41:19 2018 - [info] 
172.25.15.12(172.25.15.12:3306) (current master)
 +--172.25.15.11(172.25.15.11:3306)
 +--172.25.15.13(172.25.15.13:3306)

Thu Aug  9 12:41:19 2018 - [info] Checking replication health on 172.25.15.11..
Thu Aug  9 12:41:19 2018 - [info]  ok.
Thu Aug  9 12:41:19 2018 - [info] Checking replication health on 172.25.15.13..
Thu Aug  9 12:41:19 2018 - [info]  ok.
Thu Aug  9 12:41:19 2018 - [info] Checking master_ip_failover_script status:
Thu Aug  9 12:41:19 2018 - [info]   /etc/masterha/master_ip_failover --command=status --ssh_user=root --orig_master_host=172.25.15.12 --orig_master_ip=172.25.15.12 --orig_master_port=3306 


IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 172.25.0.100/24===

Checking the Status of the script.. OK 
Thu Aug  9 12:41:19 2018 - [info]  OK.
Thu Aug  9 12:41:19 2018 - [warning] shutdown_script is not defined.
Thu Aug  9 12:41:19 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

**

8查看MHA状态.

**

mkdir -p /var/log/masterha/app/

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

masterha_check_status  --conf=/etc/masterha/app.cnf 

运行正常结果为下图
这里写图片描述

启动参数设置简介

启动参数介绍:

--remove_dead_master_conf          该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除。
--manger_log                       日志存放位置
--ignore_last_failover             在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。该参数代表忽略上次MHA触发切换产生的文件,默认情况下,MHA发生切换后会在日志目录,也就是上面我设置的/data产生app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为--ignore_last_failover。

关闭MHA

masterha_stop --conf=/etc/masterha/app.cnf
Stopped app successfully.
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/masterha/app.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app/manager.log 2>&1  (wd: /etc/masterha)
(wd now: /mnt/mysql5.7)

查看程序日至了解信息
日志在这里写图片描述
这里写图片描述

**

9.测试中的错误

**

Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 99.
mysqlbinlog version not found!

所有节点添加软链接
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

**

10.relay脚本

**
MHA在发生切换的过程中,从库的恢复过程中依赖于relay log的相关信息,所以这里要将relay log的自动清除设置为OFF,采用手动清除relay log的方式。在默认情况下,从服务器上的中继日志会在SQL线程执行完毕后被自动删除。但是在MHA环境中,这些中继日志在恢复其他从服务器时可能会被用到,因此需要禁用中继日志的自动删除功能。定期清除中继日志需要考虑到复制延时的问题。在ext3的文件系统下,删除大的文件需要一定的时间,会导致严重的复制延时。为了避免复制延时,需要暂时为中继日志创建硬链接,因为在linux系统中通过硬链接删除大文件速度会很快。(在mysql数据库中,删除大表时,通常也采用建立硬链接的方式)
pure_relay_logs 脚本如下

#!/bin/bash
user=root
passwd=Hh~802119323
port=3306
log_dir='/data/masterha/log'
work_dir='/data'
purge='/usr/local/bin/purge_relay_logs'
if [ ! -d $log_dir ]
then
   mkdir $log_dir -p
fi
$purge --user=$user --password=$passwd --disable_relay_log_purge --port=$port --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1

脚本释义

--user mysql                      用户名
--password mysql                  密码
--port                            端口号
--workdir                         指定创建relay log的硬链接的位置,默认是/var/tmp,由于系统不同分区创建硬链接文件会失败,故需要执行硬链接具体位置,成功执行脚本后,硬链接的中继日志文件被删除
--disable_relay_log_purge         默认情况下,如果relay_log_purge=1,脚本会什么都不清理,自动退出,通过设定这个参数,当relay_log_purge=1的情况下会将relay_log_purge设置为0。清理relay log之后,最后将参数设置为OFF

这里写图片描述

关于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 = '172.25.0.100/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$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 "\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 \"`;
}
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=port\n";
}

修改后检测无报错

测试 手动测试
进行rpel检测 检测集群配置 出现多台主机时 set global read——read=only

手动命令
masterha_master_switch –conf=/etc/masterha/app1.cnf –master_state=alive –new_master_host=172.25.15.13 –new_master_port=3306 –orig_master_is_new_slave –running_updates_limit=10000
这里写图片描述
看到12的master切换为13
测试成功

自动切换测试

kill -9 mysql的id即可
可以在13slave上看到 master由12切换为11
这里写图片描述
这里写图片描述
测试成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值