目录

前言 

一、MHA概述

1.MHA定义

2.MHA组成

3.MHA特点

4.MHA工作原理

二、数据同步方式

1.异步复制(Asynchronous replication)

2.全同步复制(Fully synchronous replication)

3.半同步复制(Semisynchronous replication)

三、搭建MHA高可用

1.实验环境准备

2.主从服务器配置

2.1主服务器数据库配置

2.2从服务器数据库配置

2.3主从服务器做软连接

2.4主从服务器数据库授权

2.5查看Master主服务器位置偏移量

3.从服务器开启主从复制读写分离

3.1主从复制

3.2读写分离

3.3验证

主服务器插入数据

从服务器验证数据

4.安装MHA Node数据节点和MHA Manager管理节点

4.1主从服务器和MHA Manager管理节点安装Node数据节点

4.2MHA Manager安装管理节点

4.3主从服务器和MHA Manager管理节点做免密登录

Manager管理节点免密配置给Master、Slave1和Slave2

Master主服务器免密配置给Slave1和Slave2

Slave1从服务器免密配置给Master和Slave2

Slave2从服务器免密配置给Master和Slave1

验证免密登录

5.MHA Manager管理节点配置MHA

5.1使用master_ip_failover管理VIP和故障切换

5.2使用app1.cnf管理Mysql节点服务器

6.设置主服务器虚拟IP

7.MHA Manager管理节点检测

7.1Master_check_ssh——检查MHASSH配置

7.2masterha_check_repl——检查 MySQL 复制状况

8.启动MHA

9.模拟故障——查看MHA是否可以正常调用

9.1将主服务器的Mysql服务停止

9.2查看是否正常切换

9.3修复故障

9.4再次配置app1.cnf

9.5再次启动MHA

9.6验证——检查主动复制和Mysql_SSH

四、总结

1.作用

2.核心部分

3.重点配置文件

4.MHA工作原理

5.MHA故障问题


前言 

MHA高可用故障就是单点故障,那么我们如何解决单点故障

MHA中Master如何将故障的机器停止,使用备用的Slave服务器

一、MHA概述

1.MHA定义

  • MHA(MasterHigh Availablity)是一套优秀的Mysql高可用环境下故障切换和主从复制的软件。
  • MHA 的出现就是解决MySQL 单点的问题。
  • MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。
  • MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。

MHA是建在主从复制的基础上的;0-30秒自动完成故障切换是MHA的特性 

2.MHA组成

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

MHA Manager 可以单独部署在一台独立的机器上,管理多个 master-slave 集群;也可以部署在一台 slave 节点上。
MHA Manager 会定时探测集群中的 master 节点。当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 mas然后将所有其他的 slave 重新指向新的 master。整个故障转移过程对应用程序完全透明。

Node节点就是探测Mysql数据库是否正常

3.MHA特点

  • 自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
  • 使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
  • 目前MHA支持一主多从架构,最少三台服务,即一主两从

MHA Manger 管理多组主从复制;从服务器要等主服务器宕掉后才能升为主服务器;从服务器中数据最新最全的才会成为新的主服务器。

4.MHA工作原理

  • 从宕机崩溃的master 保存二进制日志事件(binlog  events);
  • 识别含有最新的更新 slave 日志(可以理解为主从复制数据最新的从服务器)
  • 应用差异的中继日志(relay log)到其他的slave(将数据信息最全最新的从服务器的中继日志给其他的从服务器)
  • 应用从master保存的二进制日志事件
  • 提升一个 salve 为新的master
  • 使其他的slave连接行的master 进行复制。

二、数据同步方式

1.异步复制(Asynchronous replication)

MySQL默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题:主如果crash掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。

2.全同步复制(Fully synchronous replication)

指当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。因为需要等待所有从库执行完该事务才能返回,所以全同步复制的性能必然会收到严重的影响。

3.半同步复制(Semisynchronous replication)

介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到relay log中才返回给客户端。相对于异步复制,半同步复制提高了数据的安全性,同时它也造成了一定程度的延迟,这个延迟最少是一个TCP/IP往返的时间。所以,半同步复制最好在低延时的网络中使用。

三、搭建MHA高可用

实验思路:搭建MHA机构---->数据库安装---->一主两从---->MHA搭建

故障模拟:主服务器失效---->主服务器的备胎服务器成为主服务器---->原故障

MHA是为了解决故障切换、数据尽可能的保存,以及所有节点日志的一致性

Master 主服务器 MHA Node :192.168.241.11

Slave 1 从服务器 MHA Node :192.168.241.22

Slave 2 从服务器 MHA Node :192.168.241.23

MHA Manager MHA Node :192.168.241.24

1.实验环境准备

systemctl stop firewalld

setenforce 0

2.主从服务器配置

2.1主服务器数据库配置

[root@master ~]#vim /etc/my.cnf
[root@master ~]#sed -n '25,27p' /etc/my.cnf
server-id = 1
#主服务器id号
log_bin=master-bin
#开启二进制文件
log-slave-updates = true
#开启从服务器可以主从复制更新二进制文件
[root@master ~]#systemctl restart mysqld.service

2.2从服务器数据库配置

vim /etc/my.cnf
sed -n '25,28p' /etc/my.cnf


server-id = 2
#从服务器id号   注意:主服务器和两台从服务器id号均不一致
log_bin=master-bin
#二进制日志master
relay-log=relay-log-bin
#开启中继日志
relay-log-index=slave-relay-bin.index
#中继日志索引

systemctl restart mysqld.service
#重启Mysql服务

2.3主从服务器做软连接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

2.4主从服务器数据库授权

mysql -uroot -p123456

mysql> grant replication slave on *.* to 'myslave'@'192.168.241.%' identified by '123456';
#从数据库同步使用
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to 'mha'@'192.168.241.%' identified by 'manager';
#manager 使用
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager'; 
#防止从库通过主机名连接不上主库
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager'; 
#Slave1 使用
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager'; 
#Slave2 使用
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
#刷新数据库
Query OK, 0 rows affected (0.00 sec)

2.5查看Master主服务器位置偏移量

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

3.从服务器开启主从复制读写分离

3.1主从复制

mysql> change master to master_host='192.168.241.11',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1747;
#开启主从复制同步指定主服务器数据
Query OK, 0 rows affected, 2 warnings (0.11 sec)

mysql> start slave;
#开启主从复制
Query OK, 0 rows affected (0.00 sec)


mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.241.11
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1747
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
              #从IO线程开启  此项如果是No
            Slave_SQL_Running: Yes
                #从SQL线程开启  此项如果是No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1747
              Relay_Log_Space: 526
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 04b457a0-e5b0-11ee-a5c5-000c29514bb5
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

3.2读写分离

mysql> set global read_only=1;
#从服务器设置为只读   类似于做了一个读写分离
Query OK, 0 rows affected (0.00 sec)

3.3验证

主服务器插入数据
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.10 sec)

mysql> create database class;
Query OK, 1 row affected (0.00 sec)

mysql> use class;
Database changed
mysql> create table class(id int,name varchar(8),hobby varchar(40));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into class values(1,'cxk','ctrl');
Query OK, 1 row affected (0.01 sec)
从服务器验证数据
mysql> use class;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from class;
+------+------+-------+
| id   | name | hobby |
+------+------+-------+
|    1 | cxk  | ctrl  |
+------+------+-------+
1 row in set (0.00 sec)

4.安装MHA Node数据节点和MHA Manager管理节点

MHA Node数据节点是为了方便MHA Manager管理节点

4.1主从服务器和MHA Manager管理节点安装Node数据节点

yum install epel-release --nogpgcheck -y
#安装额外源

yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
#安装依赖环境

[root@manager opt]#vim /etc/hosts
[root@manager opt]#sed -n '3,7p' /etc/hosts
192.168.241.24 manager
192.168.241.11 master
192.168.241.22 slave1
192.168.241.23 slave2
#在Manager管理节点设置主机映射 域名解析 方便scp远程拷贝便捷
[root@manager opt]#scp mha4mysql-node-0.57.tar.gz master:/opt
[root@manager opt]#scp mha4mysql-node-0.57.tar.gz slave1:/opt
[root@manager opt]#scp mha4mysql-node-0.57.tar.gz slave2:/opt

#编译安装MHA Node数据节点

tar zxvf mha4mysql-node-0.57.tar.gz

cd mha4mysql-node-0.57/

perl Makefile.PL 

make && make install

ls /usr/local/bin/
apply_diff_relay_logs
#识别差异的中继日志事件并将其差异的事件应用于其他的 slave
purge_relay_logs
#清除中继日志(不会阻塞 SQL 线程)
filter_mysqlbinlog
#去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具)
save_binary_logs
#保存和复制 master 的二进制日志


#这些工具通常由 MHAManager 的脚本触发,无需人为操作

4.2MHA Manager安装管理节点

[root@manager mha4mysql-node-0.57]#cd /opt
[root@manager opt]#ls
mha4mysql-node-0.57         mysql-boost-5.7.20.tar.gz  php-7.1.10
mha4mysql-node-0.57.tar.gz  nginx-1.18.0               php-7.1.10.tar.bz2
mysql-5.7.20                nginx-1.18.0.tar.gz
[root@manager opt]#rz -E
rz waiting to receive.
[root@manager opt]#ls
mha4mysql-manager-0.57.tar.gz  nginx-1.18.0
mha4mysql-node-0.57            nginx-1.18.0.tar.gz
mha4mysql-node-0.57.tar.gz     php-7.1.10
mysql-5.7.20                   php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz
[root@manager opt]#tar zxvf mha4mysql-manager-0.57.tar.gz
[root@manager opt]#cd mha4mysql-manager-0.57/
[root@manager mha4mysql-manager-0.57]#perl Makefile.PL
[root@manager mha4mysql-manager-0.57]#make && make install
[root@manager mha4mysql-manager-0.57]#ls
AUTHORS  COPYING  lib          MANIFEST    README   t
bin      debian   Makefile     META.yml    rpm      tests
blib     inc      Makefile.PL  pm_to_blib  samples
[root@manager mha4mysql-manager-0.57]#cd samples/
[root@manager samples]#ls
conf  scripts
[root@manager samples]#cd scripts/
[root@manager scripts]#ls
master_ip_failover  		#自动切换时 VIP 管理的脚本
master_ip_online_change 	#在线切换时 vip 的管理
power_manager 				#故障发生后关闭主机的脚本
send_report 				#因故障切换后发送报警的脚本

4.3主从服务器和MHA Manager管理节点做免密登录

Manager管理节点免密配置给Master、Slave1和Slave2
[root@manager scripts]#ssh-keygen -t rsa
[root@manager scripts]#ssh-copy-id 192.168.241.11
[root@manager scripts]#ssh-copy-id 192.168.241.22
[root@manager scripts]#ssh-copy-id 192.168.241.23
Master主服务器免密配置给Slave1和Slave2
[root@master mha4mysql-node-0.57]#ssh-keygen -t rsa
[root@master mha4mysql-node-0.57]#ssh-copy-id 192.168.241.22
[root@master mha4mysql-node-0.57]#ssh-copy-id 192.168.241.23
Slave1从服务器免密配置给Master和Slave2
[root@slave1 mha4mysql-node-0.57]#ssh-keygen -t rsa
[root@slave1 mha4mysql-node-0.57]#ssh-copy-id 192.168.241.11
[root@slave1 mha4mysql-node-0.57]#ssh-copy-id 192.168.241.23
Slave2从服务器免密配置给Master和Slave1
[root@slave2 mha4mysql-node-0.57]#ssh-keygen -t rsa
[root@slave2 mha4mysql-node-0.57]#ssh-copy-id 192.168.241.11
[root@slave2 mha4mysql-node-0.57]#ssh-copy-id 192.168.241.22
验证免密登录
ssh 192.168.241.11

ssh 192.168.241.22

ssh 192.168.241.23

ssh 192.168.241.24

5.MHA Manager管理节点配置MHA

5.1使用master_ip_failover管理VIP和故障切换

[root@manager scripts]#pwd
/opt/mha4mysql-manager-0.57/samples/scripts
[root@manager scripts]#ls
master_ip_failover  master_ip_online_change  power_manager  send_report
[root@manager scripts]#cp -rp /opt/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/
[root@manager scripts]#cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/
#复制自动切换时VIP管理的脚本到/usr/local/bin目录,这里使用master_ip_failover脚本来管理 VIP 和故障切换

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

Esc      :set paste
#进入末行模式   输入set paste  不加#号键


#!/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.241.200';
#指定vip的地址
my $brdc = '192.168.241.255';
#指定vip的广播地址
my $ifdev = 'ens33';
#指定vip绑定的网卡
my $key = '1';
#指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
#代表此变量值为ifconfig ens33:1 192.168.241.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
#代表此变量值为ifconfig ens33:1 192.168.241.200 down
my $exit_code = 0;
#指定退出状态码为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";
}

5.2使用app1.cnf管理Mysql节点服务器

[root@manager scripts]#mkdir /etc/masterha
[root@manager scripts]#cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@manager scripts]#vim /etc/masterha/app1.cnf 
[root@manager scripts]#cat /etc/masterha/app1.cnf 
[server default]
manager_log=/var/log/masterha/app1/manager.log
#manager日志
manager_workdir=/var/log/masterha/app1
#manager工作目录
master_binlog_dir=/usr/local/mysql/data
#master二进制日志文件保存路径(这里的路径要与master里配置的binlog的路径一致,以便MHA能找到)
master_ip_failover_script=/usr/local/bin/master_ip_failover
#设置自动failover时切换脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
#设置手动切换时的脚本
password=manager
#设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码
ping_interval=1
#设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行failover,这里设置为1秒
remote_workdir=/tmp
#设置远端mysql在发生切换时binlog的保存位置
repl_password=123
#设置复制用户的密码
repl_user=myslave
#设置复制用户的用户
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.10.22 -s 192.168.10.23
#指定检查的从服务器IP地址
shutdown_script=""
#设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机防止发生脑裂,这里没有使用)
ssh_user=root
#设置ssh的登录用户名
user=mha
#设置监控用户root

[server1]
hostname=192.168.241.11
port=3306

[server2]
candidate_master=1
#设置为候选master,设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个从库不是集群中最新的slave
check_repl_delay=0
#默认情况下如果一个slave落后master 超过100M的relay logs的话,MHA将不会选择该slave作为一个新的master, 因为对于这个slave的恢复需要花费很长时间;通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master
hostname=192.168.241.22
port=3306

[server3]
hostname=192.168.241.23
port=3306

6.设置主服务器虚拟IP

[root@master mha4mysql-node-0.57]#/sbin/ifconfig ens33:1 192.168.241.200/24
[root@master mha4mysql-node-0.57]#ifconfig|grep inet|sed -n 3p|awk '{print $2}'
192.168.241.200

7.MHA Manager管理节点检测

Manager管理节点主件安装后会有如下工具

  • 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

7.1Master_check_ssh——检查MHASSH配置

[root@manager ~]#masterha_check_ssh -conf=/etc/masterha/app1.cnf
Fri Mar 29 17:28:22 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Mar 29 17:28:22 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Mar 29 17:28:22 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Mar 29 17:28:22 2024 - [info] Starting SSH connection tests..
Fri Mar 29 17:28:23 2024 - [debug] 
Fri Mar 29 17:28:22 2024 - [debug]  Connecting via SSH from root@192.168.241.11(192.168.241.11:22) to root@192.168.241.22(192.168.241.22:22)..
Fri Mar 29 17:28:22 2024 - [debug]   ok.
Fri Mar 29 17:28:22 2024 - [debug]  Connecting via SSH from root@192.168.241.11(192.168.241.11:22) to root@192.168.241.23(192.168.241.23:22)..
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:24 2024 - [debug] 
Fri Mar 29 17:28:23 2024 - [debug]  Connecting via SSH from root@192.168.241.23(192.168.241.23:22) to root@192.168.241.11(192.168.241.11:22)..
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:23 2024 - [debug]  Connecting via SSH from root@192.168.241.23(192.168.241.23:22) to root@192.168.241.22(192.168.241.22:22)..
Fri Mar 29 17:28:24 2024 - [debug]   ok.
Fri Mar 29 17:28:24 2024 - [debug] 
Fri Mar 29 17:28:22 2024 - [debug]  Connecting via SSH from root@192.168.241.22(192.168.241.22:22) to root@192.168.241.11(192.168.241.11:22)..
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:23 2024 - [debug]  Connecting via SSH from root@192.168.241.22(192.168.241.22:22) to root@192.168.241.23(192.168.241.23:22)..
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:24 2024 - [info] All SSH connection tests passed successfully.

7.2masterha_check_repl——检查 MySQL 复制状况

vim /etc/my.cnf
sed -n 3p /etc/my.cnf
#default-character-set=utf8


#这里主从服务器都需要修改字符集 否则检查repl会报错


systemctl restart mysqld.service 
[root@manager ~]#masterha_check_ssh -conf=/etc/masterha/app1.cnf
Fri Mar 29 17:28:22 2024 - [warning] Global configuration file /etc/masterha_d
Fri Mar 29 17:28:22 2024 - [info] Reading application default configuration fr
Fri Mar 29 17:28:22 2024 - [info] Reading server configuration from /etc/maste
Fri Mar 29 17:28:22 2024 - [info] Starting SSH connection tests..
Fri Mar 29 17:28:23 2024 - [debug] 
Fri Mar 29 17:28:22 2024 - [debug]  Connecting via SSH from root@192.168.241.1
Fri Mar 29 17:28:22 2024 - [debug]   ok.
Fri Mar 29 17:28:22 2024 - [debug]  Connecting via SSH from root@192.168.241.1
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:24 2024 - [debug] 
Fri Mar 29 17:28:23 2024 - [debug]  Connecting via SSH from root@192.168.241.2
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:23 2024 - [debug]  Connecting via SSH from root@192.168.241.2
Fri Mar 29 17:28:24 2024 - [debug]   ok.
Fri Mar 29 17:28:24 2024 - [debug] 
Fri Mar 29 17:28:22 2024 - [debug]  Connecting via SSH from root@192.168.241.2
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:23 2024 - [debug]  Connecting via SSH from root@192.168.241.2
Fri Mar 29 17:28:23 2024 - [debug]   ok.
Fri Mar 29 17:28:24 2024 - [info] All SSH connection tests passed successfully
[root@manager ~]#masterha_check_repl -conf=/etc/masterha/app1.cnf
Fri Mar 29 17:55:24 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Mar 29 17:55:24 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Mar 29 17:55:24 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Mar 29 17:55:24 2024 - [info] MHA::MasterMonitor version 0.57.
Creating directory /var/log/masterha/app1.. done.
Fri Mar 29 17:55:25 2024 - [info] GTID failover mode = 0
Fri Mar 29 17:55:25 2024 - [info] Dead Servers:
Fri Mar 29 17:55:25 2024 - [info] Alive Servers:
Fri Mar 29 17:55:25 2024 - [info]   192.168.241.11(192.168.241.11:3306)
Fri Mar 29 17:55:25 2024 - [info]   192.168.241.22(192.168.241.22:3306)
Fri Mar 29 17:55:25 2024 - [info]   192.168.241.23(192.168.241.23:3306)
Fri Mar 29 17:55:25 2024 - [info] Alive Slaves:
Fri Mar 29 17:55:25 2024 - [info]   192.168.241.22(192.168.241.22:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Fri Mar 29 17:55:25 2024 - [info]     Replicating from 192.168.241.11(192.168.241.11:3306)
Fri Mar 29 17:55:25 2024 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 29 17:55:25 2024 - [info]   192.168.241.23(192.168.241.23:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Fri Mar 29 17:55:25 2024 - [info]     Replicating from 192.168.241.11(192.168.241.11:3306)
Fri Mar 29 17:55:25 2024 - [info] Current Alive Master: 192.168.241.11(192.168.241.11:3306)
Fri Mar 29 17:55:25 2024 - [info] Checking slave configurations..
Fri Mar 29 17:55:25 2024 - [warning]  relay_log_purge=0 is not set on slave 192.168.241.22(192.168.241.22:3306).
Fri Mar 29 17:55:25 2024 - [warning]  relay_log_purge=0 is not set on slave 192.168.241.23(192.168.241.23:3306).
Fri Mar 29 17:55:25 2024 - [info] Checking replication filtering settings..
Fri Mar 29 17:55:25 2024 - [info]  binlog_do_db= , binlog_ignore_db= 
Fri Mar 29 17:55:25 2024 - [info]  Replication filtering check ok.
Fri Mar 29 17:55:25 2024 - [info] GTID (with auto-pos) is not supported
Fri Mar 29 17:55:25 2024 - [info] Starting SSH connection tests..
Fri Mar 29 17:55:28 2024 - [info] All SSH connection tests passed successfully.
Fri Mar 29 17:55:28 2024 - [info] Checking MHA Node version..
Fri Mar 29 17:55:29 2024 - [info]  Version check ok.
Fri Mar 29 17:55:29 2024 - [info] Checking SSH publickey authentication settings on the current master..
Fri Mar 29 17:55:29 2024 - [info] HealthCheck: SSH to 192.168.241.11 is reachable.
Fri Mar 29 17:55:29 2024 - [info] Master MHA Node version is 0.57.
Fri Mar 29 17:55:29 2024 - [info] Checking recovery script configurations on 192.168.241.11(192.168.241.11:3306)..
Fri Mar 29 17:55:29 2024 - [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 Mar 29 17:55:29 2024 - [info]   Connecting to root@192.168.241.11(192.168.241.11: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 Mar 29 17:55:29 2024 - [info] Binlog setting check done.
Fri Mar 29 17:55:29 2024 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Mar 29 17:55:29 2024 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.241.22 --slave_ip=192.168.241.22 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-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 Mar 29 17:55:29 2024 - [info]   Connecting to root@192.168.241.22(192.168.241.22: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..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Mar 29 17:55:30 2024 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.241.23 --slave_ip=192.168.241.23 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-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 Mar 29 17:55:30 2024 - [info]   Connecting to root@192.168.241.23(192.168.241.23: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..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Mar 29 17:55:30 2024 - [info] Slaves settings check done.
Fri Mar 29 17:55:30 2024 - [info] 
192.168.241.11(192.168.241.11:3306) (current master)
 +--192.168.241.22(192.168.241.22:3306)
 +--192.168.241.23(192.168.241.23:3306)

Fri Mar 29 17:55:30 2024 - [info] Checking replication health on 192.168.241.22..
Fri Mar 29 17:55:30 2024 - [info]  ok.
Fri Mar 29 17:55:30 2024 - [info] Checking replication health on 192.168.241.23..
Fri Mar 29 17:55:30 2024 - [info]  ok.
Fri Mar 29 17:55:30 2024 - [info] Checking master_ip_failover_script status:
Fri Mar 29 17:55:30 2024 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.241.11 --orig_master_ip=192.168.241.11 --orig_master_port=3306 


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

Checking the Status of the script.. OK 
Fri Mar 29 17:55:30 2024 - [info]  OK.
Fri Mar 29 17:55:30 2024 - [warning] shutdown_script is not defined.
Fri Mar 29 17:55:30 2024 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

8.启动MHA

[root@manager ~]#cd /opt/
[root@manager opt]#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] 16253

[root@manager opt]#ps -aux|grep manager
root      16253  0.2  1.1 297404 21748 pts/1    S    17:57   0:00 perl /usr/local/bin/masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root      16392  0.0  0.0 112824   988 pts/1    S+   17:59   0:00 grep --color=auto manager


[root@manager opt]#masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:16253) is running(0:PING_OK), master:192.168.241.11

9.模拟故障——查看MHA是否可以正常调用

9.1将主服务器的Mysql服务停止

[root@master ~]#systemctl stop mysqld.service

9.2查看是否正常切换

[root@slave1 ~]#ifconfig|grep inet|sed -n 3p|awk '{print $2}'
192.168.241.200
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

9.3修复故障

[root@master ~]#systemctl restart mysqld.service 
mysql> change master to master_host='192.168.241.22',master_user='myslave',master_password='123456',master_log_file='master-bin.000003',master_log_pos=154;
#让11主机去找到22主机为主服务器
Query OK, 0 rows affected, 2 warnings (0.14 sec)

mysql> start slave;
#开启主从复制
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.241.22
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: master-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 529
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 04b4af19-e5b0-11ee-bffc-000c293ea008
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

9.4再次配置app1.cnf

[root@manager opt]#vim /etc/masterha/app1.cnf

[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
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=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.241.11 -s 192.168.241.23
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.241.11
port=3306

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

[server3]
hostname=192.168.241.23
port=3306

因为192.168.241.11主服务器宕掉了,所以该配置文件的server1服务器就自动删除了,需要再次修改 

9.5再次启动MHA

[root@manager opt]#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 &

[root@manager opt]#ps aux |grep manager
root      16253  0.1  1.2 299276 23684 pts/1    S    17:57   0:04 perl /usr/local/bin/masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root      17137  0.1  1.2 298824 23284 pts/1    S    18:08   0:03 perl /usr/local/bin/masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root      24141  1.0  1.1 297316 21748 pts/1    S    18:51   0:00 perl /usr/local/bin/masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root      24221  0.0  0.0 112824   988 pts/1    S+   18:51   0:00 grep --color=auto manager

9.6验证——检查主动复制和Mysql_SSH

[root@manager opt]#masterha_check_ssh -conf=/etc/masterha/app1.cnf
Fri Mar 29 18:52:09 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Mar 29 18:52:09 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Mar 29 18:52:09 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Mar 29 18:52:09 2024 - [info] Starting SSH connection tests..
Fri Mar 29 18:52:10 2024 - [debug] 
Fri Mar 29 18:52:09 2024 - [debug]  Connecting via SSH from root@192.168.241.11(192.168.241.11:22) to root@192.168.241.22(192.168.241.22:22)..
Fri Mar 29 18:52:09 2024 - [debug]   ok.
Fri Mar 29 18:52:11 2024 - [debug] 
Fri Mar 29 18:52:09 2024 - [debug]  Connecting via SSH from root@192.168.241.22(192.168.241.22:22) to root@192.168.241.11(192.168.241.11:22)..
Fri Mar 29 18:52:10 2024 - [debug]   ok.
Fri Mar 29 18:52:10 2024 - [debug]  Connecting via SSH from root@192.168.241.22(192.168.241.22:22) to root@192.168.241.23(192.168.241.23:22)..
Fri Mar 29 18:52:10 2024 - [debug]   ok.
Fri Mar 29 18:52:11 2024 - [debug] 
Fri Mar 29 18:52:10 2024 - [debug]  Connecting via SSH from root@192.168.241.23(192.168.241.23:22) to root@192.168.241.11(192.168.241.11:22)..
Fri Mar 29 18:52:10 2024 - [debug]   ok.
Fri Mar 29 18:52:10 2024 - [debug]  Connecting via SSH from root@192.168.241.23(192.168.241.23:22) to root@192.168.241.22(192.168.241.22:22)..
Fri Mar 29 18:52:11 2024 - [debug]   ok.
Fri Mar 29 18:52:11 2024 - [info] All SSH connection tests passed successfully.




[root@manager opt]#masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:24141) is running(0:PING_OK), master:192.168.241.22
[root@manager opt]#masterha_check_repl -conf=/etc/masterha/app1.cnf
Fri Mar 29 18:53:11 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Mar 29 18:53:11 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Mar 29 18:53:11 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Mar 29 18:53:11 2024 - [info] MHA::MasterMonitor version 0.57.
Fri Mar 29 18:53:12 2024 - [info] GTID failover mode = 0
Fri Mar 29 18:53:12 2024 - [info] Dead Servers:
Fri Mar 29 18:53:12 2024 - [info] Alive Servers:
Fri Mar 29 18:53:12 2024 - [info]   192.168.241.11(192.168.241.11:3306)
Fri Mar 29 18:53:12 2024 - [info]   192.168.241.22(192.168.241.22:3306)
Fri Mar 29 18:53:12 2024 - [info]   192.168.241.23(192.168.241.23:3306)
Fri Mar 29 18:53:12 2024 - [info] Alive Slaves:
Fri Mar 29 18:53:12 2024 - [info]   192.168.241.11(192.168.241.11:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Fri Mar 29 18:53:12 2024 - [info]     Replicating from 192.168.241.22(192.168.241.22:3306)
Fri Mar 29 18:53:12 2024 - [info]   192.168.241.23(192.168.241.23:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Fri Mar 29 18:53:12 2024 - [info]     Replicating from 192.168.241.22(192.168.241.22:3306)
Fri Mar 29 18:53:12 2024 - [info] Current Alive Master: 192.168.241.22(192.168.241.22:3306)
Fri Mar 29 18:53:12 2024 - [info] Checking slave configurations..
Fri Mar 29 18:53:12 2024 - [info]  read_only=1 is not set on slave 192.168.241.11(192.168.241.11:3306).
Fri Mar 29 18:53:12 2024 - [warning]  relay_log_purge=0 is not set on slave 192.168.241.11(192.168.241.11:3306).
Fri Mar 29 18:53:12 2024 - [info]  read_only=1 is not set on slave 192.168.241.23(192.168.241.23:3306).
Fri Mar 29 18:53:12 2024 - [warning]  relay_log_purge=0 is not set on slave 192.168.241.23(192.168.241.23:3306).
Fri Mar 29 18:53:12 2024 - [info] Checking replication filtering settings..
Fri Mar 29 18:53:12 2024 - [info]  binlog_do_db= , binlog_ignore_db= 
Fri Mar 29 18:53:12 2024 - [info]  Replication filtering check ok.
Fri Mar 29 18:53:12 2024 - [info] GTID (with auto-pos) is not supported
Fri Mar 29 18:53:12 2024 - [info] Starting SSH connection tests..
Fri Mar 29 18:53:15 2024 - [info] All SSH connection tests passed successfully.
Fri Mar 29 18:53:15 2024 - [info] Checking MHA Node version..
Fri Mar 29 18:53:15 2024 - [info]  Version check ok.
Fri Mar 29 18:53:15 2024 - [info] Checking SSH publickey authentication settings on the current master..
Fri Mar 29 18:53:16 2024 - [info] HealthCheck: SSH to 192.168.241.22 is reachable.
Fri Mar 29 18:53:16 2024 - [info] Master MHA Node version is 0.57.
Fri Mar 29 18:53:16 2024 - [info] Checking recovery script configurations on 192.168.241.22(192.168.241.22:3306)..
Fri Mar 29 18:53:16 2024 - [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.000003 
Fri Mar 29 18:53:16 2024 - [info]   Connecting to root@192.168.241.22(192.168.241.22: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.000003
Fri Mar 29 18:53:16 2024 - [info] Binlog setting check done.
Fri Mar 29 18:53:16 2024 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Mar 29 18:53:16 2024 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.241.11 --slave_ip=192.168.241.11 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-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 Mar 29 18:53:16 2024 - [info]   Connecting to root@192.168.241.11(192.168.241.11: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 master-relay-bin.000002
    Temporary relay log file is /usr/local/mysql/data/master-relay-bin.000002
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Mar 29 18:53:17 2024 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.241.23 --slave_ip=192.168.241.23 --slave_port=3306 --workdir=/tmp --target_version=5.7.17-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 Mar 29 18:53:17 2024 - [info]   Connecting to root@192.168.241.23(192.168.241.23: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..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Mar 29 18:53:17 2024 - [info] Slaves settings check done.
Fri Mar 29 18:53:17 2024 - [info] 
192.168.241.22(192.168.241.22:3306) (current master)
 +--192.168.241.11(192.168.241.11:3306)
 +--192.168.241.23(192.168.241.23:3306)

Fri Mar 29 18:53:17 2024 - [info] Checking replication health on 192.168.241.11..
Fri Mar 29 18:53:17 2024 - [info]  ok.
Fri Mar 29 18:53:17 2024 - [info] Checking replication health on 192.168.241.23..
Fri Mar 29 18:53:17 2024 - [info]  ok.
Fri Mar 29 18:53:17 2024 - [info] Checking master_ip_failover_script status:
Fri Mar 29 18:53:17 2024 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.241.22 --orig_master_ip=192.168.241.22 --orig_master_port=3306 


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

Checking the Status of the script.. OK 
Fri Mar 29 18:53:17 2024 - [info]  OK.
Fri Mar 29 18:53:17 2024 - [warning] shutdown_script is not defined.
Fri Mar 29 18:53:17 2024 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

四、总结

1.作用

Mysql高可用,解决单点故障,切换主备服务器

2.核心部分

  • Manager:管理节点、做MHA启动关闭和检测Mysql中各种健康检查
  • Node:在发生故障的时候,尽可能保存二进制日志,并实现故障切换(VIP地址飘逸)

3.重点配置文件

  • master ip failover:命令工具 ,定义的是基于VIP的检测和故障转移 (VIP从master----->新的 master)
  • app1.conf:mha的主要配置文件,主要定义了mha的工作目录、日志(mysal二进制日志位置)、使用mha的登录mysql的用户、密码使用从服务器、身份同步master的账号、密码   (五个)

4.MHA工作原理

  • MHA会多次尝试检测master的存活状态(/var/log/masterha/app1/manager.log )
  • MHA会多次尝试、尽可能的保存master的二进制日志
  • MHA会根据app1.cnf中的配置部分,进行主备服务器顶替主服务器的位置
  • MHA最后会将master的VIP地址切换到从服务器的位置
  • MHA再选择完整数据内容的master主服务器,会在其余的salve上执行change master操作,指向性的master, 来保证Mysql的集群的健康性

5.MHA故障问题

  • 软件连接
  • 免密登录
  • 五个账号授权(其中三个账号是测试环境需要做的)
  • 初次运行MHA功能是,需要临时添加虚拟IP(/sbin/ifconfig ens33:1 192.168.xxx.xxx/24)
  • 配置文件--效验 (master_ip_failover 1个故障切换的脚本,app1.cnf mha的主配置文件)
  • 先安装node节点 再安装主节点
  • 软连接必须要做
  • Mysqll配置文件,建议将中文字符集注释掉
  • MHA的内存 建议4G+(可能会导致VIP地址无法飘逸)
  • app1.cnf配置文件中,Mysql的二进制文件位置一定要指对
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐