MHA的高可用及故障切换

一、MHA 概述

  • 一套优秀的MySQL 高可用环境下故障切换和主从复制的软件
  • MySQL故障过程当中,MHA 能做到0~30秒内自动完成故障切换

MHA 组成

  • MHA Manger(管理节点)
  • MHA Node (数据节点)

MHA 特点

  • 自动故障切换过程中,MHA 试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
  • 使用半同步复制,可以大大减少数据丢失的风险
  • 目前MHA 支持一主多从架构,最少 三台服务器 即一主两从

二、MHA 高可用及故障切换实验

2.1 实验环境

  • 四台CentOS系统的虚拟机充当服务器安装
  • 其中 一台充当数据库主服务器,一台充当数据库从/主备服务器,一台充当从服务器 安装MySQL 5.6.36
  • 一台充当MHA 监控管理服务器

在这里插入图片描述

2.2 数据库5.6.36的安装

  • 三台数据库服务器都需要安装 准备好mysql-5.6.36.tar.gz 压缩包
1、安装编译依赖的环境
[root@mysql1 ~]# yum -y install ncurses-devel gcc-c++ perl-Module-Install

2、安装gmake 编译软件
[root@mysql1 ~]# tar zxvf cmake-2.8.6.tar.gz 
[root@mysql1 ~]# cd cmake-2.8.6/
[root@mysql1 cmake-2.8.6]# ./configure 
[root@mysql1 cmake-2.8.6]# gmake && gmake install 
3、安装Mysql数据库

[root@mysql1 ~]# tar zxvf mysql-5.6.36.tar.gz 
[root@mysql1 ~]# cd mysql-5.6.36/
[root@mysql1 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
[root@mysql1 mysql-5.6.36]# make && make install
[root@mysql1 mysql-5.6.36]# cp support-files/my-default.cnf  /etc/my.cnf
[root@mysql1 mysql-5.6.36]# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
[root@mysql1 ~]# chmod +x /etc/rc.d/init.d/mysqld 
[root@mysql1 ~]# chkconfig --add mysqld
[root@mysql1 ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql1 ~]# source /etc/profile
[root@mysql1 ~]# groupadd mysql
[root@mysql1 ~]# useradd -M -s /sbin/nologin mysql -g mysql
[root@mysql1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@mysql1 ~]# mkdir -p /data/mysql
[root@mysql1 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

2.3 主从复制配置

4、修改Master 的主服务器配置文件  三台服务器的server-id 不能一样
主服务器修改
[root@mysql1 ~]# vim /etc/my.cnf
server-id = 1
log_bin = master-bin
log-slave-updates = true

两台从服务器修改
[root@mysql2 mysql-5.6.36]# vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
5、三台服务器分别做两个软连接
[root@mysql1 ~]# ln -s /usr//local/mysql/bin/mysql /usr/sbin/
[root@mysql1 ~]# ln -s /usr//local/mysql/bin/mysqlbinlog /usr/sbin/

6、三台服务器启动MySQL
[root@mysql1 bin]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[root@mysql1 bin]# netstat -atnp | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      21159/mysqld 

7、 配置 Mysql  一主两从
 (1)、所有数据库节点都需要授权这两个用户
两个授权用户  一个是从库同步使用,一个是manager使用
[root@mysql1 ~]# mysql -uroot -p  ## 此时没有设置密码  直接回车登录数据库
mysql> grant replication slave on *.* to 'myslave'@'192.168.233.%' identified by '123';
mysql> grant all privileges on *.* to 'mha'@'192.168.233.%' identified by 'manager';
mysql> flush privileges;
(2)、 下面三条理论不同添加,但是案例实验环境通过MHA检查MySQL 主从有报错,
报错信息两个从数据库连接不上主库,所以所有数据库加上下面授权
mysql> grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';  ## 注意  后面跟的是主机名  主机名不区分大小写,系统会自动识别
mysql> grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';

(3)、主服务器查看二进制文件和同步点
mysql> show master status;  ## 这时候主服务器一定要停止操作
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1294 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

(4)、 两台从服务器分别执行同步
mysql> change master to master_host='192.168.233.100',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1294;
mysql> start slave;
(5)、 查看IO和SQL 线程
mysql> show slave status\G;
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
* 必须设置两个从库为只读模式
mysql> set global read_only=1;
mysql> flush privileges;


2.4 MHA Node (数据节点) 安装

  • 四台都要安装 包括MHA的服务区
1、所有服务器都安装MHA依赖的环境,首先安装epel源
[root@mysql2 mysql-5.6.36]# yum -y install epel-release --nogpgcheck -y   ## 先安装epel源 才能装下面的,否则有些找不到
[root@mysql1 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

2、 MHA 软件包对于每个操作系统版本不一样,这里 用0.57版本
注意:所有服务器 上必须先安装node组件,最后再MHA-manager 节点上安装manager组件,
因为manager依赖node组件,下面是在mysql1 上安装node组件
[root@mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz 
[root@mysql1 ~]# cd mha4mysql-node-0.57/
[root@mysql1 mha4mysql-node-0.57]# perl Makefile.PL 
root@mysql3 mha4mysql-node-0.57]# make
[root@mysql3 mha4mysql-node-0.57]# make install

2.5 在MHA-manager 上安装manager组件

  • 注意 一定要先安装node组件才能安装manager组件
[root@mha-manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz 
[root@mha-manager ~]# cd mha4mysql-manager-0.57/
[root@mha-manager mha4mysql-manager-0.57]# perl Makefile.PL 

## 安装后 进入/usr/local/bin/  下面 会生成几个工具,主要包括以下几个

[root@mha-manager mha4mysql-manager-0.57]# cd /usr/local/bin/
[root@mha-manager bin]# ll
总用量 84
-r-xr-xr-x. 1 root root 16381 8月  28 01:34 apply_diff_relay_logs
-r-xr-xr-x. 1 root root  4807 8月  28 01:34 filter_mysqlbinlog
-r-xr-xr-x. 1 root root  1995 8月  28 01:40 masterha_check_repl  ## 检查mysql 复制状况
-r-xr-xr-x. 1 root root  1779 8月  28 01:40 masterha_check_ssh  ## 检查 MHA的SSH 配置状况
-r-xr-xr-x. 1 root root  1865 8月  28 01:40 masterha_check_status  ## 检查当前MHA 运行状态
-r-xr-xr-x. 1 root root  3201 8月  28 01:40 masterha_conf_host     ## 添加或删除配置的server信息
-r-xr-xr-x. 1 root root  2517 8月  28 01:40 masterha_manager  ## 启动 manager 的脚本
-r-xr-xr-x. 1 root root  2165 8月  28 01:40 masterha_master_monitor  ## 检测master 是否宕机
-r-xr-xr-x. 1 root root  2373 8月  28 01:40 masterha_master_switch ## 控制故障迁移(自动或手动)
-r-xr-xr-x. 1 root root  5171 8月  28 01:40 masterha_secondary_check
-r-xr-xr-x. 1 root root  1739 8月  28 01:40 masterha_stop  ## 关闭manager
-r-xr-xr-x. 1 root root  8261 8月  28 01:34 purge_relay_logs
-r-xr-xr-x. 1 root root  7525 8月  28 01:34 save_binary_logs

4、node安装后也会在/usr/local/bin 下面会生成几个脚本(这些工具通常由MHA Manager的脚本触发,无需人为操作)
主要如下
[root@mysql1 bin]# ll
总用量 26440
-r-xr-xr-x. 1 root root    16381 8月  28 01:32 apply_diff_relay_logs  ## 识别差异的中继日志事件并将差异的事件应用于其他的slave
-rwxr-xr-x. 1 root root  8157912 8月  27 17:11 cmake
-rwxr-xr-x. 1 root root  8743880 8月  27 17:11 cpack
-rwxr-xr-x. 1 root root 10124504 8月  27 17:11 ctest
-r-xr-xr-x. 1 root root     4807 8月  28 01:32 filter_mysqlbinlog     ## 去除不必要的 ROLLBACK事件(MHA 已不再使用这个工具) 
-r-xr-xr-x. 1 root root     8261 8月  28 01:32 purge_relay_logs    ## 清除中继日志(不会阻塞线程)
-r-xr-xr-x. 1 root root     7525 8月  28 01:32 save_binary_logs  ## 保存和辅助master的二进制日志

2.6 无密码认证的配置

1、mha-manager 匹配到所有数据库节点的无密码认证
[root@mha-manager ~]# ssh-keygen -t rsa         ## 一路按回车键
[root@mysql1 ~]# cd .ssh
[root@mha-manager ~]# ssh-copy-id 192.168.233.100
[root@mha-manager ~]# ssh-copy-id 192.168.233.200
[root@mha-manager ~]# ssh-copy-id 192.168.233.180
2、msql1 匹配到msql2和mysql3数据库节点的无密码认证
[root@mha-manager ~]# ssh-keygen -t rsa         ## 一路按回车键
[root@mysql1 ~]# ssh-copy-id 192.168.233.200
[root@mysql1 ~]# ssh-copy-id 192.168.233.180
3、msql2 匹配到msql1和mysql3数据库节点的无密码认证
[root@mha-manager ~]# ssh-keygen -t rsa         ## 一路按回车键
[root@mysql1 ~]# ssh-copy-id 192.168.233.100
[root@mysql1 ~]# ssh-copy-id 192.168.233.180
4、msql3 匹配到msql1和mysql2数据库节点的无密码认证
[root@mha-manager ~]# ssh-keygen -t rsa         ## 一路按回车键
[root@mysql1 ~]# ssh-copy-id 192.168.233.100
[root@mysql1 ~]# ssh-copy-id 192.168.233.200


2.7 MHA 配置文件的修改

1、在manager 节点上 复制相关脚本到/usr/local/bin 目录
[root@mha-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin/  
## 拷贝会有四个执行文件
[root@mha-manager ~]# ll /usr/local/bin/scripts/    
总用量 32
-rwxr-xr-x. 1 1001 1001  3648 5月  31 2015 master_ip_failover  ## 自动切换时VIP管理的脚本
-rwxr-xr-x. 1 1001 1001  9870 5月  31 2015 master_ip_online_change  ## 在线切换时vip 的管理  ## vip及虚拟ip
-rwxr-xr-x. 1 1001 1001 11867 5月  31 2015 power_manager  ## 故障发生后关闭主机的脚本
-rwxr-xr-x. 1 1001 1001  1360 5月  31 2015 send_report  ## 因故障切换后发送报警的脚本

2、 复制上面自动切换时VIP的管理脚本到/usr/local/bin  目录    这里使用脚本管理VIP
[root@mha-manager ~]# cp /usr/local/bin/scripts/master_ip_failover  /usr/local/bin/

3、 修改内容如下:(删除原有内容直接复制)
[root@mha-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
);
#############################添加内容部分#########################################
my $vip = '192.168.233.210';
my $brdc = '192.168.233.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";
}


4、 创建 MHA 软件目录 并拷贝配置文件
[root@mha-manager ~]# mkdir /etc/masterha
[root@mha-manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf  /etc/masterha

[root@mha-manager ~]# vim /etc/masterha/app1.cnf 
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover   ## 注意文件路径里是否有这些文件,没有需要去scripts里面拿出来
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
report_script=/usr/local/send_report
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.233.200 -s 192.168.233.180
shutdown_script=""
ssh_user=root
user=mha



[server1]
hostname=192.168.233.100
port=3306

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.233.200
port=3306


[server3]
hostname=192.168.233.180
port=3306

2.8 测试

  • 测试ssh无密码验证,如果正常输出successfully
[root@mha-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Fri Aug 28 15:59:00 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Aug 28 15:59:00 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Aug 28 15:59:00 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Aug 28 15:59:00 2020 - [info] Starting SSH connection tests..
Fri Aug 28 15:59:03 2020 - [debug] 
Fri Aug 28 15:59:01 2020 - [debug]  Connecting via SSH from root@192.168.233.200(192.168.233.200:22) to root@192.168.233.100(192.168.233.100:22)..
Fri Aug 28 15:59:02 2020 - [debug]   ok.
Fri Aug 28 15:59:02 2020 - [debug]  Connecting via SSH from root@192.168.233.200(192.168.233.200:22) to root@192.168.233.180(192.168.233.180:22)..
Fri Aug 28 15:59:02 2020 - [debug]   ok.
Fri Aug 28 15:59:03 2020 - [debug] 
Fri Aug 28 15:59:00 2020 - [debug]  Connecting via SSH from root@192.168.233.100(192.168.233.100:22) to root@192.168.233.200(192.168.233.200:22)..
Fri Aug 28 15:59:01 2020 - [debug]   ok.
Fri Aug 28 15:59:01 2020 - [debug]  Connecting via SSH from root@192.168.233.100(192.168.233.100:22) to root@192.168.233.180(192.168.233.180:22)..
Fri Aug 28 15:59:02 2020 - [debug]   ok.
Fri Aug 28 15:59:04 2020 - [debug] 
Fri Aug 28 15:59:01 2020 - [debug]  Connecting via SSH from root@192.168.233.180(192.168.233.180:22) to root@192.168.233.100(192.168.233.100:22)..
Fri Aug 28 15:59:02 2020 - [debug]   ok.
Fri Aug 28 15:59:02 2020 - [debug]  Connecting via SSH from root@192.168.233.180(192.168.233.180:22) to root@192.168.233.200(192.168.233.200:22)..
Fri Aug 28 15:59:03 2020 - [debug]   ok.
Fri Aug 28 15:59:04 2020 - [info] All SSH connection tests passed successfully.

  • 测试MHA 配置,最后出现is OK 表示成功
[root@mha-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf 
Fri Aug 28 16:00:59 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Aug 28 16:00:59 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Aug 28 16:00:59 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Aug 28 16:00:59 2020 - [info] MHA::MasterMonitor version 0.57.
Fri Aug 28 16:01:01 2020 - [info] GTID failover mode = 0
Fri Aug 28 16:01:01 2020 - [info] Dead Servers:
Fri Aug 28 16:01:01 2020 - [info] Alive Servers:
Fri Aug 28 16:01:01 2020 - [info]   192.168.233.100(192.168.233.100:3306)
Fri Aug 28 16:01:01 2020 - [info]   192.168.233.200(192.168.233.200:3306)
Fri Aug 28 16:01:01 2020 - [info]   192.168.233.180(192.168.233.180:3306)
Fri Aug 28 16:01:01 2020 - [info] Alive Slaves:
Fri Aug 28 16:01:01 2020 - [info]   192.168.233.200(192.168.233.200:3306)  Version=5.6.36-log (oldest major version between slaves) log-bin:enabled
Fri Aug 28 16:01:01 2020 - [info]     Replicating from 192.168.233.100(192.168.233.100:3306)
Fri Aug 28 16:01:01 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Aug 28 16:01:01 2020 - [info]   192.168.233.180(192.168.233.180:3306)  Version=5.6.36-log (oldest major version between slaves) log-bin:enabled
Fri Aug 28 16:01:01 2020 - [info]     Replicating from 192.168.233.100(192.168.233.100:3306)
Fri Aug 28 16:01:01 2020 - [info] Current Alive Master: 192.168.233.100(192.168.233.100:3306)
Fri Aug 28 16:01:01 2020 - [info] Checking slave configurations..
Fri Aug 28 16:01:01 2020 - [warning]  relay_log_purge=0 is not set on slave 192.168.233.200(192.168.233.200:3306).
Fri Aug 28 16:01:01 2020 - [info]  read_only=1 is not set on slave 192.168.233.180(192.168.233.180:3306).
Fri Aug 28 16:01:01 2020 - [warning]  relay_log_purge=0 is not set on slave 192.168.233.180(192.168.233.180:3306).
Fri Aug 28 16:01:01 2020 - [info] Checking replication filtering settings..
Fri Aug 28 16:01:01 2020 - [info]  binlog_do_db= , binlog_ignore_db= 
Fri Aug 28 16:01:01 2020 - [info]  Replication filtering check ok.
Fri Aug 28 16:01:01 2020 - [info] GTID (with auto-pos) is not supported
Fri Aug 28 16:01:01 2020 - [info] Starting SSH connection tests..
Fri Aug 28 16:01:04 2020 - [info] All SSH connection tests passed successfully.
Fri Aug 28 16:01:04 2020 - [info] Checking MHA Node version..
Fri Aug 28 16:01:05 2020 - [info]  Version check ok.
Fri Aug 28 16:01:05 2020 - [info] Checking SSH publickey authentication settings on the current master..
Fri Aug 28 16:01:05 2020 - [info] HealthCheck: SSH to 192.168.233.100 is reachable.
Fri Aug 28 16:01:06 2020 - [info] Master MHA Node version is 0.57.
Fri Aug 28 16:01:06 2020 - [info] Checking recovery script configurations on 192.168.233.100(192.168.233.100:3306)..
Fri Aug 28 16:01:06 2020 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/tmp/save_binary_logs_test --manager_version=0.57 --start_file=master-bin.000001 
Fri Aug 28 16:01:06 2020 - [info]   Connecting to root@192.168.233.100(192.168.233.100:22).. 
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /usr/local/mysql/data, up to master-bin.000001
Fri Aug 28 16:01:06 2020 - [info] Binlog setting check done.
Fri Aug 28 16:01:06 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Aug 28 16:01:06 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.233.200 --slave_ip=192.168.233.200 --slave_port=3306 --workdir=/tmp --target_version=5.6.36-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Fri Aug 28 16:01:06 2020 - [info]   Connecting to root@192.168.233.200(192.168.233.200:22).. 
  Checking slave recovery environment settings..
    Opening /usr/local/mysql/data/relay-log.info ... ok.
    Relay log found at /usr/local/mysql/data, up to relay-log-bin.000002
    Temporary relay log file is /usr/local/mysql/data/relay-log-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.
Fri Aug 28 16:01:07 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.233.180 --slave_ip=192.168.233.180 --slave_port=3306 --workdir=/tmp --target_version=5.6.36-log --manager_version=0.57 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx
Fri Aug 28 16:01:07 2020 - [info]   Connecting to root@192.168.233.180(192.168.233.180:22).. 
  Checking slave recovery environment settings..
    Opening /usr/local/mysql/data/relay-log.info ... ok.
    Relay log found at /usr/local/mysql/data, up to relay-log-bin.000004
    Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000004
    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.
Fri Aug 28 16:01:07 2020 - [info] Slaves settings check done.
Fri Aug 28 16:01:07 2020 - [info] 
192.168.233.100(192.168.233.100:3306) (current master)
 +--192.168.233.200(192.168.233.200:3306)
 +--192.168.233.180(192.168.233.180:3306)

Fri Aug 28 16:01:07 2020 - [info] Checking replication health on 192.168.233.200..
Fri Aug 28 16:01:07 2020 - [info]  ok.
Fri Aug 28 16:01:07 2020 - [info] Checking replication health on 192.168.233.180..
Fri Aug 28 16:01:07 2020 - [info]  ok.
Fri Aug 28 16:01:07 2020 - [info] Checking master_ip_failover_script status:
Fri Aug 28 16:01:07 2020 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.233.100 --orig_master_ip=192.168.233.100 --orig_master_port=3306 


IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.233.200===

Checking the Status of the script.. OK 
Fri Aug 28 16:01:07 2020 - [info]  OK.
Fri Aug 28 16:01:07 2020 - [warning] shutdown_script is not defined.
Fri Aug 28 16:01:07 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

2.9 虚拟IP 设置

  • 第一次配置需要去master上手动开启虚拟ip
[root@mysql1 ~]# /sbin/ifconfig ens33:1 192.168.233.210/24

2.10 启动MHA

[root@mha-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 &

2.11 查看 MHA 状态

可以看到当前主服务器是mysql1节点

[root@mha-manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:81859) is running(0:PING_OK), master:192.168.233.100
[2]+  退出 1                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

2.12 查看日志

也可以看到当前master是mysql1

[root@mha-manager ~]# cat /var/log/masterha/app1/manager.log 

192.168.233.100(192.168.233.100:3306) (current master)
 +--192.168.233.200(192.168.233.200:3306)
 +--192.168.233.180(192.168.233.180:3306)

2.13查看mysql1的VIP地址是否存在,这个地址不会因为manager节点停止而消失

[root@mysql1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.233.100  netmask 255.255.255.0  broadcast 192.168.233.255
        inet6 fe80::bdfe:6407:b656:c916  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:7b:dc  txqueuelen 1000  (Ethernet)
        RX packets 26401  bytes 11820994 (11.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19535  bytes 3314774 (3.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.233.210  netmask 255.255.255.0  broadcast 192.168.233.255
        ether 00:0c:29:2f:7b:dc  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 354  bytes 21650 (21.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 354  bytes 21650 (21.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.14 验证

[root@mha-manager ~]# tailf /var/log/masterha/app1/manager.log  ## 启用监控观察日志记录
[root@mysql1 ~]# pkill -9 mysql       ## 查看master变化


[root@mysql2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.233.200  netmask 255.255.255.0  broadcast 192.168.233.255
        inet6 fe80::1199:c740:2050:ac62  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e7:9d:50  txqueuelen 1000  (Ethernet)
        RX packets 36370  bytes 12405923 (11.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24783  bytes 4235966 (4.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.233.210  netmask 255.255.255.0  broadcast 192.168.233.255
        ether 00:0c:29:e7:9d:50  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 303  bytes 25961 (25.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 303  bytes 25961 (25.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:5f:ad:7f  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值