1在一主两从正常的情况下将主库的数据库进行停止宕机:

[root@db01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL............ SUCCESS!

2在db02数据库中查看主从状态发现已经自动变为主库:

[root@db02 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

db02 [(none)]>show slave status \G

 

 3查看从库db03发现指针已经自动指向了新主库db02,表示MHA自动切换完成:

[root@db03 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

db03 [(none)]>show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.202
Master_User: repl

4还有一个细节当我们退出manager所在的主机数据库时,发现MHA已经自动自杀(MHA是一次性的高可用):

db03 [(none)]>exit
Bye
[1]+ 完成 nohup masterha_manager --conf=/etc/mha/appl.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/appl/manager.log 2>&1
[root@db03 ~]#

5修复MHA环境:

1启动原来损坏的库db01:
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS!

2备份一下新主库的数据

3将修复好的db01和新主库建立关系:

(1)在manager所在的db03中执行如下语句(看日志),获取change master to修复语句:

[root@db03 ~]# grep -i 'change master to' /var/log/mha/appl/manager
Thu Mar 4 21:45:00 2021 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER T MASTER_HOST='10.0.0.202', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';

(2)在坏的数据库db01中执行这个change master to语句后开启主从复制:

[root@db01 ~]# mysql -uroot -p123456
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 2
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

db01 [(none)]>CHANGE MASTER TO MASTER_HOST='10.0.0.202', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';
Query OK, 0 rows affected, 2 warnings (0.26 sec)

db01 [(none)]>start slave;
Query OK, 0 rows affected (0.10 sec)

db01 [(none)]>

 (3)在修复好的db01中检查一下主从状态

db01 [(none)]>show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.202
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 234
Relay_Log_File: db01-relay-bin.000006
Relay_Log_Pos: 407
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

(4)在manager所在的db03主机中修改配置文件:

[root@db03 ~]# vim /etc/mha/appl.cnf
[root@db03 ~]# cat /etc/mha/appl.cnf
[server default]
manager_log=/var/log/mha/appl/manager
manager_workdir=/var/log/mha/appl
master_binlog_dir=/data/binlog
password=mha
ping_interval=2
repl_password=123
repl_user=repl
ssh_user=root
user=mha
[server1]
hostname=10.0.0.201
port=3306

(5)在manager所在的db03中启动MHA并检查状态:

[root@db03 ~]# nohup masterha_manager --conf=/etc/mha/appl.cnf --remove_dead_master_conf --ignore_last_failover </dev/null> /var/log/mha/appl/manager.log 2>&1 &
[1] 2362
[root@db03 ~]# masterha_check_status --conf=/etc/mha/appl.cnf