mysql+MHA高可用 (一主雙從)


1、准備三台服務器

10.0.0.12

10.0.0.13

10.0.0.14

2、在三台服務器上執行操作

時間同步

[root@ c7m01 ~]# echo "*/5* * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root

修改主機名(主)

[root@ c7m01 ~]# vim /etc/hostname 

c7m01

修改主機名(從)

[root@ c702 ~]# vim /etc/hostname

c702

修改主機名(從)

[root@ c703 ~]# vim /etc/hostname 

c703

修改完主機名重啟機子就可以

3、三台服務器配置hosts解析(這里的主機名要和自己的一樣,以便於區分)

[root@ c7m01 ~]# vim /etc/hostsEOF 

c7m01 10.0.0.12
c702  10.0.0.13
c703  10.0.0.14

4、關閉防火牆和selinux  (三台服務器執行同樣的操作)

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

[root@ c7m01 ~]# sed -i ' /^SELINUX/s#enforcing#disabled#g' /etc/selinux/config

5、配置免密登錄(三台服務器執行同樣的操作)

[root@ c7m01 ~]# vim ssh.sh

#!/bin/bash
yum -y install sshpass &> /dev/null
read -p "請輸入服務器密碼:" passwd
UserName=root
IP="10.0.0."
#創建密鑰
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null
#分發公鑰
for i in 12 13 14   #這里的要改成自己機子的ip
  do
    sshpass -p "$passwd" ssh-copy-id -i ~/.ssh/id_dsa.pub -p 22 -o StrictHostKeyChecking=no $UserName@$IP$i &>/dev/null
done

6、執行腳本配置,並連接其中一台服務器

[root@ c7m01 ~]# sh ssh.sh 
請輸入服務器密碼:123456
[root@ c7m01 ~]# 
[root@ c7m01 ~]# ssh root@10.0.0.13
Last login: Fri Feb 14 00:14:41 2020 from 10.0.0.1
[root@ c702 ~]# exit
logout
Connection to 10.0.0.13 closed.

7、mysql安裝yum repo(三台服務器執行同樣的操作)

[root@ c7m01 ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[root@ c7m01 ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

[root@ c7m01 ~]# yum -y install mysql-server

8、啟動MySQL,三台服務器執行同樣的操作

[root@ c7m01 ~]# systemctl restart mysql

9、修改MySQL密碼,三台服務器執行同樣的操作

mysql> update mysql.user set password=password('123456') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

mysql> 

主服務器上操作

10、在c7m01服務器上更改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=1
log-bin=mysql-bin
#禁止mysql自動刪除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,開啟gtid,必須主從全開
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

更改完成后重啟mysql
systemctl restart mysql

11、創建同步用戶

[root@ c7m01 ~]# mysql -uroot -p123456
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.6.47-log MySQL Community Server (GPL)

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

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

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

mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

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

12、查看MySQL主庫的master狀態

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 530
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: f45fe1ec-53a4-11ea-899f-000c29d39f7e:1-2
1 row in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000002 |      530 |              |                  | f45fe1ec-53a4-11ea-899f-000c29d39f7e:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

13、查看GTID狀態

mysql> show global variables like '%gtid%';
+---------------------------------+------------------------------------------+
| Variable_name                   | Value                                    |
+---------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery     | OFF                                      |
| enforce_gtid_consistency        | ON                                       |  #執行GTID模塊
| gtid_executed                   | f45fe1ec-53a4-11ea-899f-000c29d39f7e:1-2 |
| gtid_mode                       | ON                                       |  #開啟GTID模塊
| gtid_owned                      |                                          |
| gtid_purged                     |                                          |
| simplified_binlog_gtid_recovery | OFF                                      |
+---------------------------------+------------------------------------------+
7 rows in set (0.00 sec)

c702從服務器上操作

14、在c702服務器上更改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=2
log-bin=mysql-bin
#禁止mysql自動刪除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,開啟gtid,必須主從全開
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

更改完成后重啟mysql
systemctl restart mysql

15、創建同步用戶

[root@ c702 ~]# mysql -uroot -p123456
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.6.47-log MySQL Community Server (GPL)

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

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

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

mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

16、關閉復制功能,配置指向master,開啟服務器復制狀態,檢查復制狀態,出現倆個yes狀態表示成功

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host='10.0.0.12',
    -> master_user='rep',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=530;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

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

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql
> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.12 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 530 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000002 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: 530 Relay_Log_Space: 519 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: f45fe1ec-53a4-11ea-899f-000c29d39f7e 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 the slave I/O thread to update it 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: fdcedb5b-53a4-11ea-899f-000c2927e658:1-5 Auto_Position: 0 1 row in set (0.00 sec) mysql>

c703服務器

17、在c702服務器上更改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=3
log-bin=mysql-bin
#禁止mysql自動刪除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,開啟gtid,必須主從全開
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

更改完成后重啟mysql
systemctl restart mysql

18、創建同步用戶

[root@ c703 ~]# mysql -uroot -p123456
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.6.47-log MySQL Community Server (GPL)

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

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

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

mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

19、關閉復制功能,配置指向master,開啟服務器復制狀態,檢查復制狀態,出現倆個yes狀態表示成功

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to
    -> master_host='10.0.0.12',
    -> master_user='rep',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=530;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

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

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql
> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.12 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 530 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000002 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: 530 Relay_Log_Space: 519 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: f45fe1ec-53a4-11ea-899f-000c29d39f7e 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 the slave I/O thread to update it 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: 0033cbb6-53a5-11ea-899f-000c298eeea7:1-4 Auto_Position: 0 1 row in set (0.00 sec)

20、在三台服務器裝MHA的依賴

yum -y install perl-dbd-mysql

yum -y install per-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForKManager perl-Time-HiRes

21、授權MHA管理用戶(三台服務器執行同樣的操作)

mysql> grant all privileges on *.* to mha@'10.0.0.%' identified by 'mha';
Query OK, 0 rows affected (0.00 sec)

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

22、安裝MHA node節點

[root@ c7m01 ~]# ll
total 140
-rw-------. 1 root root  1277 2019-09-17 22:18 anaconda-ks.cfg
-rw-r--r--  1 root root 81024 2020-02-13 16:32 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root 36328 2020-02-13 16:32 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm.1
-rw-r--r--  1 root root   349 2020-02-13 16:53 ssh.sh
[root@ c7m01 ~]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-node-0.58-0.el7.centos ################################# [100%]
[root@ c7m01 ~]# 

在c703上執行

如果安裝到錯誤的機器上可以撤銷掉,否則后期vip漂移會出錯

[root@ c7m01 ~]# rpm -qa |grep mha4mysql-manager-0.58-0.el7.centos.noarch.rpm 
[root@ c7m01 ~]# rpm -e mha4mysql-manager

23、安裝管理MHA的管理節點

[root@ c703 ~]# ll
total 132
-rw-------. 1 root root  1277 2019-09-17 22:18 anaconda-ks.cfg
-rw-r--r--  1 root root 81024 2020-02-13 16:32 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root 36328 2020-02-13 16:32 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm
-rw-r--r--  1 root root   349 2020-02-13 16:37 ssh.sh
[root@ c703 ~]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-manager-0.58-0.el7.cent################################# [100%]

24、配置MHA

[root@ c703 ~]# mkdir -p /etc/mha
[root@ c703 ~]# mkdir -p /var/log/mha/app1
[root@ c703 ~]# vim /etc/mha/app1.cnf

[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql #binlog的目錄,如果說miysql的環境不一樣,binlog位置不同,每台服務器的binlog的位置寫在server標簽里面即可
user=mha
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root

[server1]
hostname=10.0.0.12
port=3306

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1  #如果這個節點掛了,mha將不可用,加上這個參數,slave掛了一樣可以用
no_master=1  #從不將這台主機轉換為master
#candidate_master=1 #如果候選master有延遲的話,relay日志超過100m,failover切換不能成功,加上此參數后會忽略延遲日志大小。
#check_repl_delay=0 #用防止master故障時,切換時slave有延遲,卡在那里切不過來

#注意這里的配置要把注釋和空格全部刪除

檢查ssh

[root@ c703 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Fri Feb 21 13:37:37 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Feb 21 13:37:37 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Fri Feb 21 13:37:37 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Fri Feb 21 13:37:37 2020 - [info] Starting SSH connection tests..
Fri Feb 21 13:37:38 2020 - [debug] 
Fri Feb 21 13:37:37 2020 - [debug]  Connecting via SSH from root@10.0.0.12(10.0.0.12:22) to root@10.0.0.13(10.0.0.13:22)..
Fri Feb 21 13:37:38 2020 - [debug]   ok.
Fri Feb 21 13:37:38 2020 - [debug]  Connecting via SSH from root@10.0.0.12(10.0.0.12:22) to root@10.0.0.14(10.0.0.14:22)..
Fri Feb 21 13:37:38 2020 - [debug]   ok.
Fri Feb 21 13:37:39 2020 - [debug] 
Fri Feb 21 13:37:37 2020 - [debug]  Connecting via SSH from root@10.0.0.13(10.0.0.13:22) to root@10.0.0.12(10.0.0.12:22)..
Fri Feb 21 13:37:39 2020 - [debug]   ok.
Fri Feb 21 13:37:39 2020 - [debug]  Connecting via SSH from root@10.0.0.13(10.0.0.13:22) to root@10.0.0.14(10.0.0.14:22)..
Fri Feb 21 13:37:39 2020 - [debug]   ok.
Fri Feb 21 13:37:40 2020 - [debug] 
Fri Feb 21 13:37:38 2020 - [debug]  Connecting via SSH from root@10.0.0.14(10.0.0.14:22) to root@10.0.0.12(10.0.0.12:22)..
Fri Feb 21 13:37:39 2020 - [debug]   ok.
Fri Feb 21 13:37:39 2020 - [debug]  Connecting via SSH from root@10.0.0.14(10.0.0.14:22) to root@10.0.0.13(10.0.0.13:22)..
Fri Feb 21 13:37:40 2020 - [debug]   ok.
Fri Feb 21 13:37:40 2020 - [info] All SSH connection tests passed successfully.
[root@ c703 ~]#

這里放一個報錯的

 

 解決辦法就是在三台服務器的mysql配置文件加一個跳過域名解析

skip-name-resolve

 

[root@ c703 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf

 

25、啟動MHA

[root@ c703 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
[1] 6041
[root@ c703 ~]# ps -ef |grep mha
root 6041 4852 0 14:37 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover
root 6066 4852 0 14:38 pts/0 00:00:00 grep --color=auto mha

26、查看MHA狀態

[root@ c703 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:6041) is running(0:PING_OK), master:10.0.0.12

27、關閉MHA

[root@ c703 ~]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1

28、從庫從新加入新生

[root@ c703 ~]# grep -i "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1

29、測試MHA故障轉移

停掉c7m01上的mysql 10.0.0.12

[root@ c7m01 ~]# systemctl stop mysql

30、在c703上查看slave的狀態,發現master_host變成10.0.0.13

[root@ c703 ~]# mysql -uroot -p123456 -e 'show slave status\G';
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 10.0.0.13
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 191
               Relay_Log_File: mysqld-relay-bin.000006
                Relay_Log_Pos: 401
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Connecting
            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: 191
              Relay_Log_Space: 2309
              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: 2003
                Last_IO_Error: error reconnecting to master 'rep@10.0.0.12:3306' - retry-time: 60  retries: 4
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: f45fe1ec-53a4-11ea-899f-000c29d39f7e
             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 the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 200221 14:48:05
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: f45fe1ec-53a4-11ea-899f-000c29d39f7e:3-4
            Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:1-6,
f45fe1ec-53a4-11ea-899f-000c29d39f7e:3-4
                Auto_Position: 0

31、查看c703的配置文件

[root@ c703 ~]# cat /etc/mha/app1.cnf 
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
user=mha
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1
no_master=1
#candidate_master=1
#check_repl_delay=0

當c7m01上的主庫盪機之后,MHA會自動檢查,發現主庫mysql停機,立刻會把從庫上提升為主庫,然后另一台服務器會把mysql主從復制的master_host改為新提升的主庫

32、MHA故障還原

先在c703上查看一下

[root@c703 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
Fri Feb 14 01:25:18 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.12', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='xxx';

33、在c7m01上執行

[root@c7m01 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='10.0.0.13',MASTER_PORT=3306, MASTER_AUTO_POSITION=1,MASTER_USER='rep',MASTER_PASSWORD='123456';"
[root@c7m01 ~]# mysql -uroot -p123456 -e "start slave;"

[root@c7m01 ~]# mysql -uroot -p123456 -e "show slave status \G"

這個時候的MHA是一次性的,當12上的mysql盪掉之后,就會把13的mysql變成主庫,當再次盪機之后,不會在發生改變

在c703上執行

34、修復mysql,將server1標簽加入到app1的配置文件中

[root@ c703 ~]# cat /etc/mha/app1.cnf 
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
user=mha
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root

[server1]
hostname=10.0.0.12
port=3306

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1
no_master=1
#candidate_master=1
#check_repl_delay=0

34、重新啟動MHA

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

配置vip漂移,有倆種方式

第一種是keepalived的方式,管理虛擬ip的漂移

第二種是MHA自帶腳本的方式,管理虛擬ip的漂移(這個漂移就是那個服務器的mysql庫提升為主庫,就漂移到那個上面,要根據binlog的最新slave方式提升)

35、MHA腳本方式

修改app1配置文件,添加

 

[root@ c703 ~]# vim /etc/mha/app1.cnf 

[server default]
master_ip_failover_script=/usr/bin/master_ip_failover    #添加這一行

 

編寫腳本

[root@ c703 ~]# vim /usr/bin/master_ip_failover 

#!/usr/bin/env perl

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

use Getopt::Long;

my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);

my $vip = '10.0.0.100/24';    #這里的vip地址寫一個與自己IP地址相同的IP段
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";    #這里的網卡要看自己外網的外卡是不是ens33,不是的話要改成自己的外網網卡
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";

GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);

exit &main();

sub main {

    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

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

        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {

        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
     return 0  unless  ($ssh_user);
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}

36、給腳本加權限

[root@ c703 ~]# chmod +x /usr/bin/master_ip_failover

37、手動添加vip,綁定到主庫的機子上,我這里是10.0.0.13

[root@ c702 ~]# ifconfig ens33:1 10.0.0.100/24
[root@ c702 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d3:9f:7e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.13/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/24 brd 10.0.0.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed3:9f7e/64 scope link 
       valid_lft forever preferred_lft forever

38、重啟MHA

[root@ c703 ~]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1
[root@ c703 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
[1] 6468

模擬主庫宕機vip飄移

在c702上操作

[root@ c702 ~]# systemctl stop mysql
[root@ c702 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d3:9f:7e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.12/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed3:9f7e/64 scope link 
       valid_lft forever preferred_lft forever

這個時候vip已經漂移到另一台服務器上,將另一台服務器做為主庫

查看c7m01上是否有vip漂移

 

[root@ c7m01 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:27:e6:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.13/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/24 brd 10.0.0.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe27:e658/64 scope link 
       valid_lft forever preferred_lft forever

 

當vip漂移到另一台服務器上表示成功,然后查看MHA的服務器上是否改變到另一台服務器上的master_host

 

[root@ c703 ~]# mysql -uroot -p123456 -e 'show slave status\G';
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 231
               Relay_Log_File: mysqld-relay-bin.000004
                Relay_Log_Pos: 401
        Relay_Master_Log_File: mysql-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: 231
              Relay_Log_Space: 2892
              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: fdcedb5b-53a4-11ea-899f-000c2927e658
             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 the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: fdcedb5b-53a4-11ea-899f-000c2927e658:1-7
            Executed_Gtid_Set: 0033cbb6-53a5-11ea-899f-000c298eeea7:1-6,
f45fe1ec-53a4-11ea-899f-000c29d39f7e:3-4,
fdcedb5b-53a4-11ea-899f-000c2927e658:1-7
                Auto_Position: 1
[1]+  Done                    nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1

 

這個時候MHA上app1配置文件里server2已經被誒剔除了

 

[root@ c703 ~]# cat /etc/mha/app1.cnf 
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/bin/master_ip_failover
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1]
hostname=10.0.0.12
port=3306

[server3]
hostname=10.0.0.14
ignore_fail=1
no_master=1
port=3306

 

這個時候開始修復,手動操作,將他恢復到之前的狀態

 

[root@ c703 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
Fri Feb 21 16:19:25 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.13', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='xxx';
[root@c702 ~]#systemctl restart mysql
[root@c702 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='10.0.0.12',MASTER_PORT=3306, MASTER_AUTO_POSITION=1,MASTER_USER='rep',MASTER_PASSWORD='123456';"
[root@c702 ~]# mysql -uroot -p123456 -e "start slave;" [root@c702 ~]# mysql -uroot -p123456 -e "show slave status \G"

#重啟MHA
 nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM