搭建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

相关文章: