16、MySQL备份、主从复制及MHA高可用

1、编写脚本,支持让用户自主选择,使用mysqldump还是xtrabackup全量备份。

vim backup.sh

#!/bin/bash
dir=/backup/backup_`date +%F_%T`
backtype=0
echo -e "请选择备份方式:\n 1.mysqldump \n 2.xtrabackup\n 3.退出"
while :
do
    read -p "请输入(1,2,3):" backtype
    case $backtype in
    1)
        mysqldump -A -F --single-transaction --master-data=1 --default-character-set=utf8 > $dir.sql
        echo "使用mysqldump备份成功,备份文件:$dir.sql"
        break
        ;;
    2)
        xtrabackup --backup --target-dir=$dir &>/dev/null
        echo "使用xtrabackup备份成功,备份文件:$dir"
        break
        ;;
    3)
        echo "退出成功,再见!"
        break
        ;;
    *)
        echo "输入错误,请重新输入!"
        continue
        ;;
    esac
done

执行验证:

[root@centos7 data]# ./backup.sh 
请选择备份方式:
 1.mysqldump 
 2.xtrabackup
 3.退出
请输入(1,2,3):1
使用mysqldump备份成功,备份文件:/backup/backup_2020-08-18_22:17:29.sql

[root@centos7 data]# ./backup.sh 
请选择备份方式:
 1.mysqldump 
 2.xtrabackup
 3.退出
请输入(1,2,3):2
使用xtrabackup备份成功,备份文件:/backup/backup_2020-08-18_22:15:49

[root@centos7 data]# ./backup.sh 
请选择备份方式:
 1.mysqldump 
 2.xtrabackup
 3.退出
请输入(1,2,3):3
退出成功,再见!

[root@centos7 backup]# ll
total 504
drwx------ 5 root root    172 Aug 18 22:15 backup_2020-08-18_22:15:49
-rw-r--r-- 1 root root 515067 Aug 18 22:17 backup_2020-08-18_22:17:29.sql

2、配置Mysql主从同步

主数据库服务器:192.168.45.202
从数据库服务器:192.168.45.203

  1. 建立主服务器
    1)安装mariadb数据库
[root@s202 ~]# yum install mariadb-server -y

2)修改配置文件/etc/my.cnf
vim /etc/my.cnf,[mysqld]增加以下三行

[mysqld]
log_bin=master-bin
server_id=1
skip_name_resolve=1

3)启动数据库,登录数据库创建有复制权限的账号

[root@s202 ~]# systemctl start mariadb
[root@s202 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.45.203' identified by 'replpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

4)查看主服务器binlog位置

MariaDB [(none)]> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30328 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       481 |
+-------------------+-----------+
3 rows in set (0.00 sec)
  1. 建立从服务器
     1)安装mariadb数据库
[root@s203 ~]# yum install mariadb-server -y

2)修改配置文件/etc/my.cnf
vim /etc/my.cnf,[mysqld]增加以下几行

[mysqld]
server-id=2
read_only=on #设置从数据库只读
relay_log=relay-log #启用中继日志
skip_name_resolve=1
relay_log_purge=0
log-bin

3)使用有复制权限的用户账号连接至主服务器,并启动复制线程

[root@s203 ~]# systemctl start mariadb
[root@s203 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> change master to master_host='192.168.45.202',master_user='repluser',master_password='replpass',master_log_file='master-bin.000003',master_log_pos=481;
Query OK, 0 rows affected (0.01 sec)

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

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.45.202
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 481
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 481
              Relay_Log_Space: 818
              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
1 row in set (0.00 sec)

此时已经完成主从数据库,测试主从复制
主服务器,创建testdb数据库:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

从服务器:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

主从复制成功。

3、使用MHA实现Mysql高可用。

规划:
mha服务器:192.168.45.135
主服务器:192.168.45.202
从服务器1(备主服务器):192.168.45.203
从服务器2:192.168.45.204

  1. 搭建主服务器192.168.45.202
    1)安装mariadb数据库
[root@s202 ~]# yum install mariadb-server -y

2)修改配置文件/etc/my.cnf
vim /etc/my.cnf,[mysqld]增加以下三行

[mysqld]
log_bin=master-bin
server_id=1
skip_name_resolve=1

3)启动数据库,登录数据库创建有复制权限的账号

[root@s202 ~]# systemctl start mariadb
[root@s202 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.45.%' identified by 'replpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
  1. 搭建从服务器192.168.45.203
     1)安装mariadb数据库
[root@s203 ~]# yum install mariadb-server -y

2)修改配置文件/etc/my.cnf
vim /etc/my.cnf,[mysqld]增加以下几行

[mysqld]
server-id=2
read_only=on #设置从数据库只读
relay_log=relay-log #启用中继日志
skip_name_resolve=1
relay_log_purge=0
log-bin

3)使用有复制权限的用户账号连接至主服务器,并启动复制线程

[root@s203 ~]# systemctl start mariadb
[root@s203 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> change master to master_host='192.168.45.202',master_user='repluser',master_password='replpass',master_log_file='master-bin.000003',master_log_pos=479;
Query OK, 0 rows affected (0.01 sec)

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

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.45.202
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 479
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 479
              Relay_Log_Space: 818
              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
1 row in set (0.00 sec)
  1. 搭建从服务器192.168.45.204
     1)安装mariadb数据库
[root@s204 ~]# yum install mariadb-server -y

2)修改配置文件/etc/my.cnf
vim /etc/my.cnf,[mysqld]增加以下几行

[mysqld]
server-id=3
read_only=on #设置从数据库只读
relay_log=relay-log #启用中继日志
skip_name_resolve=1
relay_log_purge=0
log-bin

3)使用有复制权限的用户账号连接至主服务器,并启动复制线程

[root@s203 ~]# systemctl start mariadb
[root@s203 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> change master to master_host='192.168.45.202',master_user='repluser',master_password='replpass',master_log_file='master-bin.000003',master_log_pos=479;
Query OK, 0 rows affected (0.01 sec)

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

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.45.202
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 479
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 479
              Relay_Log_Space: 818
              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
1 row in set (0.00 sec)
  1. 验证主从复制
    在主服务器创建数据库testdb
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

查看两个从服务器的数据库同步情况,均如下,同步成功。

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)
  1. 搭建mha管理服务器
    1)安装mha-manage(mha的安装需要repo源,需提前配置好)
     将下载好的如下软件进行安装
      mha4mysql-manager-0.56-0.el6.noarch.rpm
      mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7 data]# ll
total 124
-rw-r--r-- 1 root root 87119 Mar 25 02:14 mha4mysql-manager-0.56-0.el6.noarch.rpm
-rw-r--r-- 1 root root 36326 Mar 25 02:14 mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7 data]# yum install mha4*.rpm -y

2)在其他节点安装mha4mysql-node

[root@s202 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
[root@s203 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
[root@s204 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y

3)在所有节点实现相互之间ssh key验证
首先在mha服务器上生成ssh key,并完成自我免登验证

[root@centos7 data]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:PRTGBcbkHhqnz7hkT+zSIFFVFpt2U+bHiPOkrE3n2YQ [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|         =B+=.  o|
|        .+oo + * |
|       .. = * = +|
|      .  O + * o.|
|       .S + + E .|
|      . .= = o + |
|       .+o* . o .|
|       o.=.      |
|        ..o      |
+----[SHA256]-----+

#实现自我免登
[root@centos7 data]# ssh-copy-id 192.168.45.135
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.45.135 (192.168.45.135)' can't be established.
ECDSA key fingerprint is SHA256:IJSDPnogSYHD4HFYJdnm3q4DwUrEiMYNa71KRXUt69Q.
ECDSA key fingerprint is MD5:5d:c0:86:72:92:64:2d:09:b2:7e:33:ea:2b:35:71:75.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.45.135'"
and check to make sure that only the key(s) you wanted were added.

#验证自我免登
[root@centos7 data]# ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:IJSDPnogSYHD4HFYJdnm3q4DwUrEiMYNa71KRXUt69Q.
ECDSA key fingerprint is MD5:5d:c0:86:72:92:64:2d:09:b2:7e:33:ea:2b:35:71:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Last login: Wed Aug 19 21:14:27 2020 from 192.168.45.200

实现自我免登之后,将key拷贝到其他节点

[root@centos7 ~]# scp -r .ssh/ 192.168.45.202:
The authenticity of host '192.168.45.203 (192.168.45.203)' can't be established.
ECDSA key fingerprint is SHA256:IJSDPnogSYHD4HFYJdnm3q4DwUrEiMYNa71KRXUt69Q.
ECDSA key fingerprint is MD5:5d:c0:86:72:92:64:2d:09:b2:7e:33:ea:2b:35:71:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.45.203' (ECDSA) to the list of known hosts.
[email protected]'s password: 
id_rsa                                                                                100% 1675   993.0KB/s   00:00    
id_rsa.pub                                                                            100%  406   278.6KB/s   00:00    
known_hosts                                                                           100%  699   528.8KB/s   00:00    
authorized_keys                                                                       100%  406   421.4KB/s   00:00    

[root@centos7 ~]# scp -r .ssh/ 192.168.45.203:
The authenticity of host '192.168.45.203 (192.168.45.203)' can't be established.
ECDSA key fingerprint is SHA256:IJSDPnogSYHD4HFYJdnm3q4DwUrEiMYNa71KRXUt69Q.
ECDSA key fingerprint is MD5:5d:c0:86:72:92:64:2d:09:b2:7e:33:ea:2b:35:71:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.45.203' (ECDSA) to the list of known hosts.
[email protected]'s password: 
id_rsa                                                                                100% 1675   993.0KB/s   00:00    
id_rsa.pub                                                                            100%  406   278.6KB/s   00:00    
known_hosts                                                                           100%  699   528.8KB/s   00:00    
authorized_keys                                                                       100%  406   421.4KB/s   00:00    

[root@centos7 ~]# scp -r .ssh/ 192.168.45.204:
The authenticity of host '192.168.45.204 (192.168.45.204)' can't be established.
ECDSA key fingerprint is SHA256:IJSDPnogSYHD4HFYJdnm3q4DwUrEiMYNa71KRXUt69Q.
ECDSA key fingerprint is MD5:5d:c0:86:72:92:64:2d:09:b2:7e:33:ea:2b:35:71:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.45.204' (ECDSA) to the list of known hosts.
[email protected]'s password: 
id_rsa                                                                                100% 1675     1.6MB/s   00:00    
id_rsa.pub                                                                            100%  406   377.0KB/s   00:00    
known_hosts                                                                           100%  875   866.5KB/s   00:00    
authorized_keys                                                                       100%  406   454.3KB/s   00:00    

4)在master上创建管理账号

[root@centos7 ~]# systemctl start mariadb
[root@centos7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> grant all on *.* to 'mhauser'@'192.168.45.%' identified by 'qwe123';
Query OK, 0 rows affected (0.00 sec)

5)准备mha管理节点的配置文件

[root@centos7 ~]# mkdir /etc/mha
[root@centos7 ~]# vim /etc/mha/mha.cnf
[root@centos7 ~]# cat /etc/mha/mha.cnf
[server default] 
user=mhauser 
password=qwe123
manager_workdir=/etc/mha/ 
manager_log=/etc/mha/manager.log 
remote_workdir=/etc/mha/ 
ssh_user=root 
repl_user=repluser 
repl_password=replpass 
ping_interval=1 
[server1] 
hostname=192.168.45.202
candidate_master=1 
[server2]
hostname=192.168.45.203
candidate_master=1
[server3]
hostname=192.168.45.204

6)mha验证

[root@centos7 mha]# masterha_check_ssh --conf=/etc/mha/mha.cnf
Wed Aug 19 22:43:20 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 19 22:43:20 2020 - [info] Reading application default configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:43:20 2020 - [info] Reading server configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:43:20 2020 - [info] Starting SSH connection tests..
Wed Aug 19 22:43:21 2020 - [debug] 
Wed Aug 19 22:43:20 2020 - [debug]  Connecting via SSH from [email protected](192.168.45.202:22) to [email protected](192.168.45.203:22)..
Wed Aug 19 22:43:21 2020 - [debug]   ok.
Wed Aug 19 22:43:21 2020 - [debug]  Connecting via SSH from [email protected](192.168.45.202:22) to [email protected](192.168.45.204:22)..
Wed Aug 19 22:43:21 2020 - [debug]   ok.
Wed Aug 19 22:43:22 2020 - [debug] 
Wed Aug 19 22:43:21 2020 - [debug]  Connecting via SSH from [email protected](192.168.45.203:22) to [email protected](192.168.45.202:22)..
Wed Aug 19 22:43:21 2020 - [debug]   ok.
Wed Aug 19 22:43:21 2020 - [debug]  Connecting via SSH from [email protected](192.168.45.203:22) to [email protected](192.168.45.204:22)..
Wed Aug 19 22:43:22 2020 - [debug]   ok.
Wed Aug 19 22:43:23 2020 - [debug] 
Wed Aug 19 22:43:21 2020 - [debug]  Connecting via SSH from [email protected](192.168.45.204:22) to [email protected](192.168.45.202:22)..
Wed Aug 19 22:43:22 2020 - [debug]   ok.
Wed Aug 19 22:43:22 2020 - [debug]  Connecting via SSH from [email protected](192.168.45.204:22) to [email protected](192.168.45.203:22)..
Wed Aug 19 22:43:22 2020 - [debug]   ok.
Wed Aug 19 22:43:23 2020 - [info] All SSH connection tests passed successfully.

[root@centos7 mha]# masterha_check_repl --conf=/etc/mha/mha.cnf
Wed Aug 19 22:41:26 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 19 22:41:26 2020 - [info] Reading application default configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:41:26 2020 - [info] Reading server configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:41:26 2020 - [info] MHA::MasterMonitor version 0.56.
Wed Aug 19 22:41:28 2020 - [info] GTID failover mode = 0
Wed Aug 19 22:41:28 2020 - [info] Dead Servers:
Wed Aug 19 22:41:28 2020 - [info] Alive Servers:
Wed Aug 19 22:41:28 2020 - [info]   192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:41:28 2020 - [info]   192.168.45.203(192.168.45.203:3306)
Wed Aug 19 22:41:28 2020 - [info]   192.168.45.204(192.168.45.204:3306)
Wed Aug 19 22:41:28 2020 - [info] Alive Slaves:
Wed Aug 19 22:41:28 2020 - [info]   192.168.45.203(192.168.45.203:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:41:28 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:41:28 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 19 22:41:28 2020 - [info]   192.168.45.204(192.168.45.204:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:41:28 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:41:28 2020 - [info] Current Alive Master: 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:41:28 2020 - [info] Checking slave configurations..
Wed Aug 19 22:41:28 2020 - [info] Checking replication filtering settings..
Wed Aug 19 22:41:28 2020 - [info]  binlog_do_db= , binlog_ignore_db= 
Wed Aug 19 22:41:28 2020 - [info]  Replication filtering check ok.
Wed Aug 19 22:41:28 2020 - [info] GTID (with auto-pos) is not supported
Wed Aug 19 22:41:28 2020 - [info] Starting SSH connection tests..
Wed Aug 19 22:41:30 2020 - [info] All SSH connection tests passed successfully.
Wed Aug 19 22:41:30 2020 - [info] Checking MHA Node version..
Wed Aug 19 22:41:31 2020 - [info]  Version check ok.
Wed Aug 19 22:41:31 2020 - [info] Checking SSH publickey authentication settings on the current master..
Wed Aug 19 22:41:31 2020 - [info] HealthCheck: SSH to 192.168.45.202 is reachable.
Wed Aug 19 22:41:31 2020 - [info] Master MHA Node version is 0.56.
Wed Aug 19 22:41:31 2020 - [info] Checking recovery script configurations on 192.168.45.202(192.168.45.202:3306)..
Wed Aug 19 22:41:31 2020 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/etc/mha//save_binary_logs_test --manager_version=0.56 --start_file=master-bin.000003 
Wed Aug 19 22:41:31 2020 - [info]   Connecting to [email protected](192.168.45.202:22).. 
  Creating /etc/mha if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to master-bin.000003
Wed Aug 19 22:41:31 2020 - [info] Binlog setting check done.
Wed Aug 19 22:41:31 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Wed Aug 19 22:41:31 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=192.168.45.203 --slave_ip=192.168.45.203 --slave_port=3306 --workdir=/etc/mha/ --target_version=5.5.65-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Wed Aug 19 22:41:31 2020 - [info]   Connecting to [email protected](192.168.45.203:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to relay-log.000004
    Temporary relay log file is /var/lib/mysql/relay-log.000004
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Aug 19 22:41:32 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=192.168.45.204 --slave_ip=192.168.45.204 --slave_port=3306 --workdir=/etc/mha/ --target_version=5.5.65-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Wed Aug 19 22:41:32 2020 - [info]   Connecting to [email protected](192.168.45.204:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to relay-log.000004
    Temporary relay log file is /var/lib/mysql/relay-log.000004
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Aug 19 22:41:32 2020 - [info] Slaves settings check done.
Wed Aug 19 22:41:32 2020 - [info] 
192.168.45.202(192.168.45.202:3306) (current master)
 +--192.168.45.203(192.168.45.203:3306)
 +--192.168.45.204(192.168.45.204:3306)

Wed Aug 19 22:41:32 2020 - [info] Checking replication health on 192.168.45.203..
Wed Aug 19 22:41:32 2020 - [info]  ok.
Wed Aug 19 22:41:32 2020 - [info] Checking replication health on 192.168.45.204..
Wed Aug 19 22:41:32 2020 - [info]  ok.
Wed Aug 19 22:41:32 2020 - [warning] master_ip_failover_script is not defined.
Wed Aug 19 22:41:32 2020 - [warning] shutdown_script is not defined.
Wed Aug 19 22:41:32 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

7)启动mha

[root@centos7 mha]# masterha_manager --conf=/etc/mha/mha.cnf
Wed Aug 19 22:44:51 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 19 22:44:51 2020 - [info] Reading application default configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:44:51 2020 - [info] Reading server configuration from /etc/mha/mha.cnf..

查看日志

[root@centos7 ~]# tail -f /etc/mha/manager.log 
192.168.45.202(192.168.45.202:3306) (current master)
 +--192.168.45.203(192.168.45.203:3306)
 +--192.168.45.204(192.168.45.204:3306)

Wed Aug 19 22:44:56 2020 - [warning] master_ip_failover_script is not defined.
Wed Aug 19 22:44:56 2020 - [warning] shutdown_script is not defined.
Wed Aug 19 22:44:56 2020 - [info] Set master ping interval 1 seconds.
Wed Aug 19 22:44:56 2020 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Wed Aug 19 22:44:56 2020 - [info] Starting ping health check on 192.168.45.202(192.168.45.202:3306)..
Wed Aug 19 22:44:56 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..

8)验证
此时停止主服务器的MySQL服务,然后查看mha日志

[root@s202 ~]# systemctl stop mariadb
[root@centos7 ~]# tail -f /etc/mha/manager.log 
192.168.45.202(192.168.45.202:3306) (current master)
 +--192.168.45.203(192.168.45.203:3306)
 +--192.168.45.204(192.168.45.204:3306)

Wed Aug 19 22:44:56 2020 - [warning] master_ip_failover_script is not defined.
Wed Aug 19 22:44:56 2020 - [warning] shutdown_script is not defined.
Wed Aug 19 22:44:56 2020 - [info] Set master ping interval 1 seconds.
Wed Aug 19 22:44:56 2020 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Wed Aug 19 22:44:56 2020 - [info] Starting ping health check on 192.168.45.202(192.168.45.202:3306)..
Wed Aug 19 22:44:56 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Wed Aug 19 22:47:22 2020 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Wed Aug 19 22:47:22 2020 - [info] Executing SSH check script: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/etc/mha//save_binary_logs_test --manager_version=0.56 --binlog_prefix=master-bin
Wed Aug 19 22:47:22 2020 - [info] HealthCheck: SSH to 192.168.45.202 is reachable.
Wed Aug 19 22:47:23 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.45.202' (111))
Wed Aug 19 22:47:23 2020 - [warning] Connection failed 2 time(s)..
Wed Aug 19 22:47:24 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.45.202' (111))
Wed Aug 19 22:47:24 2020 - [warning] Connection failed 3 time(s)..
Wed Aug 19 22:47:25 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.45.202' (111))
Wed Aug 19 22:47:25 2020 - [warning] Connection failed 4 time(s)..
Wed Aug 19 22:47:25 2020 - [warning] Master is not reachable from health checker!
Wed Aug 19 22:47:25 2020 - [warning] Master 192.168.45.202(192.168.45.202:3306) is not reachable!
Wed Aug 19 22:47:25 2020 - [warning] SSH is reachable.
Wed Aug 19 22:47:25 2020 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/mha/mha.cnf again, and trying to connect to all servers to check server status..
Wed Aug 19 22:47:25 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 19 22:47:25 2020 - [info] Reading application default configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:47:25 2020 - [info] Reading server configuration from /etc/mha/mha.cnf..
Wed Aug 19 22:47:26 2020 - [info] GTID failover mode = 0
Wed Aug 19 22:47:26 2020 - [info] Dead Servers:
Wed Aug 19 22:47:26 2020 - [info]   192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:26 2020 - [info] Alive Servers:
Wed Aug 19 22:47:26 2020 - [info]   192.168.45.203(192.168.45.203:3306)
Wed Aug 19 22:47:26 2020 - [info]   192.168.45.204(192.168.45.204:3306)
Wed Aug 19 22:47:26 2020 - [info] Alive Slaves:
Wed Aug 19 22:47:26 2020 - [info]   192.168.45.203(192.168.45.203:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:26 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:26 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 19 22:47:26 2020 - [info]   192.168.45.204(192.168.45.204:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:26 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:26 2020 - [info] Checking slave configurations..
Wed Aug 19 22:47:26 2020 - [info] Checking replication filtering settings..
Wed Aug 19 22:47:26 2020 - [info]  Replication filtering check ok.
Wed Aug 19 22:47:26 2020 - [info] Master is down!
Wed Aug 19 22:47:26 2020 - [info] Terminating monitoring script.
Wed Aug 19 22:47:26 2020 - [info] Got exit code 20 (Master dead).
Wed Aug 19 22:47:26 2020 - [info] MHA::MasterFailover version 0.56.
Wed Aug 19 22:47:26 2020 - [info] Starting master failover.
Wed Aug 19 22:47:26 2020 - [info] 
Wed Aug 19 22:47:26 2020 - [info] * Phase 1: Configuration Check Phase..
Wed Aug 19 22:47:26 2020 - [info] 
Wed Aug 19 22:47:27 2020 - [info] GTID failover mode = 0
Wed Aug 19 22:47:27 2020 - [info] Dead Servers:
Wed Aug 19 22:47:27 2020 - [info]   192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:27 2020 - [info] Checking master reachability via MySQL(double check)...
Wed Aug 19 22:47:27 2020 - [info]  ok.
Wed Aug 19 22:47:27 2020 - [info] Alive Servers:
Wed Aug 19 22:47:27 2020 - [info]   192.168.45.203(192.168.45.203:3306)
Wed Aug 19 22:47:27 2020 - [info]   192.168.45.204(192.168.45.204:3306)
Wed Aug 19 22:47:27 2020 - [info] Alive Slaves:
Wed Aug 19 22:47:27 2020 - [info]   192.168.45.203(192.168.45.203:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:27 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:27 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 19 22:47:27 2020 - [info]   192.168.45.204(192.168.45.204:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:27 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:27 2020 - [info] Starting Non-GTID based failover.
Wed Aug 19 22:47:27 2020 - [info] 
Wed Aug 19 22:47:27 2020 - [info] ** Phase 1: Configuration Check Phase completed.
Wed Aug 19 22:47:27 2020 - [info] 
Wed Aug 19 22:47:27 2020 - [info] * Phase 2: Dead Master Shutdown Phase..
Wed Aug 19 22:47:27 2020 - [info] 
Wed Aug 19 22:47:27 2020 - [info] Forcing shutdown so that applications never connect to the current master..
Wed Aug 19 22:47:27 2020 - [warning] master_ip_failover_script is not set. Skipping invalidating dead master IP address.
Wed Aug 19 22:47:27 2020 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Wed Aug 19 22:47:28 2020 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Wed Aug 19 22:47:28 2020 - [info] 
Wed Aug 19 22:47:28 2020 - [info] * Phase 3: Master Recovery Phase..
Wed Aug 19 22:47:28 2020 - [info] 
Wed Aug 19 22:47:28 2020 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Wed Aug 19 22:47:28 2020 - [info] 
Wed Aug 19 22:47:28 2020 - [info] The latest binary log file/position on all slaves is master-bin.000003:1200
Wed Aug 19 22:47:28 2020 - [info] Latest slaves (Slaves that received relay log files to the latest):
Wed Aug 19 22:47:28 2020 - [info]   192.168.45.203(192.168.45.203:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:28 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:28 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 19 22:47:28 2020 - [info]   192.168.45.204(192.168.45.204:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:28 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:28 2020 - [info] The oldest binary log file/position on all slaves is master-bin.000003:1200
Wed Aug 19 22:47:28 2020 - [info] Oldest slaves:
Wed Aug 19 22:47:28 2020 - [info]   192.168.45.203(192.168.45.203:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:28 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:28 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 19 22:47:28 2020 - [info]   192.168.45.204(192.168.45.204:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:28 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:28 2020 - [info] 
Wed Aug 19 22:47:28 2020 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Wed Aug 19 22:47:28 2020 - [info] 
Wed Aug 19 22:47:28 2020 - [info] Fetching dead master's binary logs..
Wed Aug 19 22:47:28 2020 - [info] Executing command on the dead master 192.168.45.202(192.168.45.202:3306): save_binary_logs --command=save --start_file=master-bin.000003  --start_pos=1200 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56
  Creating /etc/mha if not exists..    ok.
 Concat binary/relay logs from master-bin.000003 pos 1200 to master-bin.000003 EOF into /etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog ..
  Dumping binlog format description event, from position 0 to 245.. ok.
  Dumping effective binlog data from /var/lib/mysql/master-bin.000003 position 1200 to tail(1219).. ok.
 Concat succeeded.
Wed Aug 19 22:47:29 2020 - [info] scp from [email protected]:/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog to local:/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog succeeded.
Wed Aug 19 22:47:29 2020 - [info] HealthCheck: SSH to 192.168.45.203 is reachable.
Wed Aug 19 22:47:29 2020 - [info] HealthCheck: SSH to 192.168.45.204 is reachable.
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] * Phase 3.3: Determining New Master Phase..
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Wed Aug 19 22:47:30 2020 - [info] All slaves received relay logs to the same position. No need to resync each other.
Wed Aug 19 22:47:30 2020 - [info] Searching new master from slaves..
Wed Aug 19 22:47:30 2020 - [info]  Candidate masters from the configuration file:
Wed Aug 19 22:47:30 2020 - [info]   192.168.45.203(192.168.45.203:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Aug 19 22:47:30 2020 - [info]     Replicating from 192.168.45.202(192.168.45.202:3306)
Wed Aug 19 22:47:30 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 19 22:47:30 2020 - [info]  Non-candidate masters:
Wed Aug 19 22:47:30 2020 - [info]  Searching from candidate_master slaves which have received the latest relay log events..
Wed Aug 19 22:47:30 2020 - [info] New master is 192.168.45.203(192.168.45.203:3306)
Wed Aug 19 22:47:30 2020 - [info] Starting master failover..
Wed Aug 19 22:47:30 2020 - [info] 
From:
192.168.45.202(192.168.45.202:3306) (current master)
 +--192.168.45.203(192.168.45.203:3306)
 +--192.168.45.204(192.168.45.204:3306)

To:
192.168.45.203(192.168.45.203:3306) (new master)
 +--192.168.45.204(192.168.45.204:3306)
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] * Phase 3.3: New Master Diff Log Generation Phase..
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Wed Aug 19 22:47:30 2020 - [info] Sending binlog..
Wed Aug 19 22:47:30 2020 - [info] scp from local:/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog to [email protected]:/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog succeeded.
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] * Phase 3.4: Master Log Apply Phase..
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
Wed Aug 19 22:47:30 2020 - [info] Starting recovery on 192.168.45.203(192.168.45.203:3306)..
Wed Aug 19 22:47:30 2020 - [info]  Generating diffs succeeded.
Wed Aug 19 22:47:30 2020 - [info] Waiting until all relay logs are applied.
Wed Aug 19 22:47:30 2020 - [info]  done.
Wed Aug 19 22:47:30 2020 - [info] Getting slave status..
Wed Aug 19 22:47:30 2020 - [info] This slave(192.168.45.203)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(master-bin.000003:1200). No need to recover from Exec_Master_Log_Pos.
Wed Aug 19 22:47:30 2020 - [info] Connecting to the target slave host 192.168.45.203, running recover script..
Wed Aug 19 22:47:30 2020 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user='mhauser' --slave_host=192.168.45.203 --slave_ip=192.168.45.203  --slave_port=3306 --apply_files=/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog --workdir=/etc/mha/ --target_version=5.5.65-MariaDB --timestamp=20200819224726 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56 --slave_pass=xxx
Wed Aug 19 22:47:30 2020 - [info] 
Applying differential binary/relay log files /etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog on 192.168.45.203:3306. This may take long time...
Applying log files succeeded.
Wed Aug 19 22:47:30 2020 - [info]  All relay logs were successfully applied.
Wed Aug 19 22:47:30 2020 - [info] Getting new master's binlog name and position..
Wed Aug 19 22:47:30 2020 - [info]  mariadb-bin.000002:622
Wed Aug 19 22:47:30 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.45.203', MASTER_PORT=3306, MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=622, MASTER_USER='repluser', MASTER_PASSWORD='xxx';
Wed Aug 19 22:47:30 2020 - [warning] master_ip_failover_script is not set. Skipping taking over new master IP address.
Wed Aug 19 22:47:30 2020 - [info] Setting read_only=0 on 192.168.45.203(192.168.45.203:3306)..
Wed Aug 19 22:47:30 2020 - [info]  ok.
Wed Aug 19 22:47:30 2020 - [info] ** Finished master recovery successfully.
Wed Aug 19 22:47:30 2020 - [info] * Phase 3: Master Recovery Phase completed.
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] * Phase 4: Slaves Recovery Phase..
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
Wed Aug 19 22:47:30 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info] -- Slave diff file generation on host 192.168.45.204(192.168.45.204:3306) started, pid: 54573. Check tmp log /etc/mha//192.168.45.204_3306_20200819224726.log if it takes time..
Wed Aug 19 22:47:31 2020 - [info] 
Wed Aug 19 22:47:31 2020 - [info] Log messages from 192.168.45.204 ...
Wed Aug 19 22:47:31 2020 - [info] 
Wed Aug 19 22:47:30 2020 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Wed Aug 19 22:47:31 2020 - [info] End of log messages from 192.168.45.204.
Wed Aug 19 22:47:31 2020 - [info] -- 192.168.45.204(192.168.45.204:3306) has the latest relay log events.
Wed Aug 19 22:47:31 2020 - [info] Generating relay diff files from the latest slave succeeded.
Wed Aug 19 22:47:31 2020 - [info] 
Wed Aug 19 22:47:31 2020 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
Wed Aug 19 22:47:31 2020 - [info] 
Wed Aug 19 22:47:31 2020 - [info] -- Slave recovery on host 192.168.45.204(192.168.45.204:3306) started, pid: 54575. Check tmp log /etc/mha//192.168.45.204_3306_20200819224726.log if it takes time..
Wed Aug 19 22:47:32 2020 - [info] 
Wed Aug 19 22:47:32 2020 - [info] Log messages from 192.168.45.204 ...
Wed Aug 19 22:47:32 2020 - [info] 
Wed Aug 19 22:47:31 2020 - [info] Sending binlog..
Wed Aug 19 22:47:32 2020 - [info] scp from local:/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog to [email protected]:/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog succeeded.
Wed Aug 19 22:47:32 2020 - [info] Starting recovery on 192.168.45.204(192.168.45.204:3306)..
Wed Aug 19 22:47:32 2020 - [info]  Generating diffs succeeded.
Wed Aug 19 22:47:32 2020 - [info] Waiting until all relay logs are applied.
Wed Aug 19 22:47:32 2020 - [info]  done.
Wed Aug 19 22:47:32 2020 - [info] Getting slave status..
Wed Aug 19 22:47:32 2020 - [info] This slave(192.168.45.204)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(master-bin.000003:1200). No need to recover from Exec_Master_Log_Pos.
Wed Aug 19 22:47:32 2020 - [info] Connecting to the target slave host 192.168.45.204, running recover script..
Wed Aug 19 22:47:32 2020 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user='mhauser' --slave_host=192.168.45.204 --slave_ip=192.168.45.204  --slave_port=3306 --apply_files=/etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog --workdir=/etc/mha/ --target_version=5.5.65-MariaDB --timestamp=20200819224726 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56 --slave_pass=xxx
Wed Aug 19 22:47:32 2020 - [info] 
Applying differential binary/relay log files /etc/mha//saved_master_binlog_from_192.168.45.202_3306_20200819224726.binlog on 192.168.45.204:3306. This may take long time...
Applying log files succeeded.
Wed Aug 19 22:47:32 2020 - [info]  All relay logs were successfully applied.
Wed Aug 19 22:47:32 2020 - [info]  Resetting slave 192.168.45.204(192.168.45.204:3306) and starting replication from the new master 192.168.45.203(192.168.45.203:3306)..
Wed Aug 19 22:47:32 2020 - [info]  Executed CHANGE MASTER.
Wed Aug 19 22:47:32 2020 - [info]  Slave started.
Wed Aug 19 22:47:32 2020 - [info] End of log messages from 192.168.45.204.
Wed Aug 19 22:47:32 2020 - [info] -- Slave recovery on host 192.168.45.204(192.168.45.204:3306) succeeded.
Wed Aug 19 22:47:32 2020 - [info] All new slave servers recovered successfully.
Wed Aug 19 22:47:32 2020 - [info] 
Wed Aug 19 22:47:32 2020 - [info] * Phase 5: New master cleanup phase..
Wed Aug 19 22:47:32 2020 - [info] 
Wed Aug 19 22:47:32 2020 - [info] Resetting slave info on the new master..
Wed Aug 19 22:47:32 2020 - [info]  192.168.45.203: Resetting slave info succeeded.
Wed Aug 19 22:47:32 2020 - [info] Master failover to 192.168.45.203(192.168.45.203:3306) completed successfully.
Wed Aug 19 22:47:32 2020 - [info] 

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

mha: MySQL Master failover 192.168.45.202(192.168.45.202:3306) to 192.168.45.203(192.168.45.203:3306) succeeded

Master 192.168.45.202(192.168.45.202:3306) is down!

Check MHA Manager logs at centos7.localdomain:/etc/mha/manager.log for details.

Started automated(non-interactive) failover.
The latest slave 192.168.45.203(192.168.45.203:3306) has all relay logs for recovery.
Selected 192.168.45.203(192.168.45.203:3306) as a new master.
192.168.45.203(192.168.45.203:3306): OK: Applying all logs succeeded.
192.168.45.204(192.168.45.204:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.45.204(192.168.45.204:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.45.203(192.168.45.203:3306)
192.168.45.203(192.168.45.203:3306): Resetting slave info succeeded.
Master failover to 192.168.45.203(192.168.45.203:3306) completed successfully.

从日志中可以看出,主数据库服务器已从192.168.45.202切到192.168.45.203,验证成功。

你可能感兴趣的:(16、MySQL备份、主从复制及MHA高可用)