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

安装xtrabackup:

[root@centos7 ~]# yum install percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm -y

注:percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm 需在percona官网下载

自主选择备份工具备份:

[root@centos7 ~]# cat backup.sh
#!/bin/bash
#
cat <mysqldump.sql
    ;;
2)
    innobackupex --user=root --password=centos /space/mysqlbak/ >/dev/null
    ;;
*)
    echo "Please choose 1 or 2"
    ;;
esac

2、配置Mysql主从同步

1)安装主mysql

[root@master ~]# yum install mariadb* -y

2)修改配置文件

[root@master ~]# cat /etc/my.cnf |grep -v "#"
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master-bin   #启用binlog
server_id=1 #设置id号
skip_name_resolve=1

3) 创建有复制权限的账号

[root@master ~]# systemctl start mariadb #启动数据库
[root@master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-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'@'%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4)安装从mysql

[root@slave1 ~]# yum install mariadb* -y

5)修改配置文件

[root@slave1 ~]# cat /etc/my.cnf |grep -v "#"
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

server_id=2
read_only=ON    #设置从库只读
relay_log=relay-log  #启用中继日志
skip_name_resolve=1
relay_log_purge=0
log-bin

6)查看主mysql的binlog位置

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

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

[root@jumpserver ~]# systemctl start mariadb #启动数据库
[root@jumpserver ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-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='10.1.1.109', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=245; 
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.109
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 468
              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)

8)测试主从复制

主mysql:

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

从mysql:

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

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

在2实验的基础上再安装一个从mysql:

1)安装从mysql

[root@slave2 ~]# yum install mariadb* -y

2)修改配置文件

[root@slave2 ~]# cat /etc/my.cnf |grep -v "#"
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin

server_id=3
read_only=ON    #设置从库只读
relay_log=relay-log  #启用中继日志
skip_name_resolve=1
relay_log_purge=0

3)查看主mysql的binlog位置

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

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

[root@slave2 ~]# systemctl start mariadb #启动数据库
[root@slave2 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-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='10.1.1.109', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_LOG_FILE=' mariadb-bin.000001', MASTER_LOG_POS=245; 
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.109
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 468
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 468
              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)

5)安装mha-manage

安装mha需要epel源安装依赖包,所以先安装epel源:

[root@mha ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
--2020-04-15 21:20:05--  http://mirrors.aliyun.com/repo/epel-6.repo
Resolving mirrors.aliyun.com... 124.225.134.230, 14.215.23.250, 124.225.134.225, ...
Connecting to mirrors.aliyun.com|124.225.134.230|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: “/etc/yum.repos.d/epel.repo”

100%[=========================================================================================>] 664         --.-K/s   in 0s      

2020-04-15 21:20:05 (84.6 MB/s) - “/etc/yum.repos.d/epel.repo” saved [664/664]
[root@mha ~]# yum repolist #验证epel源
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
epel                                                                                                        | 4.7 kB     00:00     
epel/primary_db                                                                                             | 6.1 MB     00:01     
repo id                                    repo name                                                                         status
base                                       CentOS-6 - Base                                                                    6,713
epel                                       Extra Packages for Enterprise Linux 6 - x86_64                                    12,587
extras                                     CentOS-6 - Extras                                                                     47
updates                                    CentOS-6 - Updates                                                                   952
repolist: 20,299

安装mha:

先在官网下载:mha4mysql-manager-0.56-0.el6.noarch.rpm mha4mysql-node-0.56-0.el6.noarch.rpm

[root@mha ~]# ls
1.txt              centos6.10_ks.cfg  Downloads           mha4mysql-manager-0.56-0.el6.noarch.rpm  Pictures    Templates
anaconda-ks.cfg    Desktop            install.log         mha4mysql-node-0.56-0.el6.noarch.rpm     Public      Videos
centos_6.10ks.cfg  Documents          install.log.syslog  Music                                    selinux.sh
[root@mha ~]# ^C
[root@mha ~]# yum install mha*.rpm -y

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

[root@master ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
[root@slave1 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
[root@slave2 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y

7)在所有节点实现相互之间ssh key验证

[root@mha ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
7c:eb:33:49:5a:28:b8:f8:46:36:6d:ba:81:37:bd:c7 root@mha
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|       .         |
|     o  S..      |
|   .=.+ ..o.     |
|  .++=.o +..     |
|  ..+o .E.+      |
|   oo...  .o     |
+-----------------+
###先设置免密ssh登录自己
[root@mha ~]# ssh-copy-id 10.1.1.110
The authenticity of host '10.1.1.110 (10.1.1.110)' can't be established.
RSA key fingerprint is 87:2f:51:55:b3:ce:9c:f7:5f:f3:86:91:a2:42:bf:04.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.1.110' (RSA) to the list of known hosts.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Now try logging into the machine, with "ssh '10.1.1.110'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@mha ~]# ssh localhost
Last login: Wed Apr 15 21:17:09 2020 from 10.1.1.16
[root@mha ~]# exit
logout
Connection to localhost closed.
###把key传给远程主机,实现免密ssh远程主机
[root@mha ~]# scp -r .ssh/ 10.1.1.107:
The authenticity of host '10.1.1.107 (10.1.1.107)' can't be established.
RSA key fingerprint is 98:ad:3e:af:a2:47:8a:89:91:ae:36:5d:7d:84:46:5d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.1.107' (RSA) to the list of known hosts.
[email protected]'s password: 
id_rsa.pub                                 100%  390     0.4KB/s   00:00    
id_rsa                                     100% 1675     1.6KB/s   00:00                  known_hosts                                100% 1567     1.5KB/s   00:00                                                          
[root@mha ~]# scp -r .ssh/ 10.1.1.108:
The authenticity of host '10.1.1.108 (10.1.1.108)' can't be established.
RSA key fingerprint is 20:13:b4:f6:cd:39:46:df:a7:7d:0f:61:c5:1d:17:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.1.108' (RSA) to the list of known hosts.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
id_rsa.pub                                 100%  390     0.4KB/s   00:00                 id_rsa                                     100% 1675     1.6KB/s   00:00                  known_hosts                                100% 1959     1.9KB/s   00:00                  [root@mha ~]# scp -r .ssh/ 10.1.1.109:
[email protected]'s password: 
id_rsa.pub                                 100%  390     0.4KB/s   00:00                 id_rsa                                     100% 1675     1.6KB/s   00:00                  known_hosts                                100% 1959     1.9KB/s   00:00                 

8)在master上创建管理账号

[root@master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.64-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 [mysql]> grant all on *.* to 'mhauser'@'%' identified by 'magedu'; 
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

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

[root@mha ~]# cat /etc/mastermha/mha.cnf
[server default] 
user=mhauser 
password=magedu 
manager_workdir=/data/mastermha/mha1/ 
manager_log=/data/mastermha/mha1/manager.log 
remote_workdir=/data/mastermha/mha1/ 
ssh_user=root 
repl_user=repluser 
repl_password=replpass 
ping_interval=1 
[server1] 
hostname=10.1.1.109
candidate_master=1 
[server2] 
hostname=10.1.1.107 
candidate_master=1 
[server3] 
hostname=10.1.1.108

10)Mha验证和启动

###Mha验证
[root@mha ~]# masterha_check_ssh --conf=/etc/mastermha/mha.cnf 
Wed Apr 15 22:00:51 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr 15 22:00:51 2020 - [info] Reading application default configuration from /etc/mastermha/mha.cnf..
Wed Apr 15 22:00:51 2020 - [info] Reading server configuration from /etc/mastermha/mha.cnf..
Wed Apr 15 22:00:51 2020 - [info] Starting SSH connection tests..
Wed Apr 15 22:00:53 2020 - [debug] 
Wed Apr 15 22:00:51 2020 - [debug]  Connecting via SSH from [email protected](10.1.1.109:22) to [email protected](10.1.1.107:22)..
Wed Apr 15 22:00:52 2020 - [debug]   ok.
Wed Apr 15 22:00:52 2020 - [debug]  Connecting via SSH from [email protected](10.1.1.109:22) to [email protected](10.1.1.108:22)..
Wed Apr 15 22:00:52 2020 - [debug]   ok.
Wed Apr 15 22:00:54 2020 - [debug] 
Wed Apr 15 22:00:52 2020 - [debug]  Connecting via SSH from [email protected](10.1.1.107:22) to [email protected](10.1.1.109:22)..
Wed Apr 15 22:00:53 2020 - [debug]   ok.
Wed Apr 15 22:00:53 2020 - [debug]  Connecting via SSH from [email protected](10.1.1.107:22) to [email protected](10.1.1.108:22)..
Wed Apr 15 22:00:53 2020 - [debug]   ok.
Wed Apr 15 22:00:54 2020 - [debug] 
Wed Apr 15 22:00:52 2020 - [debug]  Connecting via SSH from [email protected](10.1.1.108:22) to [email protected](10.1.1.109:22)..
Wed Apr 15 22:00:53 2020 - [debug]   ok.
Wed Apr 15 22:00:53 2020 - [debug]  Connecting via SSH from [email protected](10.1.1.108:22) to [email protected](10.1.1.107:22)..
Wed Apr 15 22:00:53 2020 - [debug]   ok.
Wed Apr 15 22:00:54 2020 - [info] All SSH connection tests passed successfully.
[root@mha ~]# masterha_check_repl --conf=/etc/mastermha/mha.cnf
Wed Apr 15 22:22:29 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr 15 22:22:29 2020 - [info] Reading application default configuration from /etc/mastermha/mha.cnf..
Wed Apr 15 22:22:29 2020 - [info] Reading server configuration from /etc/mastermha/mha.cnf..
Wed Apr 15 22:22:29 2020 - [info] MHA::MasterMonitor version 0.56.
Wed Apr 15 22:22:30 2020 - [info] GTID failover mode = 0
Wed Apr 15 22:22:30 2020 - [info] Dead Servers:
Wed Apr 15 22:22:30 2020 - [info] Alive Servers:
Wed Apr 15 22:22:30 2020 - [info]   10.1.1.109(10.1.1.109:3306)
Wed Apr 15 22:22:30 2020 - [info]   10.1.1.107(10.1.1.107:3306)
Wed Apr 15 22:22:30 2020 - [info]   10.1.1.108(10.1.1.108:3306)
Wed Apr 15 22:22:30 2020 - [info] Alive Slaves:
Wed Apr 15 22:22:30 2020 - [info]   10.1.1.107(10.1.1.107:3306)  Version=5.5.64-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Apr 15 22:22:30 2020 - [info]     Replicating from 10.1.1.109(10.1.1.109:3306)
Wed Apr 15 22:22:30 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Apr 15 22:22:30 2020 - [info]   10.1.1.108(10.1.1.108:3306)  Version=5.5.64-MariaDB (oldest major version between slaves) log-bin:enabled
Wed Apr 15 22:22:30 2020 - [info]     Replicating from 10.1.1.109(10.1.1.109:3306)
Wed Apr 15 22:22:30 2020 - [info] Current Alive Master: 10.1.1.109(10.1.1.109:3306)
Wed Apr 15 22:22:30 2020 - [info] Checking slave configurations..
Wed Apr 15 22:22:30 2020 - [info] Checking replication filtering settings..
Wed Apr 15 22:22:30 2020 - [info]  binlog_do_db= , binlog_ignore_db= 
Wed Apr 15 22:22:30 2020 - [info]  Replication filtering check ok.
Wed Apr 15 22:22:30 2020 - [info] GTID (with auto-pos) is not supported
Wed Apr 15 22:22:30 2020 - [info] Starting SSH connection tests..
Wed Apr 15 22:22:33 2020 - [info] All SSH connection tests passed successfully.
Wed Apr 15 22:22:33 2020 - [info] Checking MHA Node version..
Wed Apr 15 22:22:34 2020 - [info]  Version check ok.
Wed Apr 15 22:22:34 2020 - [info] Checking SSH publickey authentication settings on the current master..
Wed Apr 15 22:22:35 2020 - [info] HealthCheck: SSH to 10.1.1.109 is reachable.
Wed Apr 15 22:22:35 2020 - [info] Master MHA Node version is 0.56.
Wed Apr 15 22:22:35 2020 - [info] Checking recovery script configurations on 10.1.1.109(10.1.1.109:3306)..
Wed Apr 15 22:22:35 2020 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/data/mastermha/mha1//save_binary_logs_test --manager_version=0.56 --start_file=master-bin.000002 
Wed Apr 15 22:22:35 2020 - [info]   Connecting to [email protected](10.1.1.109:22).. 
  Creating /data/mastermha/mha1 if not exists.. Creating directory /data/mastermha/mha1.. done.
   ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to master-bin.000002
Wed Apr 15 22:22:36 2020 - [info] Binlog setting check done.
Wed Apr 15 22:22:36 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Wed Apr 15 22:22:36 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=10.1.1.107 --slave_ip=10.1.1.107 --slave_port=3306 --workdir=/data/mastermha/mha1/ --target_version=5.5.64-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Wed Apr 15 22:22:36 2020 - [info]   Connecting to [email protected](10.1.1.107:22).. 
Creating directory /data/mastermha/mha1/.. done.
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to relay-log.000006
    Temporary relay log file is /var/lib/mysql/relay-log.000006
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Apr 15 22:22:36 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=10.1.1.108 --slave_ip=10.1.1.108 --slave_port=3306 --workdir=/data/mastermha/mha1/ --target_version=5.5.64-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Wed Apr 15 22:22:36 2020 - [info]   Connecting to [email protected](10.1.1.108:22).. 
Creating directory /data/mastermha/mha1/.. done.
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to relay-log.000005
    Temporary relay log file is /var/lib/mysql/relay-log.000005
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Apr 15 22:22:36 2020 - [info] Slaves settings check done.
Wed Apr 15 22:22:36 2020 - [info] 
10.1.1.109(10.1.1.109:3306) (current master)
 +--10.1.1.107(10.1.1.107:3306)
 +--10.1.1.108(10.1.1.108:3306)

Wed Apr 15 22:22:36 2020 - [info] Checking replication health on 10.1.1.107..
Wed Apr 15 22:22:36 2020 - [info]  ok.
Wed Apr 15 22:22:36 2020 - [info] Checking replication health on 10.1.1.108..
Wed Apr 15 22:22:36 2020 - [info]  ok.
Wed Apr 15 22:22:36 2020 - [warning] master_ip_failover_script is not defined.
Wed Apr 15 22:22:36 2020 - [warning] shutdown_script is not defined.
Wed Apr 15 22:22:36 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

###mha启动
[root@mha ~]nohup masterha_manager --conf=/etc/mastermha/mha.cnf &
[1] 2233
[root@mha ~]# nohup: ignoring input and appending output to `nohup.out'
[root@mha ~]# 
[root@mha ~]# 
[root@mha ~]# 
[root@mha ~]# ps aux |grep masterha_manager
root      2233  1.6  1.9 194908 19380 pts/0    S    22:35   0:00 perl /usr/bin/masterha_manager --conf=/etc/mastermha/mha.cnf
root      2287  0.0  0.0 103324   852 pts/0    S+   22:35   0:00 grep masterha_manager
[root@mha ~]# 

11)验证

停止master的mysql服务,然后看mha日志:

###mha日志包含了整个主mysql切换到新的主mysql
[root@mha ~]# tailf /data/mastermha/mha1/manager.log
10.1.1.109(10.1.1.109:3306) (current master)
 +--10.1.1.107(10.1.1.107:3306)
 +--10.1.1.108(10.1.1.108:3306)

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

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

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

mha: MySQL Master failover 10.1.1.109(10.1.1.109:3306) to 10.1.1.107(10.1.1.107:3306) succeeded

Master 10.1.1.109(10.1.1.109:3306) is down!

Check MHA Manager logs at mha:/data/mastermha/mha1/manager.log for details.

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

###masterha_manager已经自动退出
[root@mha ~]# ps aux |grep masterha_manager |grep -v grep
[root@mha ~]#