MySQLmha

mha

master

bash 复制代码
[root@mysql-master ~ 09:50:43]# vim /etc/my.cnf
[root@mysql-master ~ 09:51:58]# vim /etc/my.cnf
[root@mysql-master ~ 09:52:24]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/
[root@mysql-master ~ 09:58:03]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@mysql-master ~ 09:58:20]# systemctl restart mysqld
[root@mysql-master ~ 09:58:36]#

slave01

bash 复制代码
[root@mysql-slave02 ~ 09:43:45]# systemctl stop mysqld.service
[root@mysql-slave02 ~ 09:49:06]# rm -f /usr/local/mysql/data/auto.cnf
[root@mysql-slave02 ~ 09:49:30]# systemctl start mysqld.service
[root@mysql-slave02 ~ 09:49:46]# yum -y install perl-Module-Install
[root@mysql-slave01 ~ 09:50:43]# vim /etc/my.cnf
[root@mysql-slave01 ~ 09:53:06]# vim /etc/my.cnf
[root@mysql-slave01 ~ 09:56:38]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/
[root@mysql-slave01 ~ 09:58:03]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@mysql-slave01 ~ 09:58:20]# systemctl restart mysqld

slave02

bash 复制代码
[root@mysql-slave02 ~ 09:43:45]# systemctl stop mysqld.service
[root@mysql-slave02 ~ 09:49:06]# rm -f /usr/local/mysql/data/auto.cnf
[root@mysql-slave02 ~ 09:49:30]# systemctl start mysqld.service
[root@mysql-slave02 ~ 09:49:46]# yum -y install perl-Module-Install
[root@mysql-slave02 ~ 09:50:43]# vim /etc/my.cnf
[root@mysql-slave02 ~ 09:57:11]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/
[root@mysql-slave02 ~ 09:58:03]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@mysql-slave02 ~ 09:58:20]# systemctl restart mysqld

配置一主两从

bash 复制代码
[root@mysql-slave01 ~ 09:58:36]# mysql -uroot -phuawei
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave on *.* to 'myslave'@'192.168.108.%' identified by
    -> '123';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

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

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

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

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

master

bash 复制代码
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1628 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

二从

bash 复制代码
mysql> change master to master_host='192.168.108.131',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1628;
Query OK, 0 rows affected, 2 warnings (0.01 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.108.131
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1628
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             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: 1628
              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: e13bbd13-608b-11f1-9c13-000c2900c45c
             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

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

安装mha软件

bash 复制代码
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@mysql-all ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@mysql-all ~]# cd mha4mysql-node-0.57
[root@mysql-all ~]# perl Makefile.PL
[root@mysql-all ~]# make && make install

在 MHA上安装 manager 组件

bash 复制代码
[root@mha mha4mysql-node-0.57 11:03:22]# cd ~
[root@mha ~ 11:03:31]# tar zxvf mha4mysql-manager-0.57.tar.gz
mha4mysql-manager-0.57/
mha4mysql-manager-0.57/tests/
[root@mha ~ 11:03:33]# cd mha4mysql-manager-0.57
[root@mha mha4mysql-manager-0.57 11:03:50]# perl Makefile.PL
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.627)
- DBD::mysql            ...loaded. (4.023)
- Time::HiRes           ...loaded. (1.9725)
- Config::Tiny          ...loaded. (2.14)
- Log::Dispatch         ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst        ...loaded. (0.57)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::manager
[root@mha mha4mysql-manager-0.57 11:04:01]# make && make install

配置无密码认证

在 manager 上配置到所有数据库节点的无密码认证

在 mysql-master 上配置到数据库节点mysql-slave01和mysql-slave02的无密码认证

在 mysql-slave01 上配置到数据库节点mysql-master和mysql-slave02的无密码认证

在 mysql-slave02 上配置到数据库节点mysql-master和mysql-slave01的无密码认证

配置 MHA

在 manager 节点上复制相关脚本到/usr/local/bin 目录。

bash 复制代码
[root@mha ~]# ssh-keygen -t rsa #一路按回车键
[root@mha ~]# ssh-copy-id 192.168.108.131
[root@mha ~]# ssh-copy-id 192.168.108.132
[root@mha ~]# ssh-copy-id 192.168.108.133
[root@mysql-master ~]# ssh-keygen -t rsa
[root@mysql-master ~]# ssh-copy-id 192.168.108.132
[root@mysql-master ~]# ssh-copy-id 192.168.108.133
[root@mysql-slave01 ~]# ssh-keygen -t rsa
[root@mysql-slave01 ~]# ssh-copy-id 192.168.108.131
[root@mysql-slave01 ~]# ssh-copy-id 192.168.108.133
[root@mysql-slave02 ~]# ssh-keygen -t rsa
[root@mysql-slave02 ~]# ssh-copy-id 192.168.108.131
[root@mysql-slave02 ~]# ssh-copy-id 192.168.108.132

配置mha

bash 复制代码
[root@mha ~ 11:37:34]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
[root@mha ~ 11:42:19]# ll /usr/local/bin/scripts/
total 32
-rwxr-xr-x 1 1001 1001  3648 May 31  2015 master_ip_failover
-rwxr-xr-x 1 1001 1001  9870 May 31  2015 master_ip_online_change
-rwxr-xr-x 1 1001 1001 11867 May 31  2015 power_manager
-rwxr-xr-x 1 1001 1001  1360 May 31  2015 send_report
[root@mha ~ 11:42:27]# cp /usr/local/bin/scripts/master_ip_failover 
[root@mha ~]# 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.108.200';
my $brdc = '192.168.108.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";
}

创建 MHA 软件目录并拷贝配置文件。

bash 复制代码
[root@mha ~]# mkdir /etc/masterha
[root@mha ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf
/etc/masterha/
[root@mha ~]# 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=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.108.132
-s 192.168.108.133
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.108.131
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.108.132
port=3306
[server3]
hostname=192.168.108.133
port=3306

配置文件解析

bash 复制代码
[server default]
manager_workdir=/var/log/masterha/app1 ##manager工作目录
manager_log=/var/log/masterha/app1/manager.log #manager日志
master_binlog_dir=/usr/local/mysql/data/ #master保存binlog的位置,这里的路径要与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中mha用户的密码
user=mha #设置监控用户mha
ping_interval=1 #设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover
remote_workdir=/tmp #设置远端mysql在发生切换时binlog的保存位置
repl_password=123 #设置复制用户的密码
repl_user=myslave #设置复制用户的用户
report_script=/usr/local/send_report #设置发生切换后发送的报警的脚本
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.108.132
-s 192.168.108.133
shutdown_script="" #设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机防止发生脑裂,这里没有使用)
ssh_user=root #设置ssh的登录用户名
[server1]
hostname=192.168.108.131
port=3306
[server2]
hostname=192.168.108.132
port=3306
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
[server3]
hostname=192.168.108.133
port=3306

验证配置

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

bash 复制代码
[root@mha ~ 12:04:00]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Mon Jun  8 12:05:40 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jun  8 12:05:40 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Jun  8 12:05:40 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Jun  8 12:05:40 2026 - [info] Starting SSH connection tests..
Mon Jun  8 12:05:41 2026 - [debug]
Mon Jun  8 12:05:40 2026 - [debug]  Connecting via SSH from root@192.168.108.131(192.168.108.131:22) to root@192.168.108.132(192.168.108.132:22)..
Mon Jun  8 12:05:41 2026 - [debug]   ok.
Mon Jun  8 12:05:41 2026 - [debug]  Connecting via SSH from root@192.168.108.131(192.168.108.131:22) to root@192.168.108.133(192.168.108.133:22)..
Mon Jun  8 12:05:41 2026 - [debug]   ok.
Mon Jun  8 12:05:42 2026 - [debug]
Mon Jun  8 12:05:41 2026 - [debug]  Connecting via SSH from root@192.168.108.132(192.168.108.132:22) to root@192.168.108.131(192.168.108.131:22)..
Mon Jun  8 12:05:41 2026 - [debug]   ok.
Mon Jun  8 12:05:41 2026 - [debug]  Connecting via SSH from root@192.168.108.132(192.168.108.132:22) to root@192.168.108.133(192.168.108.133:22)..
Mon Jun  8 12:05:42 2026 - [debug]   ok.
Mon Jun  8 12:05:43 2026 - [debug]
Mon Jun  8 12:05:41 2026 - [debug]  Connecting via SSH from root@192.168.108.133(192.168.108.133:22) to root@192.168.108.131(192.168.108.131:22)..
Mon Jun  8 12:05:42 2026 - [debug]   ok.
Mon Jun  8 12:05:42 2026 - [debug]  Connecting via SSH from root@192.168.108.133(192.168.108.133:22) to root@192.168.108.132(192.168.108.132:22)..
Mon Jun  8 12:05:42 2026 - [debug]   ok.
Mon Jun  8 12:05:43 2026 - [info] All SSH connection tests passed successfully.
[root@mha ~ 12:42:13]# masterha_check_repl -conf=/etc/masterha/app1.cnf
Mon Jun  8 12:42:18 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jun  8 12:42:18 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Jun  8 12:42:18 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Jun  8 12:42:18 2026 - [info] MHA::MasterMonitor version 0.57.
Mon Jun  8 12:42:19 2026 - [info] GTID failover mode = 0
Mon Jun  8 12:42:19 2026 - [info] Dead Servers:
Mon Jun  8 12:42:19 2026 - [info] Alive Servers:
Mon Jun  8 12:42:19 2026 - [info]   192.168.108.131(192.168.108.131:3306)
Mon Jun  8 12:42:19 2026 - [info]   192.168.108.132(192.168.108.132:3306)
Mon Jun  8 12:42:19 2026 - [info]   192.168.108.133(192.168.108.133:3306)
Mon Jun  8 12:42:19 2026 - [info] Alive Slaves:
Mon Jun  8 12:42:19 2026 - [info]   192.168.108.132(192.168.108.132:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Mon Jun  8 12:42:19 2026 - [info]     Replicating from 192.168.108.131(192.168.108.131:3306)
Mon Jun  8 12:42:19 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Jun  8 12:42:19 2026 - [info]   192.168.108.133(192.168.108.133:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Mon Jun  8 12:42:19 2026 - [info]     Replicating from 192.168.108.131(192.168.108.131:3306)
Mon Jun  8 12:42:19 2026 - [info] Current Alive Master: 192.168.108.131(192.168.108.131:3306)
Mon Jun  8 12:42:19 2026 - [info] Checking slave configurations..
Mon Jun  8 12:42:19 2026 - [warning]  relay_log_purge=0 is not set on slave 192.168.108.132(192.168.108.132:3306).
Mon Jun  8 12:42:19 2026 - [warning]  relay_log_purge=0 is not set on slave 192.168.108.133(192.168.108.133:3306).
Mon Jun  8 12:42:19 2026 - [info] Checking replication filtering settings..
Mon Jun  8 12:42:19 2026 - [info]  binlog_do_db= , binlog_ignore_db=
Mon Jun  8 12:42:19 2026 - [info]  Replication filtering check ok.
Mon Jun  8 12:42:19 2026 - [info] GTID (with auto-pos) is not supported
Mon Jun  8 12:42:19 2026 - [info] Starting SSH connection tests..
Mon Jun  8 12:42:22 2026 - [info] All SSH connection tests passed successfully.
Mon Jun  8 12:42:22 2026 - [info] Checking MHA Node version..
Mon Jun  8 12:42:22 2026 - [info]  Version check ok.
Mon Jun  8 12:42:22 2026 - [info] Checking SSH publickey authentication settings on the current master..
Mon Jun  8 12:42:23 2026 - [info] HealthCheck: SSH to 192.168.108.131 is reachable.
Mon Jun  8 12:42:23 2026 - [info] Master MHA Node version is 0.57.
Mon Jun  8 12:42:23 2026 - [info] Checking recovery script configurations on 192.168.108.131(192.168.108.131:3306)..
Mon Jun  8 12:42:23 2026 - [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
Mon Jun  8 12:42:23 2026 - [info]   Connecting to root@192.168.108.131(192.168.108.131: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
Mon Jun  8 12:42:23 2026 - [info] Binlog setting check done.
Mon Jun  8 12:42:23 2026 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Mon Jun  8 12:42:23 2026 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.108.132 --slave_ip=192.168.108.132 --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
Mon Jun  8 12:42:23 2026 - [info]   Connecting to root@192.168.108.132(192.168.108.132: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.
Mon Jun  8 12:42:23 2026 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.108.133 --slave_ip=192.168.108.133 --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
Mon Jun  8 12:42:23 2026 - [info]   Connecting to root@192.168.108.133(192.168.108.133: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.
Mon Jun  8 12:42:23 2026 - [info] Slaves settings check done.
Mon Jun  8 12:42:23 2026 - [info]
192.168.108.131(192.168.108.131:3306) (current master)
 +--192.168.108.132(192.168.108.132:3306)
 +--192.168.108.133(192.168.108.133:3306)

Mon Jun  8 12:42:23 2026 - [info] Checking replication health on 192.168.108.132..
Mon Jun  8 12:42:23 2026 - [info]  ok.
Mon Jun  8 12:42:23 2026 - [info] Checking replication health on 192.168.108.133..
Mon Jun  8 12:42:23 2026 - [info]  ok.
Mon Jun  8 12:42:23 2026 - [warning] master_ip_failover_script is not defined.
Mon Jun  8 12:42:23 2026 - [warning] shutdown_script is not defined.
Mon Jun  8 12:42:23 2026 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
#第一次配置需要去master上手动开启虚拟IP
[root@mysql-master ~ 12:43:30]#/sbin/ifconfig ens33:1 192.168.108.200/24

启动mha高可用

bash 复制代码
[root@mha ~ 12:42:24]# 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] 54992
[root@mha ~ 14:08:16]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:54992) is running(0:PING_OK), master:192.168.108.131
[root@mha ~ 14:08:56]# cat /var/log/masterha/app1/manager.log
Mon Jun  8 14:08:17 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jun  8 14:08:17 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Jun  8 14:08:17 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Jun  8 14:08:17 2026 - [info] MHA::MasterMonitor version 0.57.
Mon Jun  8 14:08:18 2026 - [info] GTID failover mode = 0
Mon Jun  8 14:08:18 2026 - [info] Dead Servers:
Mon Jun  8 14:08:18 2026 - [info] Alive Servers:
Mon Jun  8 14:08:18 2026 - [info]   192.168.108.131(192.168.108.131:3306)
Mon Jun  8 14:08:18 2026 - [info]   192.168.108.132(192.168.108.132:3306)
Mon Jun  8 14:08:18 2026 - [info]   192.168.108.133(192.168.108.133:3306)
Mon Jun  8 14:08:18 2026 - [info] Alive Slaves:
Mon Jun  8 14:08:18 2026 - [info]   192.168.108.132(192.168.108.132:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Mon Jun  8 14:08:18 2026 - [info]     Replicating from 192.168.108.131(192.168.108.131:3306)
Mon Jun  8 14:08:18 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Jun  8 14:08:18 2026 - [info]   192.168.108.133(192.168.108.133:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Mon Jun  8 14:08:18 2026 - [info]     Replicating from 192.168.108.131(192.168.108.131:3306)
Mon Jun  8 14:08:18 2026 - [info] Current Alive Master: 192.168.108.131(192.168.108.131:3306)
Mon Jun  8 14:08:18 2026 - [info] Checking slave configurations..
Mon Jun  8 14:08:18 2026 - [warning]  relay_log_purge=0 is not set on slave 192.168.108.132(192.168.108.132:3306).
Mon Jun  8 14:08:18 2026 - [warning]  relay_log_purge=0 is not set on slave 192.168.108.133(192.168.108.133:3306).
Mon Jun  8 14:08:18 2026 - [info] Checking replication filtering settings..
Mon Jun  8 14:08:18 2026 - [info]  binlog_do_db= , binlog_ignore_db=
Mon Jun  8 14:08:18 2026 - [info]  Replication filtering check ok.
Mon Jun  8 14:08:18 2026 - [info] GTID (with auto-pos) is not supported
Mon Jun  8 14:08:18 2026 - [info] Starting SSH connection tests..
Mon Jun  8 14:08:20 2026 - [info] All SSH connection tests passed successfully.
Mon Jun  8 14:08:20 2026 - [info] Checking MHA Node version..
Mon Jun  8 14:08:21 2026 - [info]  Version check ok.
Mon Jun  8 14:08:21 2026 - [info] Checking SSH publickey authentication settings on the current master..
Mon Jun  8 14:08:21 2026 - [info] HealthCheck: SSH to 192.168.108.131 is reachable.
Mon Jun  8 14:08:21 2026 - [info] Master MHA Node version is 0.57.
Mon Jun  8 14:08:21 2026 - [info] Checking recovery script configurations on 192.168.108.131(192.168.108.131:3306)..
Mon Jun  8 14:08:21 2026 - [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
Mon Jun  8 14:08:21 2026 - [info]   Connecting to root@192.168.108.131(192.168.108.131: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
Mon Jun  8 14:08:21 2026 - [info] Binlog setting check done.
Mon Jun  8 14:08:21 2026 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Mon Jun  8 14:08:21 2026 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.108.132 --slave_ip=192.168.108.132 --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
Mon Jun  8 14:08:21 2026 - [info]   Connecting to root@192.168.108.132(192.168.108.132: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.
Mon Jun  8 14:08:21 2026 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.108.133 --slave_ip=192.168.108.133 --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
Mon Jun  8 14:08:21 2026 - [info]   Connecting to root@192.168.108.133(192.168.108.133: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.
Mon Jun  8 14:08:22 2026 - [info] Slaves settings check done.
Mon Jun  8 14:08:22 2026 - [info]
192.168.108.131(192.168.108.131:3306) (current master)
 +--192.168.108.132(192.168.108.132:3306)
 +--192.168.108.133(192.168.108.133:3306)

Mon Jun  8 14:08:22 2026 - [warning] master_ip_failover_script is not defined.
Mon Jun  8 14:08:22 2026 - [warning] shutdown_script is not defined.
Mon Jun  8 14:08:22 2026 - [info] Set master ping interval 1 seconds.
Mon Jun  8 14:08:22 2026 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 192.168.108.132 -s 192.168.108.133
Mon Jun  8 14:08:22 2026 - [info] Starting ping health check on 192.168.108.131(192.168.108.131:3306)..
Mon Jun  8 14:08:22 2026 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
[root@mysql-master ~ 12:43:33]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.108.131  netmask 255.255.255.0  broadcast 192.168.108.255
        inet6 fe80::f920:294b:fdbf:59  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::8481:d640:497c:db51  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:63:13  txqueuelen 1000  (Ethernet)
        RX packets 61565  bytes 62332836 (59.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23278  bytes 2154454 (2.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.108.200  netmask 255.255.255.0  broadcast 192.168.108.255
        ether 00:0c:29:2f:63:13  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 125  bytes 10662 (10.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 125  bytes 10662 (10.4 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:d1:3a:4c  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

验证

bash 复制代码
[root@mha ~ 14:09:35]# tailf /var/log/masterha/app1/manager.log
192.168.108.131(192.168.108.131:3306) (current master)
 +--192.168.108.132(192.168.108.132:3306)
 +--192.168.108.133(192.168.108.133:3306)

Mon Jun  8 14:08:22 2026 - [warning] master_ip_failover_script is not defined.
Mon Jun  8 14:08:22 2026 - [warning] shutdown_script is not defined.
Mon Jun  8 14:08:22 2026 - [info] Set master ping interval 1 seconds.
Mon Jun  8 14:08:22 2026 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 192.168.108.132 -s 192.168.108.133
Mon Jun  8 14:08:22 2026 - [info] Starting ping health check on 192.168.108.131(192.168.108.131:3306)..
Mon Jun  8 14:08:22 2026 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
[root@mysql-master ~ 14:10:04]# pkill -9 mysql

08.132 -s 192.168.108.133  --user=root  --master_host=192.168.108.131  --master_ip=192.168.108.131  --master_port=3306 --master_user=mha --master_password=manager --ping_type=SELECT
Mon Jun  8 14:11:57 2026 - [info] Executing SSH check script: 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 --binlog_prefix=master-bin
Monitoring server 192.168.108.132 is reachable, Master is not reachable from 192.168.108.132. OK.
Mon Jun  8 14:11:57 2026 - [info] HealthCheck: SSH to 192.168.108.131 is reachable.
Monitoring server 192.168.108.133 is reachable, Master is not reachable from 192.168.108.133. OK.
Mon Jun  8 14:11:57 2026 - [info] Master is not reachable from all other monitoring servers. Failover should start.
Mon Jun  8 14:11:58 2026 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.108.131' (111))
Mon Jun  8 14:11:58 2026 - [warning] Connection failed 2 time(s)..

故障模拟

修复db

bash 复制代码
[root@mysql-master ~ 14:11:56]# systemctl start mysqld

修复主从

bash 复制代码
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     4545 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

[root@mysql-master ~ 14:13:25]# mysql -uroot -phuawei
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to master_host='192.168.108.132',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=4545;
Query OK, 0 rows affected, 2 warnings (0.01 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.108.132
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 4545
               Relay_Log_File: mysql-master-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             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: 4545
              Relay_Log_Space: 535
              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: 5415d128-62dc-11f1-86bf-000c29000e7e
             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

redis 安装

centos7

bash 复制代码
#安装依赖包
[root@localhost ~]# yum install tcl gcc gcc-c++ -y
#解压redis安装包
[root@localhost ~]# tar zxvf redis-6.2.14.tar.gz -C /usr/local/
#编译安装
[root@localhost ~]# cd /usr/local/redis-6.2.14/
[root@localhost redis-6.2.14]#make && make install
#启动并放在后台运行
[root@localhost redis-6.2.14]# redis-server redis.conf &
#查看启动端口
[root@localhost ~ 16:28:47]# netstat -antp | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      7408/redis-server 1
tcp6       0      0 ::1:6379                :::*                    LISTEN      7408/redis-server 1
#客户端进入
[root@localhost redis-6.2.14]# redis-cli
127.0.0.1:6379>

centos8

bash 复制代码
# centos stream 8 由appstream仓库提供,centos7需要配置epel仓库
[root@localhost ~]# dnf info redis
Last metadata expiration check: 0:00:18 ago on Thu 12 Feb 2026 03:04:51 PM CST.
Available Packages
Name : redis
Version : 5.0.3
Release : 5.module_el8.4.0+955+7126e393
Architecture : x86_64
Size : 927 k
Source : redis-5.0.3-5.module_el8.4.0+955+7126e393.src.rpm
Repository : appstream
Summary : A persistent key-value database
URL : http://redis.io
License : BSD and MIT
Description : Redis is an advanced key-value store. It is often referred to as a
data
: structure server since keys can contain strings, hashes, lists,
sets and
: sorted sets.
:
: You can run atomic operations on these types, like appending to a
string;
: incrementing the value in a hash; pushing to a list; computing set
: intersection, union and difference; or getting the member with
highest
: ranking in a sorted set.
:
: In order to achieve its outstanding performance, Redis works with
an
: in-memory dataset. Depending on your use case, you can persist it
either
: by dumping the dataset to disk every once in a while, or by
appending
: each command to a log.
:
: Redis also supports trivial-to-setup master-slave replication,
with very
: fast non-blocking first synchronization, auto-reconnection on net
split
: and so forth.
:
: Other features include Transactions, Pub/Sub, Lua scripting, Keys
with a
: limited time-to-live, and configuration settings to make Redis
behave like
: a cache.
:
: You can use Redis from most programming languages also.
# 安装redis
[root@localhost ~]# dnf install -y redis
# 设置服务开机自启并启动
[root@localhost ~]# systemctl enable redis --now
#查看redis-server监听6379端口
[root@xhde ~ 16:33:11]# ss -ntl
State          Recv-Q         Send-Q                 Local Address:Port                 Peer Address:Port        Process
LISTEN         0              511                        127.0.0.1:6379                      0.0.0.0:*
LISTEN         0              128                          0.0.0.0:22                        0.0.0.0:*
LISTEN         0              128                             [::]:22                           [::]:*
[root@xhde ~ 16:33:15]# redis-cli -p 6380
Could not connect to Redis at 127.0.0.1:6380: Connection refused
not connected> exit
[root@xhde ~ 16:34:02]# redis-server --port 6380
2109:C 08 Jun 2026 16:34:08.983 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2109:C 08 Jun 2026 16:34:08.983 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=2109, just started
2109:C 08 Jun 2026 16:34:08.983 # Configuration loaded
2109:M 08 Jun 2026 16:34:08.984 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6380
 |    `-._   `._    /     _.-'    |     PID: 2109
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

2109:M 08 Jun 2026 16:34:08.984 # Server initialized
2109:M 08 Jun 2026 16:34:08.984 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2109:M 08 Jun 2026 16:34:08.984 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2109:M 08 Jun 2026 16:34:08.984 * Ready to accept connections

[root@localhost ~]# pstree -p | grep redis
|-redis-server(10350)-+-{redis-server}(10351)
| |-{redis-server}(10352)
| `-{redis-server}(10353)
[root@localhost ~]# redis-cli
127.0.0.1:6379> ping
PONG

127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
......
[root@localhost ~]# redis-server --port 6380
[root@localhost ~]# ss -ntl
State Recv-Q Send-Q
Local Address:Port Peer Address:Port
Process
LISTEN 0 128
0.0.0.0:22 0.0.0.0:*
LISTEN 0 511
127.0.0.1:6379 0.0.0.0:*
LISTEN 0 511
0.0.0.0:6380 0.0.0.0:*
[root@localhost ~]# redis-cli -p 6380
127.0.0.1:6380> ping
PONG
相关推荐
程序员小羊!1 小时前
16 JAVA MySQL 8.0
java·开发语言·mysql
焦虑的说说1 小时前
mysql深分页性能瓶颈根源分析
数据库·mysql
云絮.2 小时前
数据库约束
java·数据库·sql·mysql·oracle
cui_ruicheng10 小时前
MySQL(四):数据类型与字段设计
数据库·mysql
DIY源码阁13 小时前
JavaSwing航班订票管理系统 - MySQL版
数据库·mysql
流星白龙16 小时前
【MySQL高阶】26.事务(1)
数据库·mysql
这个DBA有点耶17 小时前
索引优化深潜(下):索引合并、ICP 与索引设计的实战法则
数据库·mysql·架构
用户30745969820717 小时前
EXPLAIN 执行计划 完全精通指南
mysql
AI行业学习18 小时前
CC-Switch v3.16.1 官方下载 | 安装配置详细教程【2026.6.10】
java·开发语言·vue.js·python·mysql·eclipse·html