The MHA high availability and configuration MySQL failover

Preface:
run the same services combine server called a cluster
center of a cluster: there are master, Slave,
MHA to solve the case in the cluster master downtime,
MMM cluster architecture: there are two master servers
MHA Overview

  • Japan's DeNA company youshimaton (now working in Facebook Inc.) Development
  • Excellent as a failover at the primary and high availability environment MySQL high availability software upgrade from
    the composition of the MHA
  • MHA Manager (management node)
  • MHA Node (node data)
    the MHA Characteristics
  • Automatic failover process, MHA attempt to save the binary log from the primary server goes down, the greatest degree of assurance without losing data
  • Use MySQL5.5 semi-synchronous replication, can greatly reduce the risk of data loss
    cases topology
    The MHA high availability and configuration MySQL failover
    experimental environment
    centos7 version
    MHA version 0.57
    MySQL version 5.6.36
    cmake version 2.8.6
    MHA: 192.168.191.132
    Master: 192.168.191.160
    slave1: 192.168. 191.161
    slave2: 192.168.191.162
    build MySQL replication master from the environment
    1. the modified host name server in order to distinguish
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    same three mysql installation configuration server process, so to Example master
    installation dependent compilation environment
    The MHA high availability and configuration MySQL failover
    installed cmake compiler
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    installed mysql database
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    the next step is make && make install process of
    optimization mysql database path, create mysql user, initialize the database
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    The MHA high availability and configuration MySQL failover
    under vim /etc/my.cnf:
    modify the master server master master configuration file
    The MHA high availability and configuration MySQL failover
    to modify the main configuration file from the server slave1
    The MHA high availability and configuration MySQL failover
    modify configuration files from the master server slave2
    The MHA high availability and configuration MySQL failover
    the main connection from the server to do two soft, easy to operate
    The MHA high availability and configuration MySQL failover
    master-slave MySQL server starts
    The MHA high availability and configuration MySQL failover
    to create a master-slave synchronization user myslave
    mysql> grant replication slave on *.* to 'myslave'@'192.168.191.%' identified by '123';
    Query OK, 0 rows affected (0.00 sec)

    Mha authorized to all database users authority to operate the database

    mysql> grant all privileges on *.* to 'mha'@'192.168.191.%' identified by 'manager';
    Query OK, 0 rows affected (0.00 sec)
    mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';
    Query OK, 0 rows affected (0.00 sec)
    mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
    Query OK, 0 rows affected (0.00 sec)
    mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
    Query OK, 0 rows affected (0.00 sec)
    #刷新数据库
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    View and to prohibit file synchronization point, at this time do not add anything to the database on the main server mysql, it will lead
    The MHA high availability and configuration MySQL failover
    in the implementation of sync from the server

    mysql> change master to master_host='192.168.191.160',master_user='myslave',master_password='123',master_log_file='master-bin.000002',master_log_pos=12215;
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    mysql> start slave;
    Query OK, 0 rows affected (0.01 sec)
    mysql> show slave status\G;
    ···省略部分内容
             Slave_IO_Running: Yes      //如果此处是Slave_IO_Running: Connecting,查看主服务器的防火墙是否关闭
            Slave_SQL_Running: Yes
    ···省略部分内容

    Provided from the server to two read-only mode

    mysql> set global read_only=1;
    Query OK, 0 rows affected (0.00 sec)

    安装Node组件(所有服务器,版本为0.57)
    The MHA high availability and configuration MySQL failover
    安装perl环境
    The MHA high availability and configuration MySQL failover
    MHA对于每个操作系统版本不一样,centos7.4必须选择0.57版本,在所有服务器上必须先安装node组件,最后在MHA-manager节点上安装manager组件,因为manager依赖node组件
    安装Node组件

    [root@master ~]# tar zxvf /abc/mha/mha4mysql-node-0.57.tar.gz
    [root@master ~]# cd mha4mysql-node-0.57/
    [root@master mha4mysql-node-0.57]# perl Makefile.PL
    [root@master mha4mysql-node-0.57]# make
    [root@master mha4mysql-node-0.57]# make install

    node安装后在/usr/local/bin/下面生成几个脚本工具(这些工具通常由MHA Manager脚本触发,无需人为操作)
    The MHA high availability and configuration MySQL failover
    save_binary_logs:保存和复制master的二进制文件
    apply_diff_relay_logs:识别差异的中继日志事件并将其差异的事件应用于其他的slave
    filter_mysqlbinlog:去除不必要的ROLLBACK回滚事件(MHA已不再使用这个工具)
    purge_relay_logs:清除中继日志(不会阻塞SQL线程)
    安装Manager组件(在manager节点上,版本为0.57)

    [root@manager ~]# tar zxvf /abc/mha/mha4mysql-manager-0.57.tar.gz
    [root@manager ~]# cd mha4mysql-manager-0.57/
    [root@manager mha4mysql-manager-0.57]# perl Makefile.PL
    [root@manager mha4mysql-manager-0.57]# make
    [root@manager mha4mysql-manager-0.57]# make install

    manager安装后在/usr/local/bin/ 下面会生成几个脚本工具
    The MHA high availability and configuration MySQL failover
    masterha_check_ssh:检查MHA的SSH配置状况
    masterha_check_repl:检查MySQL复制状况
    masterha_manager:启动manager的脚本
    masterha_check_status:检测当前MHA运行状态
    masterha_master_monitor:检测master是否宕机
    masterha_master_switch:控制故障转移(自动或者手动)
    masterha_conf_host:添加或删除配置server信息
    masterha_stop:关闭manager
    配置无密码认证
    工具:ssh-keygen、ssh-copy-id
    (1)在manager上配置所有数据节点的无密码认证

    [root@manager ~]# ssh-keygen -t rsa
    #一直点回车
    [root@manager ~]# ssh-copy-id 192.168.191.160
    [root@manager ~]# ssh-copy-id 192.168.191.161
    [root@manager ~]# ssh-copy-id 192.168.191.162

    (2)在master上配置到数据库节点slave1和slave2的无密码认证

    #一直点回车
    [root@master ~]# ssh-copy-id 192.168.191.161
    [root@master ~]# ssh-copy-id 192.168.191.162

    (3)在slave1上配置到数据库节点master和slave2的无密码认证

    [root@slave1 ~]# ssh-keygen -t rsa
    #一直点回车
    [root@slave1 ~]# ssh-copy-id 192.168.191.160
    [root@slave1 ~]# ssh-copy-id 192.168.191.162

    (4)在slave2上配置到数据库节点master和slave1的无密码认证

    [root@slave2 ~]# ssh-keygen -t rsa
    #一直点回车
    [root@slave2 ~]# ssh-copy-id 192.168.191.160
    [root@slave2 ~]# ssh-copy-id 192.168.191.161

    配置MHA(在manager节点上)
    复制相关脚本到/usr/local/bin 目录

    [root@manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin

    拷贝后会有四个执行文件
    The MHA high availability and configuration MySQL failover
    master_ip_failover:自动切换时VIP管理的脚本
    master_ip_online_change:在线切换时vip的管理
    power_manager:故障发生后关闭主机的脚本
    send_report:因故障切换后发送报警的脚本
    (2)复制上述的自动切换时VIP管理脚本到/usr/local/bin目录中,使用脚本管理VIP

    [root@manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

    修改master_ip_failover脚本(删除原有的内容,重新写入)

    [root@manager ~]# vim /usr/local/bin/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
    );
    #############################添加内容部分#########################################
    #设置漂移IP
    my $vip = '192.168.191.200';
    my $brdc = '192.168.191.255';
    my $ifdev = 'ens33';
    my $key = '1';
    my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
    my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
    my $exit_code = 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

    [root@master ~]# /sbin/ifconfig ens33:1 192.168.191.200/24

    (4)创建MHA软件目录并拷贝配置文件

    [root@manager ~]# mkdir /etc/masterha
    [root@manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
    [root@manager ~]# vim /etc/masterha/app1.cnf 
    [server default]
    #manager配置文件
    manager_log=/var/log/masterha/app1/manager.log     
    #manager日志
    manager_workdir=/var/log/masterha/app1
    #master保存binlog的位置,这里的路径要与master里配置的bilog的相同
    master_binlog_dir=/home/mysql
    #设置自动failover时候的切换脚本。也就是上边的那个脚本
    master_ip_failover_script=/usr/local/bin/master_ip_failover
    #设置手动切换时候的切换脚本
    master_ip_online_change_script=/usr/local/bin/master_ip_online_change
    #这个密码是前文中创建监控用户的那个密码
    password=manager
    ping_interval=1
    remote_workdir=/tmp
    #设置复制用户密码
    repl_password=123
    #设置复制用户的用户
    repl_user=myslave
    #设置发生切换后发生报警的脚本
    secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.150.158 -s 192.168.150.244
    #设置故障发生关闭故障脚本主机
    shutdown_script=""
    #设置ssh的登录用户名
    ssh_user=root
    #设置监控用户
    user=mha
    [server1]
    hostname=192.168.150.240
    port=3306
    [server2]
    #设置为候选master,如果设置该参数以后,发送主从切换以后将会从此从库升级为主库
    candidate_master=1
    #默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave为新的master
    check_repl_delay=0
    hostname=192.168.150.158
    port=3306
    [server3]
    hostname=192.168.150.244
    port=3306

    测试
    SSH免交互登陆(manager节点

    [root@manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
    ···省略部分内容
    #如果正常会输出successfully
    Sun Jan 12 19:19:11 2020 - [info] All SSH connection tests passed successfully.

    启动MHA,查看MHA状态

    #启动MHA,放在后台运行
    [root@manager ~]# 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 &
    [1] 13525
    #查看MHA状态,可以看到当前的master是mysql节点
    [root@manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
    app1 (pid:13525) is running(0:PING_OK), master:192.168.191.200
  • –remove_dead_master_conf:该参数代表当发生主从切换后,老的ip将会从配置文件中移除
  • –ignore_last_failover:在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行failover,之所以这样限制是为了避免ping-pong效应,该参数代表忽略上次MHA触发切换后产生的文件,默认情况下,MHA发生切换后会在日志记目录,也就是上面设置的appl.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为–ignore_last_failover
    在manager上启动监控观察日志记录
    The MHA high availability and configuration MySQL failover
    模拟故障
    The MHA high availability and configuration MySQL failover
    在主库master上执行停掉mysql服务
    [root@master ~]# pkill -9 mysqld

    此时manager监测到主服务器宕机,切换备用主服务器为主服务器
    The MHA high availability and configuration MySQL failover
    在slave1上查看IP地址

    [root@slave1 ~]# ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.191.160  netmask 255.255.255.0  broadcast 192.168.191.255
    ···
    #虚拟IP地址转换到备用主服务器
    ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.191.200  netmask 255.255.255.0  broadcast 192.168.191.255
    ···

    In this case, the client through the virtual IP address of the landing, can also log on.
    MHA high-availability configurations and failover test is complete.

Guess you like

Origin blog.51cto.com/14557905/2466447