mysql5.6基于GTID模式之高可用架构搭建-MHA(mha0.56)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql5.6基于GTID模式之高可用架构搭建-MHA(mha0.56)相关的知识,希望对你有一定的参考价值。

一、测试环境部署:

mysql1:192.168.110.131   作为master

mysql2:192.168.110.132   作为slave

mysql3:192.168.110.130   作为slave,同时作为MHA的管理机

虚拟IP:192.168.110.100

二、mysql主从环境搭建和MHA安装

1、mysql主从搭建自行搭建(基于GTID复制,打开log_bin,复制规则默认,复制所有库表),这里不再说明。

2、安装MHA节点软件:rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

3、安装MHA管理节点:rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

    yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y

4、配置MHA管理目录和配置:

mkdir -p /etc/masterha   #工作目录

mkdir -p /var/log/masterha/app1/ #日志目录

cat /etc/masterha/app1.cnf #配置文件

[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_ip_failover_script="/usr/bin/master_ip_failover"
ping_interval=1
remote_workdir=/tmp
repl_password=repl
repl_user=repl
secondary_check_script="/usr/bin/masterha_secondary_check -s 192.168.110.132 -s 192.168.110.130 --user=root --master_host=mysql1 --master_ip=192.168.110.131 --master_port=3306"
ssh_user=root
user=mha
password=mha

[server1]
hostname=mysql1
master_binlog_dir="/data/mysql/mysql3306/binlog"
port=3306

[server2]
hostname=mysql2
master_binlog_dir="/data/mysql/mysql3306/binlog"
port=3306
candidate_master=1
check_repl_delay=0

[server3]
hostname=ansible
master_binlog_dir="/data/mysql/mysql3306/binlog"
port=3306

[binlog1]
hostname=mysql2
master_binlog_dir=/tmp/binlog
ignore_fail=1
no_master=1


5、配置MHA管理数据库账号mha:

mysql>GRANT ALL PRIVILEGES ON *.* TO ‘mha‘@‘192.168.110.%‘ IDENTIFIED BY ‘mha‘;

6、配置主库的binlog实时备份,脚本部署在mysql2上(非必须,如无,请去掉app1.cnf里面的[binlog1]配置信息):


cat binlog.sh

#!/bin/sh
BACKUP_BIN="/usr/bin/mysqlbinlog"
LOCAL_BACKUP_DIR="/tmp/binlog"
BACKUP_LOG="/tmp/binlog/binary.log"

REMOTE_HOST="192.168.110.131"
REMOTE_PORT="3306"
REMOTE_USER="root"
REMOTE_PASS="root"
FIRST_BINLOG="mysql-bin.000001"

#time to wait before reconnecting after failure
SLEEP_SECONDS=10

##create local_backup_dir if necessary
mkdir -p ${LOCAL_BACKUP_DIR}
cd ${LOCAL_BACKUP_DIR}

## 运行while循环,连接断开后等待指定时间,重新连接
while :
do
  if [ `ls -A "${LOCAL_BACKUP_DIR}" |wc -l` -eq 0 ];then
     LAST_FILE=${FIRST_BINLOG}
  else
     LAST_FILE=`ls -l ${LOCAL_BACKUP_DIR} | grep -v backuplog | grep mysql-bin |tail -n 1 |awk ‘{print $9}‘`
  fi
  ${BACKUP_BIN} --raw --read-from-remote-server --stop-never --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASS} ${LAST_FILE}

  echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止,返回代码:$?" | tee -a ${BACKUP_LOG}
  echo "${SLEEP_SECONDS}秒后再次连接并继续备份" | tee -a ${BACKUP_LOG}  
  sleep ${SLEEP_SECONDS}
done

7、修改/usr/bin/master_ip_failover脚本,这里采用脚本切换VIP的方式实现VIP漂移。

#!/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 $gateway = ‘192.168.110.2‘;
my $vip = ‘192.168.110.100‘;  # Virtual IP
my $key = "2";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip;/sbin/arping -I eth0 -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
$ssh_user = "root";
 
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" ) {
 
        # $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 "Disabling the VIP on old master: $orig_master_host \n";
        #    &stop_vip();
        #    $exit_code = 0;
        #};
 
 
        eval {
                print "Disabling the VIP on old master: $orig_master_host \n";
                #my $ping=`ping -c 1 10.0.0.13 | grep "packet loss" | awk -F‘,‘ ‘{print $3}‘ | awk ‘{print $1}‘`;
                #if ( $ping le "90.0%" && $ping gt "0.0%" ){
                #$exit_code = 0;
                #}
                #else {
 
                &stop_vip();
 
                # updating global catalog, etc
                $exit_code = 0;
 
                #}
        };
 
 
        if ([email protected]) {
            warn "Got Error: [email protected]\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 "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ([email protected]) {
            warn [email protected];
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        `ssh $ssh_user\@$orig_master_ip \" $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 $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";
}
 
# the end.


8、配置SSH免密登陆:(以ansible为例)

ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

基本上MHA manager,MNA node,以及二次检测的节点,都需要互相信任

注:这里配置免密的时候采用主机名,那在app1.cnf里的hostname对应也要配置成主机名。如果是IP,那就配置成IP。


9、配置sysbench压测工具,模拟生产对数据库进行压测。

安装sysbench请自行百度,这里只提供压测脚本。

mysql>grant all privileges on sbtest1.* on [email protected]‘192.168.110.%‘ identified by ‘root‘;


vim sysbench.sh


#!/bin/sh


function prepare(){
sysbench --test=oltp --mysql-host=192.168.110.100 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=10  --max-requests=0 --oltp-test-mode=nontrx --oltp-nontrx-mode=select --oltp-read-only=off --max-time=30 --num-threads=10 prepare
}

function run(){
sysbench --test=oltp --mysql-host=192.168.110.100 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=10  --max-requests=0 --oltp-test-mode=nontrx --oltp-nontrx-mode=select --oltp-read-only=off --max-time=30 --num-threads=10 run
}

function cleanup(){
sysbench --test=oltp --mysql-host=192.168.110.100 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=sbtest1 --oltp-num-tables=10 --oltp-table-size=500000 --report-interval=10  --max-requests=0 --oltp-test-mode=nontrx --oltp-nontrx-mode=select --oltp-read-only=off --max-time=30 --num-threads=10 cleanup
}

case "$1" in
prepare)
prepare
;;
run)
run
;;
cleanup)
cleanup
;;
*)
echo "Usage: $0 prepare|run|cleanup"
esac


脚本对VIP所在的mysql即主库进行压测。


10、检测SSH状态:

技术分享

11、检测复制状态:

[[email protected] app1]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Tue Mar 14 17:29:38 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Mar 14 17:29:38 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Mar 14 17:29:38 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Mar 14 17:29:38 2017 - [info] MHA::MasterMonitor version 0.56.
Tue Mar 14 17:29:38 2017 - [info] GTID failover mode = 0
Tue Mar 14 17:29:38 2017 - [info] Dead Servers:
Tue Mar 14 17:29:38 2017 - [info] Alive Servers:
Tue Mar 14 17:29:38 2017 - [info]   mysql1(192.168.110.131:3306)
Tue Mar 14 17:29:38 2017 - [info]   mysql2(192.168.110.132:3306)
Tue Mar 14 17:29:38 2017 - [info]   ansible(192.168.110.130:3306)
Tue Mar 14 17:29:38 2017 - [info] Alive Slaves:
Tue Mar 14 17:29:38 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 17:29:38 2017 - [info]     GTID ON
Tue Mar 14 17:29:38 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 17:29:38 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 17:29:38 2017 - [info]   ansible(192.168.110.130:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 17:29:38 2017 - [info]     GTID ON
Tue Mar 14 17:29:38 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 17:29:38 2017 - [info] Current Alive Master: mysql1(192.168.110.131:3306)
Tue Mar 14 17:29:38 2017 - [info] Checking slave configurations..
Tue Mar 14 17:29:38 2017 - [info]  read_only=1 is not set on slave mysql2(192.168.110.132:3306).
Tue Mar 14 17:29:38 2017 - [info]  read_only=1 is not set on slave ansible(192.168.110.130:3306).
Tue Mar 14 17:29:38 2017 - [info] Checking replication filtering settings..
Tue Mar 14 17:29:38 2017 - [info]  binlog_do_db= , binlog_ignore_db=
Tue Mar 14 17:29:38 2017 - [info]  Replication filtering check ok.
Tue Mar 14 17:29:38 2017 - [info] GTID (with auto-pos) is not supported
Tue Mar 14 17:29:38 2017 - [info] Starting SSH connection tests..
Tue Mar 14 17:29:40 2017 - [info] All SSH connection tests passed successfully.
Tue Mar 14 17:29:40 2017 - [info] Checking MHA Node version..
Tue Mar 14 17:29:40 2017 - [info]  Version check ok.
Tue Mar 14 17:29:40 2017 - [info] Checking SSH publickey authentication settings on the current master..
Tue Mar 14 17:29:40 2017 - [info] HealthCheck: SSH to mysql1 is reachable.
Tue Mar 14 17:29:40 2017 - [info] Checking recovery script configurations on mysql1(192.168.110.131:3306)..
Tue Mar 14 17:29:40 2017 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/mysql3306/binlog --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000001
Tue Mar 14 17:29:40 2017 - [info]   Connecting to [email protected](mysql1:22)..
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /data/mysql/mysql3306/binlog, up to mysql-bin.000001
Tue Mar 14 17:29:41 2017 - [info] Binlog setting check done.
Tue Mar 14 17:29:41 2017 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Tue Mar 14 17:29:41 2017 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user=‘mha‘ --slave_host=mysql2 --slave_ip=192.168.110.132 --slave_port=3306 --workdir=/tmp --target_version=5.6.34-log --manager_version=0.56 --relay_dir=/data/mysql/mysql3306/relaylog --current_relay_log=slave-relay-bin.000002  --slave_pass=xxx
Tue Mar 14 17:29:41 2017 - [info]   Connecting to [email protected](mysql2:22)..
  Checking slave recovery environment settings..
    Relay log found at /data/mysql/mysql3306/relaylog, up to slave-relay-bin.000002
    Temporary relay log file is /data/mysql/mysql3306/relaylog/slave-relay-bin.000002
    Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Tue Mar 14 17:29:42 2017 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user=‘mha‘ --slave_host=ansible --slave_ip=192.168.110.130 --slave_port=3306 --workdir=/tmp --target_version=5.6.34-log --manager_version=0.56 --relay_dir=/data/mysql/mysql3306/relaylog --current_relay_log=slave-relay-bin.000002  --slave_pass=xxx
Tue Mar 14 17:29:42 2017 - [info]   Connecting to [email protected](ansible:22)..
  Checking slave recovery environment settings..
    Relay log found at /data/mysql/mysql3306/relaylog, up to slave-relay-bin.000002
    Temporary relay log file is /data/mysql/mysql3306/relaylog/slave-relay-bin.000002
    Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Tue Mar 14 17:29:42 2017 - [info] Slaves settings check done.
Tue Mar 14 17:29:42 2017 - [info]
mysql1(192.168.110.131:3306) (current master)
 +--mysql2(192.168.110.132:3306)
 +--ansible(192.168.110.130:3306)

Tue Mar 14 17:29:42 2017 - [info] Checking replication health on mysql2..
Tue Mar 14 17:29:42 2017 - [info]  ok.
Tue Mar 14 17:29:42 2017 - [info] Checking replication health on ansible..
Tue Mar 14 17:29:42 2017 - [info]  ok.
Tue Mar 14 17:29:42 2017 - [info] Checking master_ip_failover_script status:
Tue Mar 14 17:29:42 2017 - [info]   /usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=mysql1 --orig_master_ip=192.168.110.131 --orig_master_port=3306


IN SCRIPT TEST====/sbin/ifconfig eth0:2 down==/sbin/ifconfig eth0:2 192.168.110.100;/sbin/arping -I eth0 -c 3 -s 192.168.110.100 192.168.110.2 >/dev/null 2>&1===

Checking the Status of the script.. OK
Tue Mar 14 17:29:45 2017 - [info]  OK.
Tue Mar 14 17:29:45 2017 - [warning] shutdown_script is not defined.
Tue Mar 14 17:29:45 2017 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.


13、启动、关闭和检测manager状态:

vim masterha_manager.sh


#!/bin/sh

function start(){
nohup /usr/bin/masterha_manager --conf=/etc/masterha/app1.cnf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
}

function stop(){
/usr/bin/masterha_stop --conf=/etc/masterha/app1.cnf
}

function status(){
/usr/bin/masterha_check_status --conf=/etc/masterha/app1.cnf
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo "Usage: $0 start|stop"
esac


14、初始化VIP:

首次启用MHA,需要提前手工配置VIP在mysql主库上。

shell>/sbin/ifconfig eth0:2 192.168.110.100


15、场景测试:

  • 启动manager进程:sh masterha_manager.sh start

  • 启动压测脚本:sh sysbench.sh prepare

  • 中断mysql2的slave线程,以造成mysql2有大量延迟:mysql>stop slave;

  • 压测一段时间

  • 启动mysql2的slave线程,检查slave状态。并可以强行杀掉主库进程(pkill -9 mysql),即使sysbench.sh还未跑完的时候

  • 观察此时MHA manager进程状态和日志信息。

  • 检验VIP是否切换,检查主库是否切换,从库是否从新主库那里复制。


注:即使在配置文件中配置mysql2为candidate_master=1和check_repl_delay=0,但是mysql2延迟量太大的话,MHA还是会选择拥有最新数据的slave作为master。


日志如下:

[[email protected] app1]# cat manager.log
Tue Mar 14 16:25:27 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Mar 14 16:25:27 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Mar 14 16:25:27 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Mar 14 16:26:28 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Mar 14 16:26:28 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Mar 14 16:26:28 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
ive Servers:
Tue Mar 14 16:25:28 2017 - [info]   mysql1(192.168.110.131:3306)
Tue Mar 14 16:25:28 2017 - [info]   mysql2(192.168.110.132:3306)
Tue Mar 14 16:25:28 2017 - [info]   ansible(192.168.110.130:3306)
Tue Mar 14 16:25:28 2017 - [info] Alive Slaves:
Tue Mar 14 16:25:28 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:25:28 2017 - [info]     GTID ON
Tue Mar 14 16:25:28 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:25:28 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 16:25:28 2017 - [info]   ansible(192.168.110.130:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:25:28 2017 - [info]     GTID ON
Tue Mar 14 16:25:28 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:25:28 2017 - [info] Current Alive Master: mysql1(192.168.110.131:3306)
Tue Mar 14 16:25:28 2017 - [info] Checking slave configurations..
Tue Mar 14 16:25:28 2017 - [info]  read_only=1 is not set on slave mysql2(192.168.110.132:3306).
Tue Mar 14 16:25:28 2017 - [info]  read_only=1 is not set on slave ansible(192.168.110.130:3306).
Tue Mar 14 16:25:28 2017 - [info] Checking replication filtering settings..
Tue Mar 14 16:25:28 2017 - [info]  binlog_do_db= , binlog_ignore_db=
Tue Mar 14 16:25:28 2017 - [info]  Replication filtering check ok.
Tue Mar 14 16:25:28 2017 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Tue Mar 14 16:25:29 2017 - [info] HealthCheck: SSH to mysql2 is reachable.
Tue Mar 14 16:25:29 2017 - [info] Binlog server mysql2 is reachable.
Tue Mar 14 16:25:29 2017 - [info] Checking recovery script configurations on mysql2(192.168.110.132:3306)..
Tue Mar 14 16:25:29 2017 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/tmp/binlog --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000001
Tue Mar 14 16:25:29 2017 - [info]   Connecting to [email protected](mysql2:22)..
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /tmp/binlog, up to mysql-bin.000015
Tue Mar 14 16:25:29 2017 - [info] Binlog setting check done.
Tue Mar 14 16:25:29 2017 - [info] Checking SSH publickey authentication settings on the current master..
Tue Mar 14 16:25:34 2017 - [warning] HealthCheck: Got timeout on checking SSH connection to mysql1! at /usr/share/perl5/vendor_perl/MHA/HealthCheck.pm line 342.
Tue Mar 14 16:25:34 2017 - [info]
mysql1(192.168.110.131:3306) (current master)
 +--mysql2(192.168.110.132:3306)
 +--ansible(192.168.110.130:3306)

Tue Mar 14 16:25:34 2017 - [info] Checking master_ip_failover_script status:
Tue Mar 14 16:25:34 2017 - [info]   /usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=mysql1 --orig_master_ip=192.168.110.131 --orig_master_port=3306


IN SCRIPT TEST====/sbin/ifconfig eth0:2 down==/sbin/ifconfig eth0:2 192.168.110.100;/sbin/arping -I eth0 -c 3 -s 192.168.110.100 192.168.110.2 >/dev/null 2>&1===

Checking the Status of the script.. OK
Tue Mar 14 16:25:49 2017 - [info]  OK.
Tue Mar 14 16:25:49 2017 - [warning] shutdown_script is not defined.
Tue Mar 14 16:25:49 2017 - [info] Set master ping interval 1 seconds.
Tue Mar 14 16:25:49 2017 - [info] Set secondary check script: /usr/bin/masterha_secondary_check -s 192.168.110.132 -s 192.168.110.130 --user=root --master_host=mysql1 --master_ip=192.168.110.131 --master_port=3306
Tue Mar 14 16:25:49 2017 - [info] Starting ping health check on mysql1(192.168.110.131:3306)..
Tue Mar 14 16:25:49 2017 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn‘t respond..
Tue Mar 14 16:26:25 2017 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Tue Mar 14 16:26:25 2017 - [info] Executing SSH check script: exit 0
Tue Mar 14 16:26:25 2017 - [info] Executing secondary network check script: /usr/bin/masterha_secondary_check -s 192.168.110.132 -s 192.168.110.130 --user=root --master_host=mysql1 --master_ip=192.168.110.131 --master_port=3306  --user=root  --master_host=mysql1  --master_ip=192.168.110.131  --master_port=3306 --master_user=mha --master_password=mha --ping_type=SELECT
Monitoring server 192.168.110.132 is reachable, Master is not reachable from 192.168.110.132. OK.
Tue Mar 14 16:26:26 2017 - [info] HealthCheck: SSH to mysql1 is reachable.
Monitoring server 192.168.110.130 is reachable, Master is not reachable from 192.168.110.130. OK.
Tue Mar 14 16:26:26 2017 - [info] Master is not reachable from all other monitoring servers. Failover should start.
Tue Mar 14 16:26:26 2017 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at ‘reading initial communication packet‘, system error: 111)
Tue Mar 14 16:26:26 2017 - [warning] Connection failed 2 time(s)..
Tue Mar 14 16:26:27 2017 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at ‘reading initial communication packet‘, system error: 111)
Tue Mar 14 16:26:27 2017 - [warning] Connection failed 3 time(s)..
Tue Mar 14 16:26:28 2017 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at ‘reading initial communication packet‘, system error: 111)
Tue Mar 14 16:26:28 2017 - [warning] Connection failed 4 time(s)..
Tue Mar 14 16:26:28 2017 - [warning] Master is not reachable from health checker!
Tue Mar 14 16:26:28 2017 - [warning] Master mysql1(192.168.110.131:3306) is not reachable!
Tue Mar 14 16:26:28 2017 - [warning] SSH is reachable.
Tue Mar 14 16:26:28 2017 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/masterha/app1.cnf again, and trying to connect to all servers to check server status..
Tue Mar 14 16:26:28 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Mar 14 16:26:28 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Mar 14 16:26:28 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Mar 14 16:26:28 2017 - [info] GTID failover mode = 1
Tue Mar 14 16:26:28 2017 - [info] Dead Servers:
Tue Mar 14 16:26:28 2017 - [info]   mysql1(192.168.110.131:3306)
Tue Mar 14 16:26:28 2017 - [info] Alive Servers:
Tue Mar 14 16:26:28 2017 - [info]   mysql2(192.168.110.132:3306)
Tue Mar 14 16:26:28 2017 - [info]   ansible(192.168.110.130:3306)
Tue Mar 14 16:26:28 2017 - [info] Alive Slaves:
Tue Mar 14 16:26:28 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:28 2017 - [info]     GTID ON
Tue Mar 14 16:26:28 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:28 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 16:26:28 2017 - [info]   ansible(192.168.110.130:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:28 2017 - [info]     GTID ON
Tue Mar 14 16:26:28 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:28 2017 - [info] Checking slave configurations..
Tue Mar 14 16:26:28 2017 - [info]  read_only=1 is not set on slave mysql2(192.168.110.132:3306).
Tue Mar 14 16:26:28 2017 - [info]  read_only=1 is not set on slave ansible(192.168.110.130:3306).
Tue Mar 14 16:26:28 2017 - [info] Checking replication filtering settings..
Tue Mar 14 16:26:28 2017 - [info]  Replication filtering check ok.
Tue Mar 14 16:26:28 2017 - [info] Master is down!
Tue Mar 14 16:26:28 2017 - [info] Terminating monitoring script.
Tue Mar 14 16:26:28 2017 - [info] Got exit code 20 (Master dead).
Tue Mar 14 16:26:28 2017 - [info] MHA::MasterFailover version 0.56.
Tue Mar 14 16:26:28 2017 - [info] Starting master failover.
Tue Mar 14 16:26:28 2017 - [info]
Tue Mar 14 16:26:28 2017 - [info] * Phase 1: Configuration Check Phase..
Tue Mar 14 16:26:28 2017 - [info]
Tue Mar 14 16:26:28 2017 - [info] HealthCheck: SSH to mysql2 is reachable.
Tue Mar 14 16:26:28 2017 - [info] Binlog server mysql2 is reachable.
Tue Mar 14 16:26:29 2017 - [info] GTID failover mode = 1
Tue Mar 14 16:26:29 2017 - [info] Dead Servers:
Tue Mar 14 16:26:29 2017 - [info]   mysql1(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info] Checking master reachability via MySQL(double check)...
Tue Mar 14 16:26:29 2017 - [info]  ok.
Tue Mar 14 16:26:29 2017 - [info] Alive Servers:
Tue Mar 14 16:26:29 2017 - [info]   mysql2(192.168.110.132:3306)
Tue Mar 14 16:26:29 2017 - [info]   ansible(192.168.110.130:3306)
Tue Mar 14 16:26:29 2017 - [info] Alive Slaves:
Tue Mar 14 16:26:29 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:29 2017 - [info]     GTID ON
Tue Mar 14 16:26:29 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 16:26:29 2017 - [info]   ansible(192.168.110.130:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:29 2017 - [info]     GTID ON
Tue Mar 14 16:26:29 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info] Starting GTID based failover.
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] * Phase 2: Dead Master Shutdown Phase..
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] Forcing shutdown so that applications never connect to the current master..
Tue Mar 14 16:26:29 2017 - [info] Executing master IP deactivation script:
Tue Mar 14 16:26:29 2017 - [info]   /usr/bin/master_ip_failover --orig_master_host=mysql1 --orig_master_ip=192.168.110.131 --orig_master_port=3306 --command=stopssh --ssh_user=root  


IN SCRIPT TEST====/sbin/ifconfig eth0:2 down==/sbin/ifconfig eth0:2 192.168.110.100;/sbin/arping -I eth0 -c 3 -s 192.168.110.100 192.168.110.2 >/dev/null 2>&1===

Disabling the VIP on old master: mysql1
Tue Mar 14 16:26:29 2017 - [info]  done.
Tue Mar 14 16:26:29 2017 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Tue Mar 14 16:26:29 2017 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] * Phase 3: Master Recovery Phase..
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] The latest binary log file/position on all slaves is mysql-bin.000001:133309374
Tue Mar 14 16:26:29 2017 - [info] Retrieved Gtid Set: c897448e-07c8-11e7-8af3-000c29918f29:1-244
Tue Mar 14 16:26:29 2017 - [info] Latest slaves (Slaves that received relay log files to the latest):
Tue Mar 14 16:26:29 2017 - [info]   ansible(192.168.110.130:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:29 2017 - [info]     GTID ON
Tue Mar 14 16:26:29 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info] The oldest binary log file/position on all slaves is mysql-bin.000001:31201438
Tue Mar 14 16:26:29 2017 - [info] Retrieved Gtid Set: c897448e-07c8-11e7-8af3-000c29918f29:1-81
Tue Mar 14 16:26:29 2017 - [info] Oldest slaves:
Tue Mar 14 16:26:29 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:29 2017 - [info]     GTID ON
Tue Mar 14 16:26:29 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] * Phase 3.3: Determining New Master Phase..
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [warning]  Slave mysql2(192.168.110.132:3306) SQL Thread delays too much. Latest log file:mysql-bin.000001:133309374, Current log file:mysql-bin.000001:31152313. This server is not selected as a new master because recovery will take long time.
Tue Mar 14 16:26:29 2017 - [info] Searching new master from slaves..
Tue Mar 14 16:26:29 2017 - [info]  Candidate masters from the configuration file:
Tue Mar 14 16:26:29 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:29 2017 - [info]     GTID ON
Tue Mar 14 16:26:29 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 16:26:29 2017 - [info]  Non-candidate masters:
Tue Mar 14 16:26:29 2017 - [info]   mysql2(192.168.110.132:3306)  Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Tue Mar 14 16:26:29 2017 - [info]     GTID ON
Tue Mar 14 16:26:29 2017 - [info]     Replicating from 192.168.110.131(192.168.110.131:3306)
Tue Mar 14 16:26:29 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Mar 14 16:26:29 2017 - [info]  Searching from candidate_master slaves which have received the latest relay log events..
Tue Mar 14 16:26:29 2017 - [info]   Not found.
Tue Mar 14 16:26:29 2017 - [info]  Searching from all candidate_master slaves..
Tue Mar 14 16:26:29 2017 - [info]   Not found.
Tue Mar 14 16:26:29 2017 - [info]  Searching from all slaves which have received the latest relay log events..
Tue Mar 14 16:26:29 2017 - [info] New master is ansible(192.168.110.130:3306)
Tue Mar 14 16:26:29 2017 - [info] Starting master failover..
Tue Mar 14 16:26:29 2017 - [info]
From:
mysql1(192.168.110.131:3306) (current master)
 +--mysql2(192.168.110.132:3306)
 +--ansible(192.168.110.130:3306)

To:
ansible(192.168.110.130:3306) (new master)
 +--mysql2(192.168.110.132:3306)
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] * Phase 3.3: New Master Recovery Phase..
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info]  Waiting all logs to be applied..
Tue Mar 14 16:26:29 2017 - [info]   done.
Tue Mar 14 16:26:29 2017 - [info] -- Saving binlog from host mysql2 started, pid: 39734
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] Log messages from mysql2 ...
Tue Mar 14 16:26:29 2017 - [info]
Tue Mar 14 16:26:29 2017 - [info] Fetching binary logs from binlog server mysql2..
Tue Mar 14 16:26:29 2017 - [info] Executing binlog save command: save_binary_logs --command=save --start_file=mysql-bin.000001  --start_pos=133309374 --output_file=/tmp/saved_binlog_binlog1_20170314162628.binlog --handle_raw_binlog=0 --skip_filter=1 --disable_log_bin=0 --manager_version=0.56 --oldest_version=5.6.34-log  --binlog_dir=/tmp/binlog
  Creating /tmp if not exists..    ok.
 Concat binary/relay logs from mysql-bin.000001 pos 133309374 to mysql-bin.000015 EOF into /tmp/saved_binlog_binlog1_20170314162628.binlog ..
Target file /tmp/binlog/mysql-bin.000001 size=92195648, this is smaller than exec pos 133309374. Skipping.
Event not exists.
Tue Mar 14 16:26:29 2017 - [info] Additional events were not found from the binlog server. No need to save.
Tue Mar 14 16:26:29 2017 - [info] End of log messages from mysql2.
Tue Mar 14 16:26:29 2017 - [info] No binlog events found from mysql2. Skipping
Tue Mar 14 16:26:29 2017 - [info] Getting new master‘s binlog name and position..
Tue Mar 14 16:26:29 2017 - [info]  mysql-bin.000001:133306806
Tue Mar 14 16:26:29 2017 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST=‘ansible or 192.168.110.130‘, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘xxx‘;
Tue Mar 14 16:26:29 2017 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: mysql-bin.000001, 133306806, c897448e-07c8-11e7-8af3-000c29918f29:1-244
Tue Mar 14 16:26:29 2017 - [info] Executing master IP activate script:
Tue Mar 14 16:26:29 2017 - [info]   /usr/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=mysql1 --orig_master_ip=192.168.110.131 --orig_master_port=3306 --new_master_host=ansible --new_master_ip=192.168.110.130 --new_master_port=3306 --new_master_user=‘mha‘ --new_master_password=‘mha‘  
Unknown option: new_master_user
Unknown option: new_master_password


IN SCRIPT TEST====/sbin/ifconfig eth0:2 down==/sbin/ifconfig eth0:2 192.168.110.100;/sbin/arping -I eth0 -c 3 -s 192.168.110.100 192.168.110.2 >/dev/null 2>&1===

Enabling the VIP - 192.168.110.100 on the new master - ansible
Tue Mar 14 16:26:32 2017 - [info]  OK.
Tue Mar 14 16:26:33 2017 - [info] ** Finished master recovery successfully.
Tue Mar 14 16:26:33 2017 - [info] * Phase 3: Master Recovery Phase completed.
Tue Mar 14 16:26:33 2017 - [info]
Tue Mar 14 16:26:33 2017 - [info] * Phase 4: Slaves Recovery Phase..
Tue Mar 14 16:26:33 2017 - [info]
Tue Mar 14 16:26:33 2017 - [info]
Tue Mar 14 16:26:33 2017 - [info] * Phase 4.1: Starting Slaves in parallel..
Tue Mar 14 16:26:33 2017 - [info]
Tue Mar 14 16:26:33 2017 - [info] -- Slave recovery on host mysql2(192.168.110.132:3306) started, pid: 39751. Check tmp log /var/log/masterha/app1/mysql2_3306_20170314162628.log if it takes time..
Tue Mar 14 16:27:14 2017 - [info]
Tue Mar 14 16:27:14 2017 - [info] Log messages from mysql2 ...
Tue Mar 14 16:27:14 2017 - [info]
Tue Mar 14 16:26:33 2017 - [info]  Resetting slave mysql2(192.168.110.132:3306) and starting replication from the new master ansible(192.168.110.130:3306)..
Tue Mar 14 16:26:33 2017 - [info]  Executed CHANGE MASTER.
Tue Mar 14 16:26:33 2017 - [info]  Slave started.
Tue Mar 14 16:27:14 2017 - [info]  gtid_wait(c897448e-07c8-11e7-8af3-000c29918f29:1-244) completed on mysql2(192.168.110.132:3306). Executed 168 events.
Tue Mar 14 16:27:14 2017 - [info] End of log messages from mysql2.
Tue Mar 14 16:27:14 2017 - [info] -- Slave on host mysql2(192.168.110.132:3306) started.
Tue Mar 14 16:27:14 2017 - [info] All new slave servers recovered successfully.
Tue Mar 14 16:27:14 2017 - [info]
Tue Mar 14 16:27:14 2017 - [info] * Phase 5: New master cleanup phase..
Tue Mar 14 16:27:14 2017 - [info]
Tue Mar 14 16:27:14 2017 - [info] Resetting slave info on the new master..
Tue Mar 14 16:27:15 2017 - [info]  ansible: Resetting slave info succeeded.
Tue Mar 14 16:27:15 2017 - [info] Master failover to ansible(192.168.110.130:3306) completed successfully.
Tue Mar 14 16:27:15 2017 - [info]

----- Failover Report -----

app1: MySQL Master failover mysql1(192.168.110.131:3306) to ansible(192.168.110.130:3306) succeeded

Master mysql1(192.168.110.131:3306) is down!

Check MHA Manager logs at ansible:/var/log/masterha/app1/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on mysql1(192.168.110.131:3306)
Selected ansible(192.168.110.130:3306) as a new master.
ansible(192.168.110.130:3306): OK: Applying all logs succeeded.
ansible(192.168.110.130:3306): OK: Activated master IP address.
mysql2(192.168.110.132:3306): OK: Slave started, replicating from ansible(192.168.110.130:3306)
ansible(192.168.110.130:3306): Resetting slave info succeeded.
Master failover to ansible(192.168.110.130:3306) completed successfully.




本文出自 “厚积薄发” 博客,请务必保留此出处http://1057212.blog.51cto.com/1047212/1906434

以上是关于mysql5.6基于GTID模式之高可用架构搭建-MHA(mha0.56)的主要内容,如果未能解决你的问题,请参考以下文章

Centos7搭建基于GTID的MySQL主从复制架构

MySQL5.7在线开启/关闭GTID

MySQL5.6 GTID新特性实践

mysql集群-基于GTID的主从复制

GTID主从复制 MHA高可用 binlogserver实时备份 Atlas中间件读写分离

使用innobackup实现 基于GTID的从库搭建