搭建MHA测试

搭建MHA:

 手工切换: 

  masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.60.167 --new_master_port=18601 --orig_master_is_new_slave --running_updates_limit=10000

启动mha:

     nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/masterha/app1/manager.log 2>&1 &   (启动MHA,切记不能直接关闭窗口,需要exit退出)

  master_ip_failover:  MHA自动切换新主机, 虚IP配置perl脚本

  master_ip_online_change: MHA手工切换新主机,虚IP配置perl脚本

  MHA切换过程:

  1. 从宕机崩溃的Master保存二进制日志事件(binlogevent)

  2. 识别含有最新更新的Slave

  3. 应用差异的中继日志(relaylog)到其他Slave

  4. 应用从Master保存的二进制日志事件

  5. 提升一个Slave为新的Master

  6. 使其他的Slave连接新的Master进行复制


步骤:
1, MySQL的1主多从(至少1主2从)
2,MHA配置: 1,安装MHA的rpm包,配置机器的信任SSH
3,配置app.cnf文件
4,VIP配置
利用app.cnf来管理一主多从MySQL,并利用SSH来打通,让备库可以拷贝binlog日志到备库,应用log数据。

2019年4月15日18:19:47
1,配置过程中app.cnf文件名,弄错,导致提示找不到app1.cnf文件
2,配置mysql,发现UUID一致,导致主从复制无法正常
3,配置mha,发现Can't exec "mysqlbinlog":
添加软连接(所有节点)
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
4,配置app1.cnf的master_binlog_dir路径配置错误,提示找不到路径binlog的路径不对

5,mha切换到新主库后,手工切换回原主机
1,错误 :There are 2 non-slave servers! 原因是用了一个新端口的mysql配置到了mha集群上
2,错误:192.168.60.166 is not alive!
3,SHOW SLAVE HOSTS; 在主库上执行可以看有几个slave my.cnf 配置 report-host =192.168.60.168
可以在host里显示具体名称
自动Failover(必须先启动MHA Manager,否则无法自动切换,当然手动切换不需要开启MHA Manager监控。各位童鞋请参考
前面启动MHA Manager)
#--master_state 指明在线切换
#--orig_master_is_new_slave 指定原先的主作为从库挂到新的主上
4,手工切换: master_ip_online_change
自动切换: master_ip_failover
配置VIP:
5,--remove_dead_master_conf 启动mha,加上这个参数,发生自动转移的时候,会删除app1.cnf文件的原主库配置信息,如果后面要加入原主库,需手工填入server的信息
6,在线切换需要关闭数据库事件 set global event_scheduler = off
7,mha自动切换后,原主库被删除。有下面信息: Deleted server1 entry from /etc/masterha/app1.cnf
8,手工failover,在master死掉,指定--master_state=dead,master是正常的--master_state=alive
9,全部从库:mysql -e 'set global relay_log_purge=0' 设置relay log的清除方式
10,手工和自动切换: 加上--ignore_last_failover
11,测试,故障转移,如果切换到新主库,如新主库的有relay日志延时,mha会将其他slave最新的relay日志应用过来,保证取得最新的数据:
master_pos_wait(mysql-bin1.000006:1754710) completed on 192.168.60.166(192.168.60.166:18601). Executed 3182 events.

12, 测试VIP漂移情况:  grep VIP /var/log/masterha/app1/manager.log

13,在配置VIP过程中: ifconfig eth0:0 192.168.60.200 后 其他网段不能连通,后来请教运维高手,是60.200的IP地址已经有了,虽然能前行加进去,但是在网络中争抢IP, ping不通的IP,不一定是没有使用,可能服务器设置禁止ping

14, 查看VIP漂移情况:   grep  'vip'  /var/log/masterha/app1/manager.log

15,把原来master重新作为新主库的slave:  grep -i "All other slaves should start" /var/log/masterha/app1/manager.log

 app1.cnf 配置:  

[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/etc/masterha/app1
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=cc.123
ping_interval=1
repl_password=cc.123
repl_user=sa
ssh_user=root
user=sa

[server1]
candidate_master=1
hostname=192.168.60.167
master_binlog_dir=/home/td/multi_msb_5_7_19/node1/data
port=18601

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.60.166
master_binlog_dir=/home/tc/multi_msb_5_7_19/node1/data
port=18601

[server3]
hostname=192.168.60.168
ignore_fail=1
master_binlog_dir=/home/td/multi_msb_5_7_19/node1/data
no_master=1
port=18601
View Code

 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.60.239/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $ssh_Bcast_arp = "/sbin/arping -c 3 -A 192.168.60.239";   #ARP回复模式,更新邻居。要是不加则服务器会自动等到vip缓存失效,期间VIP会有一定时间的不可用。

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();
            &start_arp();
            $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 start_arp() {
    `ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
}

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";
}
View Code

 

master_ip_online_change

#!/usr/bin/env perl
use strict;  
use warnings FATAL =>'all';  
  
use Getopt::Long;  

  
my $vip = '192.168.60.239/24';  # Virtual IP  
my $key = "0";  
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";  
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $ssh_Bcast_arp = "/sbin/arping -c 3 -A 192.168.60.239";   #ARP回复模式,更新邻居。要是不加则服务器会自动等到vip缓存失效,期间VIP会有一定时间的不可用。
my $exit_code = 0;  
  
my (  
  $command,              $orig_master_is_new_slave, $orig_master_host,  
  $orig_master_ip,       $orig_master_port,         $orig_master_user,  
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,  
  $new_master_ip,        $new_master_port,          $new_master_user,  
  $new_master_password,  $new_master_ssh_user,  
);  
GetOptions(  
  'command=s'                => \$command,  
  'orig_master_is_new_slave' => \$orig_master_is_new_slave,  
  'orig_master_host=s'       => \$orig_master_host,  
  'orig_master_ip=s'         => \$orig_master_ip,  
  'orig_master_port=i'       => \$orig_master_port,  
  'orig_master_user=s'       => \$orig_master_user,  
  'orig_master_password=s'   => \$orig_master_password,  
  'orig_master_ssh_user=s'   => \$orig_master_ssh_user,  
  'new_master_host=s'        => \$new_master_host,  
  'new_master_ip=s'          => \$new_master_ip,  
  'new_master_port=i'        => \$new_master_port,  
  'new_master_user=s'        => \$new_master_user,  
  'new_master_password=s'    => \$new_master_password,  
  'new_master_ssh_user=s'    => \$new_master_ssh_user,  
);  
  
  
exit &main();  
  
sub main {  
  
#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";  
  
if ( $command eq "stop" || $command eq "stopssh" ) {  
  
        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.  
        # If you manage master ip address at global catalog database,  
        # invalidate orig_master_ip here.  
        my $exit_code = 1;  
        eval {  
            print "\n\n\n***************************************************************\n";  
            print "Disabling the VIP - $vip on old master: $orig_master_host\n";  
            print "***************************************************************\n\n\n\n";  
&stop_vip();  
            $exit_code = 0;  
        };  
        if ($@) {  
            warn "Got Error: $@\n";  
            exit $exit_code;  
        }  
        exit $exit_code;  
}  
elsif ( $command eq "start" ) {  
  
        # all arguments are passed.  
        # If you manage master ip address at global catalog database,  
        # activate new_master_ip here.  
        # You can also grant write access (create user, set read_only=0, etc) here.  
my $exit_code = 10;  
        eval {  
            print "\n\n\n***************************************************************\n";  
            print "Enabling the VIP - $vip on new master: $new_master_host \n";  
            print "***************************************************************\n\n\n\n";  
&start_vip(); 
&start_arp();
            $exit_code = 0;  
        };  
        if ($@) {  
            warn $@;  
            exit $exit_code;  
        }  
        exit $exit_code;  
}  
elsif ( $command eq "status" ) {  
        print "Checking the Status of the script.. OK \n";  
        `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;  
        exit 0;  
}  
else {  
&usage();  
        exit 1;  
}  
}  
  
# A simple system call that enable the VIP on the new master  
sub start_vip() {  
`ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;  
}

sub start_arp() {
    `ssh $new_master_ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
}

# A simple system call that disable the VIP on the old_master  
sub stop_vip() {  
`ssh $orig_master_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";  
}
 
View Code

 

测试数据: 模拟备用主库,IO断,MHA从另一个Slave服务器中获取relay日志得到最新数据 

USE test;
CREATE TABLE test_load (
a INT PRIMARY KEY AUTO_INCREMENT,
b CHAR(80)
) ENGINE=INNODB;

DELIMITER //
CREATE PROCEDURE p_load1(COUNT INT UNSIGNED)
BEGIN
DECLARE s INT UNSIGNED DEFAULT 1;
DECLARE c CHAR(80) DEFAULT REPEAT('a',80);
WHILE s <= COUNT DO
INSERT INTO test_load(b) SELECT c;
COMMIT;
SET s = s+1;
END WHILE;
END;

CALL p_load1(10000)
View Code

 清理relaylog数据: purge_relay_log.sh

#!/bin/bash
user=root
passwd=cc.123
port=18601
log_dir='/home/td/multi_msb_5_7_19/node1/data'
work_dir='/home/data'
purge='/usr/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
View Code

 

pure_relay_logs脚本参数如下所示:
--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。

 

 

 

配置Crontab:   每天早上5:10分清理relaylog

10 5 * * * sh /home/purge_relay_log.sh

 周末,测试机房一次停电,机器全部关闭,起来MHA后,VIP没有,重建:

ifconfig eth0:0 192.168.60.239/24
arping -c 3 -A 192.168.60.239

 

posted @ 2019-04-18 16:02  zping  阅读(712)  评论(0编辑  收藏  举报