实验结构:

192.168.5.140 centos master01,HA-node
192.168.5.150 centos slave01,Candidate master,HA-node
192.168.5.128 centos slave02,HA-node
192.168.5.151 centos HA-manger
VIP 192.168.5.161

,额这个实验呢,就是模拟一个mysql的高可用集群配置。主的master,down机或mysql服务终止。vip会进行漂移至另外一台备用master。。master 对外提供写服务。。而slave.对外提供读服务。故障切换对应用程序透明,解决单点故障和最大程度保证数据完整。
manager节点,定时探测master节点是否故障,如果故障将最新数据的slave节点提升为maser,
node节点,就是数据节点

前置条件:

开搞开搞,前置条件先做好mysql 的主从从配置。这里我只写了一个库data1。上面我有写一个主从,,一通百通我就不写了。。这里只记录mysqlha的部署。

配置主从从

master01 :

主从状态:


mysql> show master status 
    -> ;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| master01-bin.000003 |     1305 | data1        | mysql            |                   |```

slave01

[root@mysql-slave01 ~]# mysql -uroot -pREDhat@123
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 25
Server version: 5.7.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.140
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master01-bin.000003
          Read_Master_Log_Pos: 1305
               Relay_Log_File: mysql-slave01-relay-bin.000011
                Relay_Log_Pos: 1474
        Relay_Master_Log_File: master01-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: 1305
              Relay_Log_Space: 1689
              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: d4285b4b-bd55-11ed-bf99-000c29ddc1a3
             Master_Info_File: /var/lib/mysql/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)

slave02

[root@mysql-slave02 ~]# mysql -uroot -pREDhat@123
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 8
Server version: 5.7.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.140
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master01-bin.000003
          Read_Master_Log_Pos: 1305
               Relay_Log_File: mysql-slave02-relay-bin.000003
                Relay_Log_Pos: 323
        Relay_Master_Log_File: master01-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: 1305
              Relay_Log_Space: 1858
              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: d4285b4b-bd55-11ed-bf99-000c29ddc1a3
             Master_Info_File: /var/lib/mysql/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)

关闭 relay log 的自动清除功能和开启只读(两个slave节点)

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

mysql> set global read_only=1;

下面就是ha配置

免密认证

manager 
ssh-keygen  -t rsa 回车
ssh-copy-id  192.168.5.140
ssh-copy-id  192.168.5.150
ssh-copy-id  192.168.5.160


mysql-master01
ssh-keygen  -t rsa 回车
ssh-copy-id  192.168.5.150
ssh-copy-id  192.168.5.160

mysql-slave01
ssh-keygen  -t rsa 回车
ssh-copy-id  192.168.5.140
ssh-copy-id  192.168.5.160


mysql-slave02
ssh-keygen  -t rsa 回车
ssh-copy-id  192.168.5.140
ssh-copy-id  192.168.5.150

所有数据库节点创建manger使用账户

grant all privileges on *.* to 'mha'@'localhost' identified by 'REDhat@123';

grant all privileges on *.* to 'mha'@'192.168.5.%' identified by 'REDhat@123';

flush privileges;


防止从库无法使用主机名连接主库登录。

grant all privileges on *.* to 'mha'@'master01' identified by 'manager';

grant all privileges on *.* to 'mha'@'slave01' identified by 'manager';

grant all privileges on *.* to 'mha'@'slave02' identified by 'manager';

flush privileges;

安装依赖包:

直接所有节点安装,如果安装的时候还缺依赖,就去rpmfind上面找,因为我就缺一些包,下载下来直接rpm -ivh 安装即可

yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-ParallelForkManager perl-Config-IniFiles ncftp perl-Params-Validate perl-CPAN perl-TestMock-LWP.noarch perl-LWP-Authen-Negotiate.noarch perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

安装node:

https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz
包存放目录/usr/local/src/

下载node包

[root@ha-manager src]# wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz
--2023-03-10 14:20:51--  https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz
正在解析主机 github.com (github.com)... 20.205.243.166
正在连接 github.com (github.com)|20.205.243.166|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://objects.githubusercontent.com/github-production-release-asset-2e65be/2093258/9d78fb60-2de4-11e8-8f0c-bac507a4e54f?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230310%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230310T062053Z&X-Amz-Expires=300&X-Amz-Signature=d768e9bf0fb9415d2aa528dc0f2919ae14515cacc8ce9ad7f0c2a1e87561e97f&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=2093258&response-content-disposition=attachment%3B%20filename%3Dmha4mysql-node-0.58.tar.gz&response-content-type=application%2Foctet-stream [跟随至新的 URL]
--2023-03-10 14:20:53--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/2093258/9d78fb60-2de4-11e8-8f0c-bac507a4e54f?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230310%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230310T062053Z&X-Amz-Expires=300&X-Amz-Signature=d768e9bf0fb9415d2aa528dc0f2919ae14515cacc8ce9ad7f0c2a1e87561e97f&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=2093258&response-content-disposition=attachment%3B%20filename%3Dmha4mysql-node-0.58.tar.gz&response-content-type=application%2Foctet-stream
正在解析主机 objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.111.133, 185.199.110.133
正在连接 objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... 失败:拒绝连接。
正在连接 objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:56220 (55K) [application/octet-stream]
正在保存至: “mha4mysql-node-0.58.tar.gz”

100%[==============================================================================================================>] 56,220       171KB/s 用时 0.3s   

2023-03-10 14:21:16 (171 KB/s) - 已保存 “mha4mysql-node-0.58.tar.gz” [56220/56220])

解压

[root@ha-manager src]# tar -zxvf mha4mysql-node-0.58.tar.gz 
mha4mysql-node-0.58/
mha4mysql-node-0.58/inc/
mha4mysql-node-0.58/inc/Module/
mha4mysql-node-0.58/inc/Module/Install/
mha4mysql-node-0.58/inc/Module/Install/Fetch.pm
mha4mysql-node-0.58/inc/Module/Install/Metadata.pm
mha4mysql-node-0.58/inc/Module/Install/AutoInstall.pm
mha4mysql-node-0.58/inc/Module/Install/Win32.pm
mha4mysql-node-0.58/inc/Module/Install/WriteAll.pm
mha4mysql-node-0.58/inc/Module/Install/Can.pm
mha4mysql-node-0.58/inc/Module/Install/Include.pm
mha4mysql-node-0.58/inc/Module/Install/Makefile.pm
mha4mysql-node-0.58/inc/Module/Install/Scripts.pm
mha4mysql-node-0.58/inc/Module/Install/Base.pm
mha4mysql-node-0.58/inc/Module/AutoInstall.pm
mha4mysql-node-0.58/inc/Module/Install.pm
mha4mysql-node-0.58/debian/
mha4mysql-node-0.58/debian/compat
mha4mysql-node-0.58/debian/changelog
mha4mysql-node-0.58/debian/rules
mha4mysql-node-0.58/debian/copyright
mha4mysql-node-0.58/debian/control
mha4mysql-node-0.58/bin/
mha4mysql-node-0.58/bin/purge_relay_logs
mha4mysql-node-0.58/bin/filter_mysqlbinlog
mha4mysql-node-0.58/bin/save_binary_logs
mha4mysql-node-0.58/bin/apply_diff_relay_logs
mha4mysql-node-0.58/AUTHORS
mha4mysql-node-0.58/MANIFEST
mha4mysql-node-0.58/t/
mha4mysql-node-0.58/t/perlcriticrc
mha4mysql-node-0.58/t/99-perlcritic.t
mha4mysql-node-0.58/README
mha4mysql-node-0.58/COPYING
mha4mysql-node-0.58/META.yml
mha4mysql-node-0.58/lib/
mha4mysql-node-0.58/lib/MHA/
mha4mysql-node-0.58/lib/MHA/BinlogPosFinderElp.pm
mha4mysql-node-0.58/lib/MHA/BinlogPosFindManager.pm
mha4mysql-node-0.58/lib/MHA/BinlogPosFinderXid.pm
mha4mysql-node-0.58/lib/MHA/BinlogPosFinder.pm
mha4mysql-node-0.58/lib/MHA/BinlogHeaderParser.pm
mha4mysql-node-0.58/lib/MHA/NodeConst.pm
mha4mysql-node-0.58/lib/MHA/NodeUtil.pm
mha4mysql-node-0.58/lib/MHA/SlaveUtil.pm
mha4mysql-node-0.58/lib/MHA/BinlogManager.pm
mha4mysql-node-0.58/Makefile.PL
mha4mysql-node-0.58/rpm/
mha4mysql-node-0.58/rpm/masterha_node.spec
[root@ha-manager src]# 
[root@ha-manager src]# ls
mha4mysql-manager-0.58  mha4mysql-manager-0.58.tar.gz  mha4mysql-node-0.58  mha4mysql-node-0.58.tar.gz

[root@ha-manager src]# cd mha4mysql-node-0.58/
[root@ha-manager mha4mysql-node-0.58]# ls
AUTHORS  bin  COPYING  debian  inc  lib  Makefile.PL  MANIFEST  META.yml  README  rpm  t

编译

[root@ha-manager mha4mysql-node-0.58]# perl Makefile.PL  
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI        ...loaded. (1.627)
- DBD::mysql ...loaded. (4.023)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::node

安装

[root@ha-manager mha4mysql-node-0.58]# make && make install 
cp lib/MHA/BinlogManager.pm blib/lib/MHA/BinlogManager.pm
cp lib/MHA/BinlogPosFindManager.pm blib/lib/MHA/BinlogPosFindManager.pm
cp lib/MHA/BinlogPosFinderXid.pm blib/lib/MHA/BinlogPosFinderXid.pm
cp lib/MHA/BinlogHeaderParser.pm blib/lib/MHA/BinlogHeaderParser.pm
cp lib/MHA/BinlogPosFinder.pm blib/lib/MHA/BinlogPosFinder.pm
cp lib/MHA/BinlogPosFinderElp.pm blib/lib/MHA/BinlogPosFinderElp.pm
cp lib/MHA/NodeUtil.pm blib/lib/MHA/NodeUtil.pm
cp lib/MHA/SlaveUtil.pm blib/lib/MHA/SlaveUtil.pm
cp lib/MHA/NodeConst.pm blib/lib/MHA/NodeConst.pm
cp bin/filter_mysqlbinlog blib/script/filter_mysqlbinlog
/usr/bin/perl "-Iinc" -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/filter_mysqlbinlog
cp bin/apply_diff_relay_logs blib/script/apply_diff_relay_logs
/usr/bin/perl "-Iinc" -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/apply_diff_relay_logs
cp bin/purge_relay_logs blib/script/purge_relay_logs
/usr/bin/perl "-Iinc" -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/purge_relay_logs
cp bin/save_binary_logs blib/script/save_binary_logs
/usr/bin/perl "-Iinc" -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/save_binary_logs
Manifying blib/man1/filter_mysqlbinlog.1
Manifying blib/man1/apply_diff_relay_logs.1
Manifying blib/man1/purge_relay_logs.1
Manifying blib/man1/save_binary_logs.1
Installing /usr/local/share/perl5/MHA/BinlogManager.pm
Installing /usr/local/share/perl5/MHA/BinlogPosFindManager.pm
Installing /usr/local/share/perl5/MHA/BinlogPosFinderXid.pm
Installing /usr/local/share/perl5/MHA/BinlogHeaderParser.pm
Installing /usr/local/share/perl5/MHA/BinlogPosFinder.pm
Installing /usr/local/share/perl5/MHA/BinlogPosFinderElp.pm
Installing /usr/local/share/perl5/MHA/NodeUtil.pm
Installing /usr/local/share/perl5/MHA/SlaveUtil.pm
Installing /usr/local/share/perl5/MHA/NodeConst.pm
Installing /usr/local/share/man/man1/filter_mysqlbinlog.1
Installing /usr/local/share/man/man1/apply_diff_relay_logs.1
Installing /usr/local/share/man/man1/purge_relay_logs.1
Installing /usr/local/share/man/man1/save_binary_logs.1
Installing /usr/local/bin/filter_mysqlbinlog
Installing /usr/local/bin/apply_diff_relay_logs
Installing /usr/local/bin/purge_relay_logs
Installing /usr/local/bin/save_binary_logs
Appending installation info to /usr/lib64/perl5/perllocal.pod

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

[root@ha-manager mha4mysql-node-0.58]# ll /usr/local/bin/
总用量 48
-r-xr-xr-x 1 root root 17639 3月  10 14:23 apply_diff_relay_logs
-r-xr-xr-x 1 root root  4807 3月  10 14:23 filter_mysqlbinlog
-r-xr-xr-x 1 root root  8337 3月  10 14:23 purge_relay_logs
-r-xr-xr-x 1 root root  7525 3月  10 14:23 save_binary_logs
[root@ha-manager mha4mysql-node-0.58]# 

安装mha-mysql-manager

https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz
下载manager包

[root@ha-manager mha4mysql-node-0.58]# wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz
--2023-03-10 10:14:50--  https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz
正在解析主机 github.com (github.com)... 20.205.243.166
正在连接 github.com (github.com)|20.205.243.166|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://objects.githubusercontent.com/github-production-release-asset-2e65be/2093236/ec72c8ae-2de4-11e8-97d2-23e26f1f9623?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230310%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230310T021450Z&X-Amz-Expires=300&X-Amz-Signature=f60ec45902a6e2fa206411f91ce7d231331ddfe6c7900a21af45eaf5171db5b2&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=2093236&response-content-disposition=attachment%3B%20filename%3Dmha4mysql-manager-0.58.tar.gz&response-content-type=application%2Foctet-stream [跟随至新的 URL]
--2023-03-10 10:14:51--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/2093236/ec72c8ae-2de4-11e8-97d2-23e26f1f9623?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230310%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230310T021450Z&X-Amz-Expires=300&X-Amz-Signature=f60ec45902a6e2fa206411f91ce7d231331ddfe6c7900a21af45eaf5171db5b2&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=2093236&response-content-disposition=attachment%3B%20filename%3Dmha4mysql-manager-0.58.tar.gz&response-content-type=application%2Foctet-stream
正在解析主机 objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133
正在连接 objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:119801 (117K) [application/octet-stream]
正在保存至: “mha4mysql-manager-0.58.tar.gz”

100%[=========================================================================================================================>] 119,801     85.4KB/s 用时 1.4s   

2023-03-10 10:14:53 (85.4 KB/s) - 已保存 “mha4mysql-manager-0.58.tar.gz” [119801/119801])

解压

[root@ha-manager mha4mysql-node-0.58]# tar -zxvf mha4mysql-manager-0.58.tar.gz  
mha4mysql-manager-0.58/
mha4mysql-manager-0.58/inc/
mha4mysql-manager-0.58/inc/Module/
mha4mysql-manager-0.58/inc/Module/Install/
mha4mysql-manager-0.58/inc/Module/Install/Fetch.pm
mha4mysql-manager-0.58/inc/Module/Install/Metadata.pm
mha4mysql-manager-0.58/inc/Module/Install/AutoInstall.pm
mha4mysql-manager-0.58/inc/Module/Install/Win32.pm
mha4mysql-manager-0.58/inc/Module/Install/WriteAll.pm
mha4mysql-manager-0.58/inc/Module/Install/Can.pm
mha4mysql-manager-0.58/inc/Module/Install/Include.pm
mha4mysql-manager-0.58/inc/Module/Install/Makefile.pm
mha4mysql-manager-0.58/inc/Module/Install/Scripts.pm
mha4mysql-manager-0.58/inc/Module/Install/Base.pm
mha4mysql-manager-0.58/inc/Module/AutoInstall.pm
mha4mysql-manager-0.58/inc/Module/Install.pm
mha4mysql-manager-0.58/debian/
mha4mysql-manager-0.58/debian/compat
mha4mysql-manager-0.58/debian/changelog
mha4mysql-manager-0.58/debian/docs
mha4mysql-manager-0.58/debian/rules
mha4mysql-manager-0.58/debian/copyright
mha4mysql-manager-0.58/debian/control
mha4mysql-manager-0.58/bin/
mha4mysql-manager-0.58/bin/masterha_check_status
mha4mysql-manager-0.58/bin/masterha_check_ssh
mha4mysql-manager-0.58/bin/masterha_master_monitor
mha4mysql-manager-0.58/bin/masterha_manager
mha4mysql-manager-0.58/bin/masterha_master_switch
mha4mysql-manager-0.58/bin/masterha_stop
mha4mysql-manager-0.58/bin/masterha_secondary_check
mha4mysql-manager-0.58/bin/masterha_check_repl
mha4mysql-manager-0.58/bin/masterha_conf_host
mha4mysql-manager-0.58/AUTHORS
mha4mysql-manager-0.58/MANIFEST
mha4mysql-manager-0.58/tests/
mha4mysql-manager-0.58/tests/intro.txt
mha4mysql-manager-0.58/tests/t/
mha4mysql-manager-0.58/tests/t/t_online_3tier.sh
mha4mysql-manager-0.58/tests/t/t_mm_ro_fail.sh
mha4mysql-manager-0.58/tests/t/t_large_data_slow.sh
mha4mysql-manager-0.58/tests/t/t_recover_master_fail.sh
mha4mysql-manager-0.58/tests/t/t_4tier.sh
mha4mysql-manager-0.58/tests/t/t_online_slave_sql_stop.sh
mha4mysql-manager-0.58/tests/t/t_online_3tier_slave.sh
mha4mysql-manager-0.58/tests/t/grant_nopass.sql
mha4mysql-manager-0.58/tests/t/t_manual.sh
mha4mysql-manager-0.58/tests/t/t_mm_noslaves.sh
mha4mysql-manager-0.58/tests/t/t_online_mm_3tier.sh
mha4mysql-manager-0.58/tests/t/t_advisory_select.sh
mha4mysql-manager-0.58/tests/t/mha_test_mm_online.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_ignore_nostart.sh
mha4mysql-manager-0.58/tests/t/t_dual_master_error.sh
mha4mysql-manager-0.58/tests/t/t_large_data_sql_fail.sh
mha4mysql-manager-0.58/tests/t/t_mm_subm_dead.sh
mha4mysql-manager-0.58/tests/t/t_online_3tier_slave_keep.sh
mha4mysql-manager-0.58/tests/t/mha_test_connect.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_latest_recovery3.sh
mha4mysql-manager-0.58/tests/t/t_save_master_log.sh
mha4mysql-manager-0.58/tests/t/t_needsync_1_ssh.sh
mha4mysql-manager-0.58/tests/t/t_mm.sh
mha4mysql-manager-0.58/tests/t/t_needsync_flush_slave.sh
mha4mysql-manager-0.58/tests/t/t_online_slave_pass.sh
mha4mysql-manager-0.58/tests/t/t_ignore_start.sh
mha4mysql-manager-0.58/tests/t/my.cnf
mha4mysql-manager-0.58/tests/t/t_large_data_sql_stop.sh
mha4mysql-manager-0.58/tests/t/t_filter_incorrect.sh
mha4mysql-manager-0.58/tests/t/start_m.sh
mha4mysql-manager-0.58/tests/t/t_large_data_bulk.sh
mha4mysql-manager-0.58/tests/t/master_ip_failover
mha4mysql-manager-0.58/tests/t/t_large_data.sh
mha4mysql-manager-0.58/tests/t/t_new_master_heavy_wait.sh
mha4mysql-manager-0.58/tests/t/t_data_io_error.sh
mha4mysql-manager-0.58/tests/t/t_large_data_tran.sh
mha4mysql-manager-0.58/tests/t/t_needsync_1_pass.sh
mha4mysql-manager-0.58/tests/t/t_save_master_log_pass.sh
mha4mysql-manager-0.58/tests/t/my-row.cnf
mha4mysql-manager-0.58/tests/t/t_online_slave.sh
mha4mysql-manager-0.58/tests/t/t_latest_recovery2.sh
mha4mysql-manager-0.58/tests/t/t_online_filter.sh
mha4mysql-manager-0.58/tests/t/stop_s1.sh
mha4mysql-manager-0.58/tests/t/init.sh
mha4mysql-manager-0.58/tests/t/t_ignore_recovery1.sh
mha4mysql-manager-0.58/tests/t/t_online_busy.sh
mha4mysql-manager-0.58/tests/t/t_keep_relay_log_purge.sh
mha4mysql-manager-0.58/tests/t/t_needsync_1.sh
mha4mysql-manager-0.58/tests/t/t_apply_many_logs.sh
mha4mysql-manager-0.58/tests/t/t_apply_many_logs3.sh
mha4mysql-manager-0.58/tests/t/t_slave_stop.sh
mha4mysql-manager-0.58/tests/t/t_slave_incorrect.sh
mha4mysql-manager-0.58/tests/t/bulk_tran_insert.pl
mha4mysql-manager-0.58/tests/t/mha_test_multi_online.cnf.tmpl
mha4mysql-manager-0.58/tests/t/stop_m.sh
mha4mysql-manager-0.58/tests/t/start_s4.sh
mha4mysql-manager-0.58/tests/t/t_4tier_subm_dead.sh
mha4mysql-manager-0.58/tests/t/mha_test_online.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_slave_sql_start3.sh
mha4mysql-manager-0.58/tests/t/run_bg.sh
mha4mysql-manager-0.58/tests/t/t_needsync_2_pass.sh
mha4mysql-manager-0.58/tests/t/t_apply_many_logs2.sh
mha4mysql-manager-0.58/tests/t/mha_test_mm.cnf.tmpl
mha4mysql-manager-0.58/tests/t/stop_s4.sh
mha4mysql-manager-0.58/tests/t/t_needsync_1_nocm.sh
mha4mysql-manager-0.58/tests/t/t_ignore_recovery4.sh
mha4mysql-manager-0.58/tests/t/t_advisory_connect.sh
mha4mysql-manager-0.58/tests/t/t_normal_crash.sh
mha4mysql-manager-0.58/tests/t/t_mm_normal_skip_reset.sh
mha4mysql-manager-0.58/tests/t/t_slave_sql_start2.sh
mha4mysql-manager-0.58/tests/t/t_slave_sql_start.sh
mha4mysql-manager-0.58/tests/t/t_normal_crash_nocm.sh
mha4mysql-manager-0.58/tests/t/t_mm_3tier_subm_dead.sh
mha4mysql-manager-0.58/tests/t/mha_test_err1.cnf.tmpl
mha4mysql-manager-0.58/tests/t/mha_test_reset.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_needsync_fail.sh
mha4mysql-manager-0.58/tests/t/t_needsync_1_nopass.sh
mha4mysql-manager-0.58/tests/t/start_s1.sh
mha4mysql-manager-0.58/tests/t/t_needsync_flush.sh
mha4mysql-manager-0.58/tests/t/run.sh
mha4mysql-manager-0.58/tests/t/master_ip_failover_blank
mha4mysql-manager-0.58/tests/t/mha_test.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_save_master_log_ssh.sh
mha4mysql-manager-0.58/tests/t/kill_m.sh
mha4mysql-manager-0.58/tests/t/t_online_slave_fail.sh
mha4mysql-manager-0.58/tests/t/t_binary.sh
mha4mysql-manager-0.58/tests/t/t_needsync_flush3.sh
mha4mysql-manager-0.58/tests/t/t_recover_slave_fail.sh
mha4mysql-manager-0.58/tests/t/mha_test_ignore.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_ignore_recovery3.sh
mha4mysql-manager-0.58/tests/t/force_start_m.sh
mha4mysql-manager-0.58/tests/t/t_recover_slave_ok.sh
mha4mysql-manager-0.58/tests/t/t_mm_normal.sh
mha4mysql-manager-0.58/tests/t/start_s2.sh
mha4mysql-manager-0.58/tests/t/t_online_mm_3tier_slave.sh
mha4mysql-manager-0.58/tests/t/insert.pl
mha4mysql-manager-0.58/tests/t/grant.sql
mha4mysql-manager-0.58/tests/t/stop_s2.sh
mha4mysql-manager-0.58/tests/t/waitpid
mha4mysql-manager-0.58/tests/t/t_mm_subm_dead_many.sh
mha4mysql-manager-0.58/tests/t/t_ignore_recovery2.sh
mha4mysql-manager-0.58/tests/t/tran_insert.pl
mha4mysql-manager-0.58/tests/t/insert_binary.pl
mha4mysql-manager-0.58/tests/t/t_online_mm.sh
mha4mysql-manager-0.58/tests/t/mha_test_nopass.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_needsync_2.sh
mha4mysql-manager-0.58/tests/t/mha_test_online_pass.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_needsync_2_ssh.sh
mha4mysql-manager-0.58/tests/t/mha_test_multi.cnf.tmpl
mha4mysql-manager-0.58/tests/t/run_tests
mha4mysql-manager-0.58/tests/t/mha_test_latest.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_online_mm_skip_reset.sh
mha4mysql-manager-0.58/tests/t/t_online_normal.sh
mha4mysql-manager-0.58/tests/t/env.sh
mha4mysql-manager-0.58/tests/t/t_needsync_flush2.sh
mha4mysql-manager-0.58/tests/t/t_conf.sh
mha4mysql-manager-0.58/tests/t/t_mm_subm_dead_noslave.sh
mha4mysql-manager-0.58/tests/t/mha_test_ssh.cnf.tmpl
mha4mysql-manager-0.58/tests/t/mha_test_err2.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_mm_3tier.sh
mha4mysql-manager-0.58/tests/t/t_no_relay_log.sh
mha4mysql-manager-0.58/tests/t/change_relay_log_info.sh
mha4mysql-manager-0.58/tests/t/t_new_master_heavy.sh
mha4mysql-manager-0.58/tests/t/mha_test_pass.cnf.tmpl
mha4mysql-manager-0.58/tests/t/t_latest_recovery1.sh
mha4mysql-manager-0.58/tests/t/t_recover_slave_fail2.sh
mha4mysql-manager-0.58/tests/t/t_large_data_bulk_slow.sh
mha4mysql-manager-0.58/tests/t/check
mha4mysql-manager-0.58/tests/t/t_large_data_slow2.sh
mha4mysql-manager-0.58/tests/run_suites.sh
mha4mysql-manager-0.58/t/
mha4mysql-manager-0.58/t/perlcriticrc
mha4mysql-manager-0.58/t/99-perlcritic.t
mha4mysql-manager-0.58/README
mha4mysql-manager-0.58/samples/
mha4mysql-manager-0.58/samples/scripts/
mha4mysql-manager-0.58/samples/scripts/master_ip_failover
mha4mysql-manager-0.58/samples/scripts/power_manager
mha4mysql-manager-0.58/samples/scripts/send_report
mha4mysql-manager-0.58/samples/scripts/master_ip_online_change
mha4mysql-manager-0.58/samples/conf/
mha4mysql-manager-0.58/samples/conf/app1.cnf
mha4mysql-manager-0.58/samples/conf/masterha_default.cnf
mha4mysql-manager-0.58/COPYING
mha4mysql-manager-0.58/META.yml
mha4mysql-manager-0.58/lib/
mha4mysql-manager-0.58/lib/MHA/
mha4mysql-manager-0.58/lib/MHA/ManagerAdmin.pm
mha4mysql-manager-0.58/lib/MHA/Server.pm
mha4mysql-manager-0.58/lib/MHA/MasterRotate.pm
mha4mysql-manager-0.58/lib/MHA/Config.pm
mha4mysql-manager-0.58/lib/MHA/ManagerAdminWrapper.pm
mha4mysql-manager-0.58/lib/MHA/ServerManager.pm
mha4mysql-manager-0.58/lib/MHA/HealthCheck.pm
mha4mysql-manager-0.58/lib/MHA/ManagerConst.pm
mha4mysql-manager-0.58/lib/MHA/DBHelper.pm
mha4mysql-manager-0.58/lib/MHA/SSHCheck.pm
mha4mysql-manager-0.58/lib/MHA/FileStatus.pm
mha4mysql-manager-0.58/lib/MHA/ManagerUtil.pm
mha4mysql-manager-0.58/lib/MHA/MasterFailover.pm
mha4mysql-manager-0.58/lib/MHA/MasterMonitor.pm
mha4mysql-manager-0.58/Makefile.PL
mha4mysql-manager-0.58/rpm/
mha4mysql-manager-0.58/rpm/masterha_manager.spec

编译

[root@ha-manager mha4mysql-node-0.58]#  cd mha4mysql-manager-0.58/
[root@client mha4mysql-manager-0.58]# ls
AUTHORS  bin  COPYING  debian  inc  lib  Makefile.PL  MANIFEST  META.yml  README  rpm  samples  t  tests

[root@ha-manager mha4mysql-manager-0.58]#
[root@ha-manager mha4mysql-manager-0.58]# 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.58)
*** Module::AutoInstall configuration finished.
Writing Makefile for mha4mysql::manager
Writing MYMETA.yml and MYMETA.json

安装

[root@ha-manager mha4mysql-manager-0.58]# make && make install 

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

[root@ha-manager mha4mysql-manager-0.58]# ll /usr/local/bin/
总用量 88
-r-xr-xr-x 1 root root 17639 3月  10 14:23 apply_diff_relay_logs
-r-xr-xr-x 1 root root  4807 3月  10 14:23 filter_mysqlbinlog
-r-xr-xr-x 1 root root  1995 3月  10 14:37 masterha_check_repl
-r-xr-xr-x 1 root root  1779 3月  10 14:37 masterha_check_ssh
-r-xr-xr-x 1 root root  1865 3月  10 14:37 masterha_check_status
-r-xr-xr-x 1 root root  3201 3月  10 14:37 masterha_conf_host
-r-xr-xr-x 1 root root  2517 3月  10 14:37 masterha_manager
-r-xr-xr-x 1 root root  2165 3月  10 14:37 masterha_master_monitor
-r-xr-xr-x 1 root root  2373 3月  10 14:37 masterha_master_switch
-r-xr-xr-x 1 root root  5172 3月  10 14:37 masterha_secondary_check
-r-xr-xr-x 1 root root  1739 3月  10 14:37 masterha_stop
-r-xr-xr-x 1 root root  8337 3月  10 14:23 purge_relay_logs
-r-xr-xr-x 1 root root  7525 3月  10 14:23 save_binary_logs
[root@ha-manager mha4mysql-manager-0.58]# ll /usr/local/bin/
总用量 88
-r-xr-xr-x 1 root root 17639 3月  10 14:23 apply_diff_relay_logs
-r-xr-xr-x 1 root root  4807 3月  10 14:23 filter_mysqlbinlog
-r-xr-xr-x 1 root root  1995 3月  10 14:37 masterha_check_repl
-r-xr-xr-x 1 root root  1779 3月  10 14:37 masterha_check_ssh
-r-xr-xr-x 1 root root  1865 3月  10 14:37 masterha_check_status
-r-xr-xr-x 1 root root  3201 3月  10 14:37 masterha_conf_host
-r-xr-xr-x 1 root root  2517 3月  10 14:37 masterha_manager
-r-xr-xr-x 1 root root  2165 3月  10 14:37 masterha_master_monitor
-r-xr-xr-x 1 root root  2373 3月  10 14:37 masterha_master_switch
-r-xr-xr-x 1 root root  5172 3月  10 14:37 masterha_secondary_check
-r-xr-xr-x 1 root root  1739 3月  10 14:37 masterha_stop
-r-xr-xr-x 1 root root  8337 3月  10 14:23 purge_relay_logs
-r-xr-xr-x 1 root root  7525 3月  10 14:23 save_binary_logs

安装完毕,配置MHA

创建mha工作目录:

mkdir -p /etc/masterha/

拷贝配置文件:

[root@ha-manager ~]# cp /usr/local/src/mha4mysql-manager-0.58/samples/conf/app1.cnf  /etc/masterha/

配置文件更改:

[server default]
manager_log=/var/log/masterha/app1/manager.log   #日志
manager_workdir=/var/log/masterha/app1  #app1工作目录,如果有多个mysql集群,可以设置为多个
master_binlog_dir=/var/lib/mysql/   #数据库binlog日志存放目录,在my.cnf中配置
master_ip_failover_script=/usr/local/bin/master_ip_failover   #故障切换,vip漂移脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change 
password=REDhat@123   #mysql root密码
ping_interval=1
remote_workdir=/tmp   #故障发生,临时存放的binlog地方
repl_password=REDhat@123   #用于复制的slave 用于密码
repl_user=slave   #见名知意
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.5.100 -s 192.168.5.200  
shutdown_script="" 
ssh_user=root   #见名知意
user=mha  #见名知意

[server1]
hostname=192.168.5.140
port=3306

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

[server3]
hostname=192.168.5.160
port=3306

复制vip管理脚本:

 cp /usr/local/bin/scripts/master_ip_failover  /usr/local/bin/

配置vip:

[root@ha-manager bin]# cat master_ip_failover 
#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;

my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);
############添加配置###############
my $vip = '192.168.5.161/24';   #虚拟ip地址
my  $ifdev = 'ens33';  #指定绑定网卡
my $key = '1';   #绑定虚拟网卡序号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";      #启动虚拟ip
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";  #关闭虚拟ip
my $exit_code = 0;  #退出代码0
###################################

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,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);

exit &main();

sub main {
  if ( $command eq "stop" || $command eq "stopssh" ) {

    # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
    # If you manage master ip address at global catalog database,
    # invalidate orig_master_ip here.
    my $exit_code = 1;
    eval {

      # updating global catalog, etc
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {

    # all arguments are passed.
    # If you manage master ip address at global catalog database,
    # activate new_master_ip here.
    # You can also grant write access (create user, set read_only=0, etc) here.
    my $exit_code = 10;
    eval {
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      print "Creating app user on the new master..\n";
      FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      ## Update master ip on the catalog database, etc
#      FIXME_xxx;

      $exit_code = 0;
    };
    if ($@) {
      warn $@;

      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

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";
}

复制vip使用的各个脚本至/usr/llocal/bin

[root@ha-manager masterha]# cp -rp /usr/local/src/mha4mysql-manager-0.58/samples/scripts/ /usr/local/bin/
[root@ha-manager masterha]# ll /usr/local/bin/scripts/
总用量 32
-rwxr-xr-x 1 ld ld  3648 3月  23 2018 master_ip_failover
-rwxr-xr-x 1 ld ld  9870 3月  23 2018 master_ip_online_change
-rwxr-xr-x 1 ld ld 11867 3月  23 2018 power_manager
-rwxr-xr-x 1 ld ld  1360 3月  23 2018 send_report

做个软连接,全局可以使用脚本

ln -s /usr/local/bin/ /usr/bin/

检查免密配置:

[root@ha-manager masterha]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Thu Mar 16 16:47:17 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Mar 16 16:47:17 2023 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Thu Mar 16 16:47:17 2023 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Thu Mar 16 16:47:17 2023 - [info] Starting SSH connection tests..
Thu Mar 16 16:47:19 2023 - [debug] 
Thu Mar 16 16:47:17 2023 - [debug]  Connecting via SSH from root@192.168.5.140(192.168.5.140:22) to root@192.168.5.150(192.168.5.150:22)..
Thu Mar 16 16:47:18 2023 - [debug]   ok.
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.140(192.168.5.140:22) to root@192.168.5.160(192.168.5.160:22)..
Thu Mar 16 16:47:18 2023 - [debug]   ok.
Thu Mar 16 16:47:20 2023 - [debug] 
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.160(192.168.5.160:22) to root@192.168.5.140(192.168.5.140:22)..
Thu Mar 16 16:47:19 2023 - [debug]   ok.
Thu Mar 16 16:47:19 2023 - [debug]  Connecting via SSH from root@192.168.5.160(192.168.5.160:22) to root@192.168.5.150(192.168.5.150:22)..
Thu Mar 16 16:47:19 2023 - [debug]   ok.
Thu Mar 16 16:47:20 2023 - [debug] 
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.150(192.168.5.150:22) to root@192.168.5.140(192.168.5.140:22)..
Thu Mar 16 16:47:18 2023 - [debug]   ok.
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.150(192.168.5.150:22) to root@192.168.5.160(192.168.5.160:22)..
Thu Mar 16 16:47:19 2023 - [debug]   ok.
Thu Mar 16 16:47:20 2023 - [info] All SSH connection tests passed successfully.
[root@ha-manager masterha]# 

检查 mysql 主从复制状态 出现MySQL Replication Health is OK

[root@ha-manager masterha]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Thu Mar 16 16:47:17 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Mar 16 16:47:17 2023 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Thu Mar 16 16:47:17 2023 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Thu Mar 16 16:47:17 2023 - [info] Starting SSH connection tests..
Thu Mar 16 16:47:19 2023 - [debug] 
Thu Mar 16 16:47:17 2023 - [debug]  Connecting via SSH from root@192.168.5.140(192.168.5.140:22) to root@192.168.5.150(192.168.5.150:22)..
Thu Mar 16 16:47:18 2023 - [debug]   ok.
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.140(192.168.5.140:22) to root@192.168.5.160(192.168.5.160:22)..
Thu Mar 16 16:47:18 2023 - [debug]   ok.
Thu Mar 16 16:47:20 2023 - [debug] 
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.160(192.168.5.160:22) to root@192.168.5.140(192.168.5.140:22)..
Thu Mar 16 16:47:19 2023 - [debug]   ok.
Thu Mar 16 16:47:19 2023 - [debug]  Connecting via SSH from root@192.168.5.160(192.168.5.160:22) to root@192.168.5.150(192.168.5.150:22)..
Thu Mar 16 16:47:19 2023 - [debug]   ok.
Thu Mar 16 16:47:20 2023 - [debug] 
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.150(192.168.5.150:22) to root@192.168.5.140(192.168.5.140:22)..
Thu Mar 16 16:47:18 2023 - [debug]   ok.
Thu Mar 16 16:47:18 2023 - [debug]  Connecting via SSH from root@192.168.5.150(192.168.5.150:22) to root@192.168.5.160(192.168.5.160:22)..
Thu Mar 16 16:47:19 2023 - [debug]   ok.
Thu Mar 16 16:47:20 2023 - [info] All SSH connection tests passed successfully.
[root@ha-manager masterha]# 
[root@ha-manager masterha]# masterha_check_repl -conf=/etc/masterha/app1.cnf
太多debug 信息。。删略。
Thu Mar 16 16:48:57 2023 - [info] Dead Servers:
Thu Mar 16 16:48:57 2023 - [info] Alive Servers:

Thu Mar 16 16:48:57 2023 - [info] Alive Slaves:

Thu Mar 16 16:49:02 2023 - [info]  OK.
Thu Mar 16 16:49:02 2023 - [warning] shutdown_script is not defined.
Thu Mar 16 16:49:02 2023 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
[root@ha-manager masterha]# 

启动MHA

nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &


--remove_dead_master_conf:该参数代表当发生主从切换后,老的主库的 ip 将会从配置文件中移除。
--manger_log:日志存放位置。
--ignore_last_failover:在缺省情况下,如果 MHA 检测到连续发生宕机,且两次宕机间隔不足 8 小时的话,则不会进行 Failover, 之所以这样限制是为了避免 ping-pong 效应。该参数代表忽略上次 MHA 触发切换产生的文件,默认情况下,MHA 发生切换后会在日志记目录,也就是上面设置的日志app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为--ignore_last_failover。

故障模拟

原来master为140. 模拟主库mysql服务停止。自动切换为150主

[root@mysql-master01 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2023-03-17 09:33:46 CST; 5min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 889 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 831 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 949 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─949 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

3月 17 09:33:45 mysql-master01 systemd[1]: Starting MySQL Server...
3月 17 09:33:46 mysql-master01 systemd[1]: Started MySQL Server.
[root@mysql-master01 ~]# systemctl stop mysqld

原slave02 切换主库为slave01

[root@mysql-slave02 ~]# mysql -uroot -pREDhat@123
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 18
Server version: 5.7.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.150
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: slave-01-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-slave02-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: slave-01-bin.000004
             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: 538
              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: 8589d150-be42-11ed-bf70-000c29929f2a
             Master_Info_File: /var/lib/mysql/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)

源配置文件会把源主剔除,就是5.140 会被剔除

[root@ha-manager masterha]# cd /etc/masterha/
[root@ha-manager masterha]# ls
app1.cnf  start.sh
[root@ha-manager masterha]# cat app1.cnf 
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/var/lib/mysql/
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=REDhat@123
ping_interval=1
remote_workdir=/tmp
repl_password=REDhat@123
repl_user=slave
shutdown_script=""
ssh_user=root
user=mha

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

[server3]
hostname=192.168.5.160
port=3306

manger 日志输出:



[root@ha-manager ~]# tail -f  /var/log/masterha/app1/manager.log 

Fri Mar 17 09:29:27 2023 - [info] Checking master_ip_failover_script status:
Fri Mar 17 09:29:27 2023 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.5.140 --orig_master_ip=192.168.5.140 --orig_master_port=3306 
Fri Mar 17 09:29:27 2023 - [info]  OK.
Fri Mar 17 09:29:27 2023 - [warning] shutdown_script is not defined.
Fri Mar 17 09:29:27 2023 - [info] Set master ping interval 1 seconds.
Fri Mar 17 09:29:27 2023 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Fri Mar 17 09:29:27 2023 - [info] Starting ping health check on 192.168.5.140(192.168.5.140:3306)..
Fri Mar 17 09:29:27 2023 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Fri Mar 17 09:33:18 2023 - [info] Got terminate signal. Exit.
Fri Mar 17 09:38:35 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Mar 17 09:38:35 2023 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Mar 17 09:38:35 2023 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Mar 17 09:38:35 2023 - [info] MHA::MasterMonitor version 0.58.
Fri Mar 17 09:38:36 2023 - [info] GTID failover mode = 0
Fri Mar 17 09:38:36 2023 - [info] Dead Servers:
Fri Mar 17 09:38:36 2023 - [info] Alive Servers:
Fri Mar 17 09:38:36 2023 - [info]   192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:38:36 2023 - [info]   192.168.5.150(192.168.5.150:3306)
Fri Mar 17 09:38:36 2023 - [info]   192.168.5.160(192.168.5.160:3306)
Fri Mar 17 09:38:36 2023 - [info] Alive Slaves:
Fri Mar 17 09:38:36 2023 - [info]   192.168.5.150(192.168.5.150:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:38:36 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:38:36 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 17 09:38:36 2023 - [info]   192.168.5.160(192.168.5.160:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:38:36 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:38:36 2023 - [info] Current Alive Master: 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:38:36 2023 - [info] Checking slave configurations..
Fri Mar 17 09:38:36 2023 - [info]  read_only=1 is not set on slave 192.168.5.150(192.168.5.150:3306).
Fri Mar 17 09:38:36 2023 - [warning]  relay_log_purge=0 is not set on slave 192.168.5.150(192.168.5.150:3306).
Fri Mar 17 09:38:36 2023 - [info]  read_only=1 is not set on slave 192.168.5.160(192.168.5.160:3306).
Fri Mar 17 09:38:36 2023 - [warning]  relay_log_purge=0 is not set on slave 192.168.5.160(192.168.5.160:3306).
Fri Mar 17 09:38:36 2023 - [info] Checking replication filtering settings..
Fri Mar 17 09:38:36 2023 - [info]  binlog_do_db= data1, binlog_ignore_db= mysql
Fri Mar 17 09:38:36 2023 - [info]  Replication filtering check ok.
Fri Mar 17 09:38:36 2023 - [info] GTID (with auto-pos) is not supported
Fri Mar 17 09:38:36 2023 - [info] Starting SSH connection tests..
Fri Mar 17 09:38:38 2023 - [info] All SSH connection tests passed successfully.
Fri Mar 17 09:38:38 2023 - [info] Checking MHA Node version..
Fri Mar 17 09:38:39 2023 - [info]  Version check ok.
Fri Mar 17 09:38:39 2023 - [info] Checking SSH publickey authentication settings on the current master..
Fri Mar 17 09:38:40 2023 - [info] HealthCheck: SSH to 192.168.5.140 is reachable.
Fri Mar 17 09:38:40 2023 - [info] Master MHA Node version is 0.58.
Fri Mar 17 09:38:40 2023 - [info] Checking recovery script configurations on 192.168.5.140(192.168.5.140:3306)..
Fri Mar 17 09:38:40 2023 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql/ --output_file=/tmp/save_binary_logs_test --manager_version=0.58 --start_file=master01-bin.000005 
Fri Mar 17 09:38:40 2023 - [info]   Connecting to root@192.168.5.140(192.168.5.140:22).. 
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql/, up to master01-bin.000005
Fri Mar 17 09:38:40 2023 - [info] Binlog setting check done.
Fri Mar 17 09:38:40 2023 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Mar 17 09:38:40 2023 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.5.150 --slave_ip=192.168.5.150 --slave_port=3306 --workdir=/tmp --target_version=5.7.41-log --manager_version=0.58 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Fri Mar 17 09:38:40 2023 - [info]   Connecting to root@192.168.5.150(192.168.5.150:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mysql-slave01-relay-bin.000017
    Temporary relay log file is /var/lib/mysql/mysql-slave01-relay-bin.000017
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    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 17 09:38:41 2023 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.5.160 --slave_ip=192.168.5.160 --slave_port=3306 --workdir=/tmp --target_version=5.7.41-log --manager_version=0.58 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Fri Mar 17 09:38:41 2023 - [info]   Connecting to root@192.168.5.160(192.168.5.160:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mysql-slave02-relay-bin.000010
    Temporary relay log file is /var/lib/mysql/mysql-slave02-relay-bin.000010
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    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 17 09:38:41 2023 - [info] Slaves settings check done.
Fri Mar 17 09:38:41 2023 - [info] 
192.168.5.140(192.168.5.140:3306) (current master)
 +--192.168.5.150(192.168.5.150:3306)
 +--192.168.5.160(192.168.5.160:3306)

Fri Mar 17 09:38:41 2023 - [info] Checking master_ip_failover_script status:
Fri Mar 17 09:38:41 2023 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.5.140 --orig_master_ip=192.168.5.140 --orig_master_port=3306 
Fri Mar 17 09:38:41 2023 - [info]  OK.
Fri Mar 17 09:38:41 2023 - [warning] shutdown_script is not defined.
Fri Mar 17 09:38:41 2023 - [info] Set master ping interval 1 seconds.
Fri Mar 17 09:38:41 2023 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Fri Mar 17 09:38:41 2023 - [info] Starting ping health check on 192.168.5.140(192.168.5.140:3306)..
Fri Mar 17 09:38:41 2023 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..


Fri Mar 17 09:41:15 2023 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Fri Mar 17 09:41:15 2023 - [info] Executing SSH check script: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql/ --output_file=/tmp/save_binary_logs_test --manager_version=0.58 --binlog_prefix=master01-bin
Fri Mar 17 09:41:16 2023 - [info] HealthCheck: SSH to 192.168.5.140 is reachable.
Fri Mar 17 09:41:16 2023 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.5.140' (111))
Fri Mar 17 09:41:16 2023 - [warning] Connection failed 2 time(s)..
Fri Mar 17 09:41:17 2023 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.5.140' (111))
Fri Mar 17 09:41:17 2023 - [warning] Connection failed 3 time(s)..
Fri Mar 17 09:41:18 2023 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.5.140' (111))
Fri Mar 17 09:41:18 2023 - [warning] Connection failed 4 time(s)..
Fri Mar 17 09:41:18 2023 - [warning] Master is not reachable from health checker!
Fri Mar 17 09:41:18 2023 - [warning] Master 192.168.5.140(192.168.5.140:3306) is not reachable!
Fri Mar 17 09:41:18 2023 - [warning] SSH is reachable.
Fri Mar 17 09:41:18 2023 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/masterha/app1.cnf again, and trying to connect to all servers to check server status..
Fri Mar 17 09:41:18 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Mar 17 09:41:18 2023 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Mar 17 09:41:18 2023 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Mar 17 09:41:19 2023 - [info] GTID failover mode = 0
Fri Mar 17 09:41:19 2023 - [info] Dead Servers:
Fri Mar 17 09:41:19 2023 - [info]   192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:19 2023 - [info] Alive Servers:
Fri Mar 17 09:41:19 2023 - [info]   192.168.5.150(192.168.5.150:3306)
Fri Mar 17 09:41:19 2023 - [info]   192.168.5.160(192.168.5.160:3306)
Fri Mar 17 09:41:19 2023 - [info] Alive Slaves:
Fri Mar 17 09:41:19 2023 - [info]   192.168.5.150(192.168.5.150:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:19 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:19 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 17 09:41:19 2023 - [info]   192.168.5.160(192.168.5.160:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:19 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:19 2023 - [info] Checking slave configurations..
Fri Mar 17 09:41:19 2023 - [info]  read_only=1 is not set on slave 192.168.5.150(192.168.5.150:3306).
Fri Mar 17 09:41:19 2023 - [warning]  relay_log_purge=0 is not set on slave 192.168.5.150(192.168.5.150:3306).
Fri Mar 17 09:41:19 2023 - [info]  read_only=1 is not set on slave 192.168.5.160(192.168.5.160:3306).
Fri Mar 17 09:41:19 2023 - [warning]  relay_log_purge=0 is not set on slave 192.168.5.160(192.168.5.160:3306).
Fri Mar 17 09:41:19 2023 - [info] Checking replication filtering settings..
Fri Mar 17 09:41:19 2023 - [info]  Replication filtering check ok.
Fri Mar 17 09:41:19 2023 - [info] Master is down!
Fri Mar 17 09:41:19 2023 - [info] Terminating monitoring script.
Fri Mar 17 09:41:19 2023 - [info] Got exit code 20 (Master dead).
Fri Mar 17 09:41:19 2023 - [info] MHA::MasterFailover version 0.58.
Fri Mar 17 09:41:19 2023 - [info] Starting master failover.
Fri Mar 17 09:41:19 2023 - [info] 
Fri Mar 17 09:41:19 2023 - [info] * Phase 1: Configuration Check Phase..
Fri Mar 17 09:41:19 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] GTID failover mode = 0
Fri Mar 17 09:41:21 2023 - [info] Dead Servers:
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info] Checking master reachability via MySQL(double check)...
Fri Mar 17 09:41:21 2023 - [info]  ok.
Fri Mar 17 09:41:21 2023 - [info] Alive Servers:
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.150(192.168.5.150:3306)
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.160(192.168.5.160:3306)
Fri Mar 17 09:41:21 2023 - [info] Alive Slaves:
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.150(192.168.5.150:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.160(192.168.5.160:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info] Starting Non-GTID based failover.
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] ** Phase 1: Configuration Check Phase completed.
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 2: Dead Master Shutdown Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] Forcing shutdown so that applications never connect to the current master..
Fri Mar 17 09:41:21 2023 - [info] Executing master IP deactivation script:
Fri Mar 17 09:41:21 2023 - [info]   /usr/local/bin/master_ip_failover --orig_master_host=192.168.5.140 --orig_master_ip=192.168.5.140 --orig_master_port=3306 --command=stopssh --ssh_user=root  
Fri Mar 17 09:41:21 2023 - [info]  done.
Fri Mar 17 09:41:21 2023 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Fri Mar 17 09:41:21 2023 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 3: Master Recovery Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] The latest binary log file/position on all slaves is master01-bin.000005:154
Fri Mar 17 09:41:21 2023 - [info] Latest slaves (Slaves that received relay log files to the latest):
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.150(192.168.5.150:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.160(192.168.5.160:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info] The oldest binary log file/position on all slaves is master01-bin.000005:154
Fri Mar 17 09:41:21 2023 - [info] Oldest slaves:
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.150(192.168.5.150:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.160(192.168.5.160:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] Fetching dead master's binary logs..
Fri Mar 17 09:41:21 2023 - [info] Executing command on the dead master 192.168.5.140(192.168.5.140:3306): save_binary_logs --command=save --start_file=master01-bin.000005  --start_pos=154 --binlog_dir=/var/lib/mysql/ --output_file=/tmp/saved_master_binlog_from_192.168.5.140_3306_20230317094119.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.58
  Creating /tmp if not exists..    ok.
 Concat binary/relay logs from master01-bin.000005 pos 154 to master01-bin.000005 EOF into /tmp/saved_master_binlog_from_192.168.5.140_3306_20230317094119.binlog ..
 Binlog Checksum enabled
  Dumping binlog format description event, from position 0 to 154.. ok.
  No need to dump effective binlog data from /var/lib/mysql//master01-bin.000005 (pos starts 154, filesize 154). Skipping.
 Binlog Checksum enabled
 /tmp/saved_master_binlog_from_192.168.5.140_3306_20230317094119.binlog has no effective data events.
Event not exists.
Fri Mar 17 09:41:21 2023 - [info] Additional events were not found from the orig master. No need to save.
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 3.3: Determining New Master Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Fri Mar 17 09:41:21 2023 - [info] All slaves received relay logs to the same position. No need to resync each other.
Fri Mar 17 09:41:21 2023 - [info] Searching new master from slaves..
Fri Mar 17 09:41:21 2023 - [info]  Candidate masters from the configuration file:
Fri Mar 17 09:41:21 2023 - [info]   192.168.5.150(192.168.5.150:3306)  Version=5.7.41-log (oldest major version between slaves) log-bin:enabled
Fri Mar 17 09:41:21 2023 - [info]     Replicating from 192.168.5.140(192.168.5.140:3306)
Fri Mar 17 09:41:21 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Mar 17 09:41:21 2023 - [info]  Non-candidate masters:
Fri Mar 17 09:41:21 2023 - [info]  Searching from candidate_master slaves which have received the latest relay log events..
Fri Mar 17 09:41:21 2023 - [info] New master is 192.168.5.150(192.168.5.150:3306)
Fri Mar 17 09:41:21 2023 - [info] Starting master failover..
Fri Mar 17 09:41:21 2023 - [info] 
From:
192.168.5.140(192.168.5.140:3306) (current master)
 +--192.168.5.150(192.168.5.150:3306)
 +--192.168.5.160(192.168.5.160:3306)

To:
192.168.5.150(192.168.5.150:3306) (new master)
 +--192.168.5.160(192.168.5.160:3306)
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 3.4: New Master Diff Log Generation Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] * Phase 3.5: Master Log Apply Phase..
Fri Mar 17 09:41:21 2023 - [info] 
Fri Mar 17 09:41:21 2023 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
Fri Mar 17 09:41:21 2023 - [info] Starting recovery on 192.168.5.150(192.168.5.150:3306)..
Fri Mar 17 09:41:21 2023 - [info]  This server has all relay logs. Waiting all logs to be applied.. 
Fri Mar 17 09:41:21 2023 - [info]   done.
Fri Mar 17 09:41:21 2023 - [info]  All relay logs were successfully applied.
Fri Mar 17 09:41:21 2023 - [info] Getting new master's binlog name and position..
Fri Mar 17 09:41:21 2023 - [info]  slave-01-bin.000004:154
Fri Mar 17 09:41:21 2023 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.5.150', MASTER_PORT=3306, MASTER_LOG_FILE='slave-01-bin.000004', MASTER_LOG_POS=154, MASTER_USER='slave', MASTER_PASSWORD='xxx';
Fri Mar 17 09:41:21 2023 - [info] Executing master IP activate script:
Fri Mar 17 09:41:21 2023 - [info]   /usr/local/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=192.168.5.140 --orig_master_ip=192.168.5.140 --orig_master_port=3306 --new_master_host=192.168.5.150 --new_master_ip=192.168.5.150 --new_master_port=3306 --new_master_user='mha'   --new_master_password=xxx
Undefined subroutine &main::FIXME_xxx_create_user called at /usr/local/bin/master_ip_failover line 97.
Set read_only=0 on the new master.
Creating app user on the new master..
Fri Mar 17 09:41:22 2023 - [error][/usr/local/share/perl5/MHA/MasterFailover.pm, ln1612]  Failed to activate master IP address for 192.168.5.150(192.168.5.150:3306) with return code 10:0
Fri Mar 17 09:41:22 2023 - [warning] Proceeding.
Fri Mar 17 09:41:22 2023 - [info] ** Finished master recovery successfully.
Fri Mar 17 09:41:22 2023 - [info] * Phase 3: Master Recovery Phase completed.
Fri Mar 17 09:41:22 2023 - [info] 
Fri Mar 17 09:41:22 2023 - [info] * Phase 4: Slaves Recovery Phase..
Fri Mar 17 09:41:22 2023 - [info] 
Fri Mar 17 09:41:22 2023 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
Fri Mar 17 09:41:22 2023 - [info] 
Fri Mar 17 09:41:22 2023 - [info] -- Slave diff file generation on host 192.168.5.160(192.168.5.160:3306) started, pid: 2390. Check tmp log /var/log/masterha/app1/192.168.5.160_3306_20230317094119.log if it takes time..
Fri Mar 17 09:41:23 2023 - [info] 
Fri Mar 17 09:41:23 2023 - [info] Log messages from 192.168.5.160 ...
Fri Mar 17 09:41:23 2023 - [info] 
Fri Mar 17 09:41:22 2023 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Fri Mar 17 09:41:23 2023 - [info] End of log messages from 192.168.5.160.
Fri Mar 17 09:41:23 2023 - [info] -- 192.168.5.160(192.168.5.160:3306) has the latest relay log events.
Fri Mar 17 09:41:23 2023 - [info] Generating relay diff files from the latest slave succeeded.
Fri Mar 17 09:41:23 2023 - [info] 
Fri Mar 17 09:41:23 2023 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
Fri Mar 17 09:41:23 2023 - [info] 
Fri Mar 17 09:41:23 2023 - [info] -- Slave recovery on host 192.168.5.160(192.168.5.160:3306) started, pid: 2392. Check tmp log /var/log/masterha/app1/192.168.5.160_3306_20230317094119.log if it takes time..
Fri Mar 17 09:41:24 2023 - [info] 
Fri Mar 17 09:41:24 2023 - [info] Log messages from 192.168.5.160 ...
Fri Mar 17 09:41:24 2023 - [info] 
Fri Mar 17 09:41:23 2023 - [info] Starting recovery on 192.168.5.160(192.168.5.160:3306)..
Fri Mar 17 09:41:23 2023 - [info]  This server has all relay logs. Waiting all logs to be applied.. 
Fri Mar 17 09:41:23 2023 - [info]   done.
Fri Mar 17 09:41:23 2023 - [info]  All relay logs were successfully applied.
Fri Mar 17 09:41:23 2023 - [info]  Resetting slave 192.168.5.160(192.168.5.160:3306) and starting replication from the new master 192.168.5.150(192.168.5.150:3306)..
Fri Mar 17 09:41:23 2023 - [info]  Executed CHANGE MASTER.
Fri Mar 17 09:41:23 2023 - [info]  Slave started.
Fri Mar 17 09:41:24 2023 - [info] End of log messages from 192.168.5.160.
Fri Mar 17 09:41:24 2023 - [info] -- Slave recovery on host 192.168.5.160(192.168.5.160:3306) succeeded.
Fri Mar 17 09:41:24 2023 - [info] All new slave servers recovered successfully.
Fri Mar 17 09:41:24 2023 - [info] 
Fri Mar 17 09:41:24 2023 - [info] * Phase 5: New master cleanup phase..
Fri Mar 17 09:41:24 2023 - [info] 
Fri Mar 17 09:41:24 2023 - [info] Resetting slave info on the new master..
Fri Mar 17 09:41:24 2023 - [info]  192.168.5.150: Resetting slave info succeeded.
Fri Mar 17 09:41:24 2023 - [info] Master failover to 192.168.5.150(192.168.5.150:3306) completed successfully.
Fri Mar 17 09:41:24 2023 - [info] Deleted server1 entry from /etc/masterha/app1.cnf .
Fri Mar 17 09:41:24 2023 - [info] 

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

app1: MySQL Master failover 192.168.5.140(192.168.5.140:3306) to 192.168.5.150(192.168.5.150:3306) succeeded

Master 192.168.5.140(192.168.5.140:3306) is down!

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

Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.5.140(192.168.5.140:3306)
The latest slave 192.168.5.150(192.168.5.150:3306) has all relay logs for recovery.
Selected 192.168.5.150(192.168.5.150:3306) as a new master.
192.168.5.150(192.168.5.150:3306): OK: Applying all logs succeeded.
Failed to activate master IP address for 192.168.5.150(192.168.5.150:3306) with return code 10:0
192.168.5.160(192.168.5.160:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.5.160(192.168.5.160:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.5.150(192.168.5.150:3306)
192.168.5.150(192.168.5.150:3306): Resetting slave info succeeded.
Master failover to 192.168.5.150(192.168.5.150:3306) completed successfully.

我就很奇怪了。我并没有手动生成vip 。且主机未看到vip。还是能故障切换。

恢复故障:
master启动mysqld

[root@mysql-master01 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 五 2023-03-17 09:41:25 CST; 16min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 889 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 831 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 949 (code=exited, status=0/SUCCESS)

3月 17 09:33:45 mysql-master01 systemd[1]: Starting MySQL Server...
3月 17 09:33:46 mysql-master01 systemd[1]: Started MySQL Server.
3月 17 09:41:14 mysql-master01 systemd[1]: Stopping MySQL Server...
3月 17 09:41:25 mysql-master01 systemd[1]: Stopped MySQL Server.
[root@mysql-master01 ~]# systemctl start mysqld

新指定为slave 身份互换,

[root@mysql-master01 ~]# mysql -uroot -pREDhat@123
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 4
Server version: 5.7.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> show master status \G;
*************************** 1. row ***************************
             File: master01-bin.000006
         Position: 154
     Binlog_Do_DB: data1
 Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> change master to master_host='192.168.5.150',master_user='slave',master_password='REDhat@123',master_log_file='slave-01-bin.000004',master_log_pos=154;
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: Connecting to master
                  Master_Host: 192.168.5.150
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: slave-01-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-master01-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: slave-01-bin.000004
             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: 154
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1045
                Last_IO_Error: error connecting to master 'slave@192.168.5.150:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/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: 230317 10:08:19
     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: 

编辑配置文件,添加5.140

[root@ha-manager masterha]# cat app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/var/lib/mysql/
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=REDhat@123
ping_interval=1
remote_workdir=/tmp
repl_password=REDhat@123
repl_user=slave
shutdown_script=""
ssh_user=root
user=mha

[server1]
candidate_master=1
check_repl_delay=0
hostname=192.168.5.140
port=3306

[server2]
hostname=192.168.5.150
port=3306

[server3]
hostname=192.168.5.160
port=3306
[root@ha-manager masterha]#

检查下配置:

[root@ha-manager masterha]# masterha_check_repl  --conf=/etc/masterha/app1.cnf
删略:
  Binlog found at /var/lib/mysql/, up to slave-01-bin.000004
Fri Mar 17 10:20:17 2023 - [info] Binlog setting check done.
Fri Mar 17 10:20:17 2023 - [info] Checking SSH publickey authentication and checking r
Fri Mar 17 10:20:18 2023 - [info] 
192.168.5.150(192.168.5.150:3306) (current master)
 +--192.168.5.140(192.168.5.140:3306)
 +--192.168.5.160(192.168.5.160:3306)

Fri Mar 17 10:20:18 2023 - [info] Checking replication health on 192.168.5.140..
Fri Mapt is not defined.
Fri Mar 17 10:20:18 2023 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

总结下,故障恢复需配置文件,手动切换。安装有点复杂,,,逻辑思维。且权限控制好像有点多,授权用户。这里我的vip 不知道是不是被吃了上面的vip脚本中我指定了一个ip为161 的。还有这个玩意儿用编译安装。。。依赖有点多。干死我了。。还去rpmfind 找包才解决,还有repl 检查脚本会检查不过 中有一个master_ip_failover 把它注释 # FIXME_xxx;。还有结合半同步性能会更好,同步和复制的库可以不用定义直接全部,
,eeeee,就是
z这个玩意的虚拟ip 去那里了? 希望有大佬给我看下。。。还有啥叫直接手动添加虚拟ip。那样不会漂移虚拟ip,我试过了。。附加一些参考连接:
鸣谢大佬:
云云云
Action
半同步

[root@mysql-slave01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:92:9f:2a brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.150/24 brd 192.168.5.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe92:9f2a/64 scope link 
       valid_lft forever preferred_lft forever
[root@mysql-slave01 ~]# 
文章作者: emporer
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Emporer-Linux
mysql mysqlha
喜欢就支持一下吧