zoukankan      html  css  js  c++  java
  • MySQL高可用群集MHA部署及故障测试分析

    一、案例前置知识

    1、MHA概述

    ·一套优秀的MySQL高可用环境下故障切换和主从复制的软件

    ·MySQL故障过程中,MHA能做到0-30秒内自动完成故障切换

    2、MHA的组成

    ·MHA Manager(管理节点)

    Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。

    ·MHA Node(数据节点)

    Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。

    3、MHA特点

    ·自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失

    ·使用半同步复制,可以大大降低数据丢失的风险

    ·目前MHA支持一主多从架构,最少三台服务,即一主两从

    4、MHA缺点

    ·需要编写脚本或利用第三方工具来实现vip的配置

    ·MHA启动后只会对数据库进行监控,需要基于ssh免认证配置,存在一定的安全隐患

    ·没有提供从服务器的读负载均很的功能。

    二、案例拓扑图

    三、案例部署

    1、部署思路

    (1)、MHA架构

    ①数据库安装

    ②一主两从

    ③MHA搭建

    2、故障模拟

    ①主库失效

    ②备选主库成为主库

    ③从库2将备选主库指向为主库

    3、故障修复

    ①坏库修复,启动

    ②在修复好的库上建立新主从关系

    ③修改manager配置文件,添加修好的库的记录

    ④重启mha

    主服务器:mysql-server 20.0.0.10,从1服务器:mysql-slave1 20.0.0.20,从2服务器:mysql-slave2 20.0.0.30,mha:20.0.0.40

    2、在三台 MySQL 节点上分别安装数据库

     MySQL 版本请使用 5.6.36,cmake 版本请使用 2.8.6。安装过程如下。

    主服务器
    yum -y install ncurses-devel gcc-c++ perl-Module-Install
    tar zxvf cmake-2.8.6.tar.gz
    cd cmake-2.8.6
    ./configure
    gmake && gmake install
    cd
    tar zxvf mysql-5.6.36.tar.gz
    cd mysql-5.6.36
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
    make && make install
    cp support-files/my-default.cnf /etc/my.cnf
    cp support-files/mysql.server /etc/rc.d/init.d/mysqld
    chmod +x /etc/rc.d/init.d/mysqld
    chkconfig --add mysqld
    echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
    source /etc/profile
    groupadd mysql
    useradd -M -s /sbin/nologin mysql -g mysql
    chown -R mysql.mysql /usr/local/mysql
    mkdir -p /data/mysql
    /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
    echo '
    [client]
    port = 3306
    socket = /usr/local/mysql/mysql.sock
    [mysql]
    port = 3306
    socket = /usr/local/mysql/mysql.sock
    [mysqld]
    user = mysql
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    pid-file = /usr/local/mysql/mysqld.pid
    socket = /usr/local/mysql/mysql.sock
    server-id = 1
    log_bin = master-bin
    log-slave-updates = true
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
    ln -s /usr/local/mysql/bin/mysql /usr/sbin/
    ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
    systemctl start mysqld
    netstat -anpt | grep 3306
    从1服务器
    yum -y install ncurses-devel gcc-c++ perl-Module-Install
    tar zxvf cmake-2.8.6.tar.gz
    cd cmake-2.8.6
    ./configure
    gmake && gmake install
    cd
    tar zxvf mysql-5.6.36.tar.gz
    cd mysql-5.6.36
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
    make && make install
    cp support-files/my-default.cnf /etc/my.cnf
    cp support-files/mysql.server /etc/rc.d/init.d/mysqld
    chmod +x /etc/rc.d/init.d/mysqld
    chkconfig --add mysqld
    echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
    source /etc/profile
    groupadd mysql
    useradd -M -s /sbin/nologin mysql -g mysql
    chown -R mysql.mysql /usr/local/mysql
    mkdir -p /data/mysql
    /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
    echo '
    [client]
    port = 3306
    socket = /usr/local/mysql/mysql.sock
    [mysql]
    port = 3306
    socket = /usr/local/mysql/mysql.sock
    [mysqld]
    user = mysql
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    pid-file = /usr/local/mysql/mysqld.pid
    socket = /usr/local/mysql/mysql.sock
    server-id = 2
    log_bin = master-bin
    relay-log = relay-log-bin
    relay-log-index = slave-relay-bin.index
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
    ln -s /usr/local/mysql/bin/mysql /usr/sbin/
    ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
    systemctl start mysqld
    netstat -anpt | grep 3306
    从2服务器
    yum -y install ncurses-devel gcc-c++ perl-Module-Install
    tar zxvf cmake-2.8.6.tar.gz
    cd cmake-2.8.6
    ./configure
    gmake && gmake install
    cd
    tar zxvf mysql-5.6.36.tar.gz
    cd mysql-5.6.36
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
    make && make install
    cp support-files/my-default.cnf /etc/my.cnf
    cp support-files/mysql.server /etc/rc.d/init.d/mysqld
    chmod +x /etc/rc.d/init.d/mysqld
    chkconfig --add mysqld
    echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
    source /etc/profile
    groupadd mysql
    useradd -M -s /sbin/nologin mysql -g mysql
    chown -R mysql.mysql /usr/local/mysql
    mkdir -p /data/mysql
    /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
    echo '
    [client]
    port = 3306
    socket = /usr/local/mysql/mysql.sock
    [mysql]
    port = 3306
    socket = /usr/local/mysql/mysql.sock
    [mysqld]
    user = mysql
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    pid-file = /usr/local/mysql/mysqld.pid
    socket = /usr/local/mysql/mysql.sock
    server-id = 3
    relay-log = relay-log-bin
    relay-log-index = slave-relay-bin.index
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
    ln -s /usr/local/mysql/bin/mysql /usr/sbin/
    ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
    systemctl start mysqld
    netstat -anpt | grep 3306

    3、配置 MySQL一主两从

    (1)、MySQL 主从配置相对比较简单,需要注意的是授权。步骤如下:在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是 manager 使用。

    mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123';
    mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
    mysql> flush privileges;

    (2)、在主服务器上查看二进制文件和同步点

    mysql> show master status;
    +-------------------+----------+--------------+------------------+-------------------+
    | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-------------------+----------+--------------+------------------+-------------------+
    | master-bin.000001 |      608 |              |                  |                   |
    +-------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

    (3)、接下来在从1 和 从2 分别执行同步,查看 IO 和 SQL 线程都是 yes 代表同步是否正常

    mysql> change master to master_host='20.0.0.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=608;
    mysql> start slave;
    mysql> show slave statusG;
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

    1 I/O线程显示为NO: 主库与从库网络不通、主库未授权给从库
    2 SQL线程显示为NO:从库日志和位置点与主不同步
    3 若从库查看连接主库I/0线程状态为conneting,一直是这个状态,考虑双方的防火墙是否开启。

    (4)、必须设置两个从库为只读模式

    mysql> set global read_only=1;

    (5)、在主库插入两条数据,测试是否同步

    mysql> create database test_lixing;
    mysql> use test_lixing;
    mysql> create table test(id int);
    mysql> insert into test(id) values (1);
    mysql> select * from test;

    (6)、在两个从库分别查询如下所示说明主从同步正常

    从1
    mysql> select * from test_lixing.test;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)
    从2
    mysql> select * from test_lixing.test;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)

    4、安装MHA软件

    (1)、在所有服务器上都安装 MHA 依赖的环境,每个服务器上都需要两个源(epel.repo和CentOS7-Base-163.repo),将这两个源放入/etc/yum.repos.d的目录下,在主服务器上操作演示安装

    yum install epel-release --nogpgcheck
    yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

    (2)、在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件

    tar zxvf mha4mysql-node-0.57.tar.gz
    cd mha4mysql-node-0.57
    perl Makefile.PL
    make && make install

    (3)、在mha-manager 服务器上安装 manager 组件

    tar zxvf mha4mysql-manager-0.57.tar.gz
    cd mha4mysql-manager-0.57
    perl Makefile.PL
    *** Module::AutoInstall version 1.06
    *** Checking for Perl dependencies...
    [Core Features]
    - DBI                   ...loaded. (1.627)
    - DBD::mysql            ...loaded. (4.023)
    - Time::HiRes           ...loaded. (1.9725)
    - Config::Tiny          ...loaded. (2.14)
    - Log::Dispatch         ...loaded. (2.41)
    - Parallel::ForkManager ...loaded. (1.18)
    - MHA::NodeConst        ...loaded. (0.57)
    *** Module::AutoInstall configuration finished.
    Checking if your kit is complete...
    Looks good
    Writing Makefile for mha4mysql::manager
    make && make install

    manager 安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
    masterha_check_ssh             检查 MHA 的 SSH 配置状况
    masterha_check_repl            检查 MySQL 复制状况
    masterha_manger                启动 manager的脚本
    masterha_check_status          检测当前 MHA 运行状态
    masterha_master_monitor        检测 master 是否宕机
    masterha_master_switch         控制故障转移(自动或者手动)
    masterha_conf_host             添加或删除配置的 server 信息
    masterha_stop                  关闭manager

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

    5、配置无密码认证

    (1)、在 manager服务器 上配置到所有节点的无密码认证

    ssh-keygen -t rsa       #一路按回车键
    ssh-copy-id 20.0.0.10    #先输入yes,再输入20.0.0.10服务器的密码
    ssh-copy-id 20.0.0.20
    ssh-copy-id 20.0.0.30
    验证
    ssh root@20.0.0.10
    ssh root@20.0.0.20
    ssh root@20.0.0.30

    (2)、在主服务器上配置到数据库节点的无密码认证

    ssh-keygen -t rsa
    ssh-copy-id 20.0.0.20
    ssh-copy-id 20.0.0.30
    验证
    ssh root@20.0.0.20
    ssh root@20.0.0.30

    (3)、在从1服务器上配置到数据库节点的无密码认证

    ssh-keygen -t rsa
    ssh-copy-id 20.0.0.10
    ssh-copy-id 20.0.0.30
    验证
    ssh root@20.0.0.10
    ssh root@20.0.0.30

    (4)、在从2服务器上配置到数据库节点的无密码认证

    ssh-keygen -t rsa
    ssh-copy-id 20.0.0.10
    ssh-copy-id 20.0.0.20
    验证
    ssh root@20.0.0.10
    ssh root@20.0.0.20

    6、配置 MHA

    (1)、在 manager 节点上复制相关脚本到/usr/local/bin 目录,复制上述的自动切换时 VIP 管理的脚本到/usr/local/bin 目录,这里使用脚本管理 VIP

    cp -ra mha4mysql-manager-0.57/samples/scripts /usr/local/bin
    ll /usr/local/bin/scripts/
    cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
    vi /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
    );
    #############################添加内容部分#########################################
    my $vip = '20.0.0.200';
    my $brdc = '20.0.0.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 " IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip=== ";
    if ( $command eq "stop" || $command eq "stopssh" ) {
    my $exit_code = 1;
    eval {
    print "Disabling the VIP on old master: $orig_master_host ";
    &stop_vip();
    $exit_code = 0;
    };
    if ($@) {
    warn "Got Error: $@ ";
    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 ";
    &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 ";
    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 ";
    }

     拷贝后会有四个执行文件,文件的含义如下:
    master_ip_failover              #自动切换时 VIP 管理的脚本
    master_ip_online_change         #在线切换时 vip 的管理
    power_manager                   #故障发生后关闭主机的脚本
    send_report                     #因故障切换后发送报警的脚本

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

    1 mkdir /etc/masterha
     2 cp mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
     3 vim /etc/masterha/app1.cnf
     4 [server default]
     5 manager_workdir=/var/log/masterha/app1
     6 manager_log=/var/log/masterha/app1/manager.log
     7 master_binlog_dir=/usr/local/mysql/data
     8 master_ip_failover_script= /usr/local/bin/master_ip_failover
     9 master_ip_online_change_script= /usr/local/bin/master_ip_online_change10 password=manager
    11 user=mha
    12 ping_interval=1
    13 remote_workdir=/tmp
    14 repl_password=123
    15 repl_user=myslave
    16 secondary_check_script= /usr/local/bin/masterha_secondary_check -s 20.0.0.20 -s 20.0.0.30
    17 shutdown_script=""
    18 ssh_user=root
    19 [server1]
    20 hostname=20.0.0.10
    21 port=3306
    22 [server2]
    23 hostname=20.0.0.20
    24 port=3306
    25 candidate_master=1
    26 check_repl_delay=0
    27 [server3]
    28 hostname=20.0.0.30
    29 port=3306

    (3)、测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示

    masterha_check_ssh -conf=/etc/masterha/app1.cnf
    ......
    Wed Oct 28 00:07:09 2020 - [debug]  Connecting via SSH from root@20.0.0.30(20.0.0.30:22) to root@20.0.0.20(20.0.0.20:22)..
    Wed Oct 28 00:07:10 2020 - [debug]   ok.
    Wed Oct 28 00:07:10 2020 - [debug]
    Wed Oct 28 00:07:08 2020 - [debug]  Connecting via SSH from root@20.0.0.20(20.0.0.20:22) to root@20.0.0.10(20.0.0.10:22)..
    Wed Oct 28 00:07:09 2020 - [debug]   ok.
    Wed Oct 28 00:07:09 2020 - [debug]  Connecting via SSH from root@20.0.0.20(20.0.0.20:22) to root@20.0.0.30(20.0.0.30:22)..
    Wed Oct 28 00:07:09 2020 - [debug]   ok.
    Wed Oct 28 00:07:10 2020 - [info] All SSH connection tests passed successfully.

    (4)、测试 MySQL 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常

    masterha_check_repl -conf=/etc/masterha/app1.cnf
    ......
    IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===
    Checking the Status of the script.. OK
    Wed Oct 28 09:25:28 2020 - [info]  OK.
    Wed Oct 28 09:25:28 2020 - [warning] shutdown_script is not defined.
    Wed Oct 28 09:25:28 2020 - [info] Got exit code 0 (Not master dead).
    MySQL Replication Health is OK.

    (5)、启动 MHA

    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 &

    (6)、查看 MHA 状态,可以看到当前的 master 是主服务器节点

    masterha_check_status --conf=/etc/masterha/app1.cnf
    app1 (pid:24405) is running(0:PING_OK), master:20.0.0.10

    (7)、查看 MHA 日志,也以看到当前的 master 是 20.0.0.10

    cat /var/log/masterha/app1/manager.log
    ......
    Wed Oct 28 09:27:04 2020 - [info]     Replicating from 20.0.0.10(20.0.0.10:3306)
    Wed Oct 28 09:27:04 2020 - [info] Current Alive Master: 20.0.0.10(20.0.0.10:3306)
    Wed Oct 28 09:27:04 2020 - [info] Checking slave configurations..
    Wed Oct 28 09:27:04 2020 - [warning]  relay_log_purge=0 is not set on slave 20.0.0.20(20.0.0.20:3306).
    Wed Oct 28 09:27:04 2020 - [warning]  relay_log_purge=0 is not set on slave 20.0.0.30(20.0.0.30:3306).
    Wed Oct 28 09:27:04 2020 - [warning]  log-bin is not set on slave 20.0.0.30(20.0.0.30:3306). This host cannot be a master.
    ......

    (8)、第一次配置vip的时候,需要在主服务器上创建虚拟IP地址

    ifconfig ens33:1 20.0.0.200/24
    ifconfig

    四、故障测试

    1、关闭主服务器的mysql服务

    pkill mysqld

    2、查看从1服务器的虚拟IP地址有没有转移过来,从2上查看主服务器是不是从1

    从1服务器
    ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 20.0.0.20  netmask 255.255.255.0  broadcast 20.0.0.255
            inet6 fe80::a140:18a2:d866:8f33  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:42:10:0a  txqueuelen 1000  (Ethernet)
            RX packets 1315993  bytes 1324408060 (1.2 GiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 795190  bytes 69519345 (66.2 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 20.0.0.200  netmask 255.0.0.0  broadcast 20.255.255.255
            ether 00:0c:29:42:10:0a  txqueuelen 1000  (Ethernet)
    从2服务器
    mysql
    mysql> show slave status G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 20.0.0.20
                      Master_User: myslave
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: master-bin.000003
              Read_Master_Log_Pos: 120
                   Relay_Log_File: relay-log-bin.000002
                    Relay_Log_Pos: 284
            Relay_Master_Log_File: master-bin.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    ......

    3、重新开启manager服务器

    (1)、主服务器开启mysql服务

    systemctl restart mysqld

    (2)、查看从1服务器查看二进制文件和同步点

    mysql> show master status;
    +-------------------+----------+--------------+------------------+-------------------+
    | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-------------------+----------+--------------+------------------+-------------------+
    | master-bin.000003 |      120 |              |                  |                   |
    +-------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

    (3)、在主服务器上执行同步,查看 IO 和 SQL 线程都是 yes 代表同步是否正常

    mysql>  change master to
    master_host='20.0.0.20',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120;
    mysql> start slave;
    mysql> show slave status G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 20.0.0.20
                      Master_User: myslave
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: master-bin.000003
              Read_Master_Log_Pos: 120
                   Relay_Log_File: mysqld-relay-bin.000002
                    Relay_Log_Pos: 284
            Relay_Master_Log_File: master-bin.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes18  ......

    (4)、在从1上创建,主服务器和从2上查看

    从1服务器
    mysql> create database aa;
    mysql> use aa;
    mysql> create table test(id int(3));
    mysql> insert into test values(1);
    mysql> select * from test;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    主服务器
    mysql> use aa;
    mysql> select * from test;
    +------+
    | id   |
    +------+
    |    1 |
    +------+
    从2服务器
    mysql> use aa;
    mysql> select * from test;
    +------+
    | id   |
    +------+
    |    1 |
    +------+

    (5)、在manager服务器上修改配置文件(再把这个记录添加进去,因为它检测掉失效时候会自动消失)

    vi /etc/masterha/app1.cnf
    ......
    [server1]
    hostname=192.168.8.134
    port=3306
    ......

    (6)、在manager服务器上启动manager

    masterha_check_ssh -conf=/etc/masterha/app1.cnf
    masterha_check_repl -conf=/etc/masterha/app1.cnf
    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 &
    masterha_check_status --conf=/etc/masterha/app1.cnf

  • 相关阅读:
    HDU 3848 CC On The Tree 树形DP
    编程求取直线一般式表达式,两直线交点
    向外国学者所要论文源代码--英语模版
    找出该树中第二小的值--思路及算法实现
    不使用额外空间交换2个数据的源代码
    华为2018软件岗笔试题解题思路和源代码分享
    华为笔试题--LISP括号匹配 解析及源码实现
    Linux 快捷键汇总(偏基础)
    快速排序算法思路分析和C++源代码(递归和非递归)
    Python读取SQLite文件数据
  • 原文地址:https://www.cnblogs.com/Xing88/p/13934809.html
Copyright © 2011-2022 走看看