MySQL——MHA高可用群集部署及故障測試

明月人倚樓發表於2020-11-03

目錄

 


 

摘要

MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,它由日本人youshimation開發,是一套優秀的MySQL故障切換和主從提升的高可用軟體,解決mysql主伺服器單點故障的方法一般常用的有兩個mmm和mha,但是一般從真正的高可用的場合,其實最常用的是mha。如何避免mysql單點故障利用mysql主從複製來解決mysql單點故障如何解決主伺服器的單點問題主伺服器切換後,如何通知應用新的主伺服器的ip地址如何檢查mysql主伺服器是否可用如何處理從伺服器和新主伺服器之間的那種複製關係

 

一、案例前置知識

 

1.1、MHA概述

1.1.1、一套優秀的MySQL高可用環境下故障切換和主從複製的軟體

1.1.2、MySQL故障過程中,MHA能做到0-30秒內自動完成故障切換

1.2、MHA的組成

1.2.1、MHA Manager(管理節點)

Manager可以單獨部署在一臺獨立的機器上管理多個master-slave叢集,也可以部署在一臺slave節點上。

1.2.2、MHA Node(資料節點)

Node執行在每臺MySQL伺服器上,MHA Manager會定時探測叢集中的master節點,當master出現故障時,它可以自動將最新資料的slave提升為新的master,然後將所有其他的slave重新指向新的master。

1.3、MHA特點

1.3.1、自動故障切換過程中,MHA試圖從當機的主伺服器上儲存二進位制日誌,最大程度的保證資料不丟失

1.3.2、使用半同步複製,可以大大降低資料丟失的風險

1.3.3、目前MHA支援一主多從架構,最少三臺服務,即一主兩從

1.4、MHA缺點

1.4.1、需要編寫指令碼或利用第三方工具來實現vip的配置

1.4.2、MHA啟動後只會對資料庫進行監控,需要基於ssh免認證配置,存在一定的安全隱患

1.4.3、沒有提供從伺服器的讀負載均很的功能。

 

二、案例拓撲圖

 

 

 

三、案例部署

3.1、部署思路

3.1.1、MHA架構

①資料庫安裝

②一主兩從

③MHA搭建

3.1.2、故障模擬

①主庫失效

②備選主庫成為主庫

③從庫2將備選主庫指向為主庫

3.1.3、故障修復

①壞庫修復,啟動

②在修復好的庫上建立新主從關係

③修改manager配置檔案,新增修好的庫的記錄

④重啟mha

主伺服器:mysql-server 20.0.0.10,從1伺服器:mysql-slave1 20.0.0.20,從2伺服器:mysql-slave2 20.0.0.30,mha:20.0.0.40

 3.2、在三臺 MySQL 節點上分別安裝資料庫

 MySQL 版本請使用 5.6.36,cmake 版本請使用 2.8.6。安裝過程如下。

1 主伺服器
 2 yum -y install ncurses-devel gcc-c++ perl-Module-Install
 3 tar zxvf cmake-2.8.6.tar.gz
 4 cd cmake-2.8.6
 5 ./configure
 6 gmake && gmake install
 7 cd
 8 tar zxvf mysql-5.6.36.tar.gz
 9 cd mysql-5.6.36
10 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
11 make && make install
12 cp support-files/my-default.cnf /etc/my.cnf
13 cp support-files/mysql.server /etc/rc.d/init.d/mysqld
14 chmod +x /etc/rc.d/init.d/mysqld
15 chkconfig --add mysqld
16 echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
17 source /etc/profile
18 groupadd mysql
19 useradd -M -s /sbin/nologin mysql -g mysql
20 chown -R mysql.mysql /usr/local/mysql
21 mkdir -p /data/mysql
22 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
23 echo '
24 [client]
25 port = 3306
26 socket = /usr/local/mysql/mysql.sock
27 
28 [mysql]
29 port = 3306
30 socket = /usr/local/mysql/mysql.sock
31 
32 [mysqld]
33 user = mysql
34 basedir = /usr/local/mysql
35 datadir = /usr/local/mysql/data
36 port = 3306
37 pid-file = /usr/local/mysql/mysqld.pid
38 socket = /usr/local/mysql/mysql.sock
39 server-id = 1
40 log_bin = master-bin
41 log-slave-updates = true
42 
43 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
44 ln -s /usr/local/mysql/bin/mysql /usr/sbin/
45 ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
46 systemctl start mysqld
47 netstat -anpt | grep 3306
 1 從1伺服器
 2 yum -y install ncurses-devel gcc-c++ perl-Module-Install
 3 tar zxvf cmake-2.8.6.tar.gz
 4 cd cmake-2.8.6
 5 ./configure
 6 gmake && gmake install
 7 cd
 8 tar zxvf mysql-5.6.36.tar.gz
 9 cd mysql-5.6.36
10 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
11 make && make install
12 cp support-files/my-default.cnf /etc/my.cnf
13 cp support-files/mysql.server /etc/rc.d/init.d/mysqld
14 chmod +x /etc/rc.d/init.d/mysqld
15 chkconfig --add mysqld
16 echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
17 source /etc/profile
18 groupadd mysql
19 useradd -M -s /sbin/nologin mysql -g mysql
20 chown -R mysql.mysql /usr/local/mysql
21 mkdir -p /data/mysql
22 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
23 echo '
24 [client]
25 port = 3306
26 socket = /usr/local/mysql/mysql.sock
27 
28 [mysql]
29 port = 3306
30 socket = /usr/local/mysql/mysql.sock
31 
32 [mysqld]
33 user = mysql
34 basedir = /usr/local/mysql
35 datadir = /usr/local/mysql/data
36 port = 3306
37 pid-file = /usr/local/mysql/mysqld.pid
38 socket = /usr/local/mysql/mysql.sock
39 server-id = 2
40 log_bin = master-bin
41 relay-log = relay-log-bin 
42 relay-log-index = slave-relay-bin.index 
43 
44 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
45 ln -s /usr/local/mysql/bin/mysql /usr/sbin/
46 ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
47 systemctl start mysqld
48 netstat -anpt | grep 3306
 1 從2伺服器
 2 yum -y install ncurses-devel gcc-c++ perl-Module-Install
 3 tar zxvf cmake-2.8.6.tar.gz
 4 cd cmake-2.8.6
 5 ./configure
 6 gmake && gmake install
 7 cd
 8 tar zxvf mysql-5.6.36.tar.gz
 9 cd mysql-5.6.36
10 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
11 make && make install
12 cp support-files/my-default.cnf /etc/my.cnf
13 cp support-files/mysql.server /etc/rc.d/init.d/mysqld
14 chmod +x /etc/rc.d/init.d/mysqld
15 chkconfig --add mysqld
16 echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
17 source /etc/profile
18 groupadd mysql
19 useradd -M -s /sbin/nologin mysql -g mysql
20 chown -R mysql.mysql /usr/local/mysql
21 mkdir -p /data/mysql
22 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
23 echo '
24 [client]
25 port = 3306
26 socket = /usr/local/mysql/mysql.sock
27 
28 [mysql]
29 port = 3306
30 socket = /usr/local/mysql/mysql.sock
31 
32 [mysqld]
33 user = mysql
34 basedir = /usr/local/mysql
35 datadir = /usr/local/mysql/data
36 port = 3306
37 pid-file = /usr/local/mysql/mysqld.pid
38 socket = /usr/local/mysql/mysql.sock
39 server-id = 3
40 relay-log = relay-log-bin 
41 relay-log-index = slave-relay-bin.index 
42 
43 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
44 ln -s /usr/local/mysql/bin/mysql /usr/sbin/
45 ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
46 systemctl start mysqld
47 netstat -anpt | grep 3306

3.3、配置 MySQL一主兩從

3.3.1、MySQL 主從配置相對比較簡單,需要注意的是授權。步驟如下:在所有資料庫節點上授權兩個使用者,一個是從庫同步使用,另外一個是 manager 使用。

1 mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123';
2 mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
3 mysql> flush privileges;

3.3.2、在主伺服器上檢視二進位制檔案和同步點

1 mysql> show master status;
2 +-------------------+----------+--------------+------------------+-------------------+
3 | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
4 +-------------------+----------+--------------+------------------+-------------------+
5 | master-bin.000001 |      608 |              |                  |                   |
6 +-------------------+----------+--------------+------------------+-------------------+
7 1 row in set (0.00 sec

3.3.3、接下來在從1 和 從2 分別執行同步,檢視 IO 和 SQL 執行緒都是 yes 代表同步是否正常

1 mysql> change master to master_host='20.0.0.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=608; 
2 mysql> start slave;
3 mysql> show slave status\G;
4 Slave_IO_Running: Yes
5 Slave_SQL_Running: Yes
1 I/O執行緒顯示為NO: 主庫與從庫網路不通、主庫未授權給從庫
2 SQL執行緒顯示為NO:從庫日誌和位置點與主不同步
3 若從庫檢視連線主庫I/0執行緒狀態為conneting,一直是這個狀態,考慮雙方的防火牆是否開啟。

3.3.4、必須設定兩個從庫為只讀模式

1 mysql> set global read_only=1;

3.3.5、在主庫插入兩條資料,測試是否同步

1 mysql> create database test_xuminhlou;
2 mysql> use test_xuminhlou;
3 mysql> create table test(id int);
4 mysql> insert into test(id) values (1);
5 mysql> select * from test;

3.3.6、在兩個從庫分別查詢如下所示說明主從同步正常

 1 從1
 2 mysql> select * from test_xuminglou.test;
 3 +------+
 4 | id   |
 5 +------+
 6 |    1 |
 7 +------+
 8 1 row in set (0.00 sec)
 9 從2
10 mysql> select * from test_xuminglou.test;
11 +------+
12 | id   |
13 +------+
14 |    1 |
15 +------+
16 1 row in set (0.00 sec)

3.4、安裝MHA軟體

3.4.1、在所有伺服器上都安裝 MHA 依賴的環境,每個伺服器上都需要兩個源(epel.repo和CentOS7-Base-163.repo),將這兩個源放入/etc/yum.repos.d的目錄下,在主伺服器上操作演示安裝

1  yum install epel-release --nogpgcheck
2  yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

3.4.2、在所有伺服器上必須先安裝 node 元件,最後在 MHA-manager 節點上安裝 manager 元件,因為 manager 依賴 node 元件

1 tar zxvf mha4mysql-node-0.57.tar.gz
2 cd mha4mysql-node-0.57
3 perl Makefile.PL
4 make && make install

3.4.3、在mha-manager 伺服器上安裝 manager 元件

 1 tar zxvf mha4mysql-manager-0.57.tar.gz 
 2 cd mha4mysql-manager-0.57
 3 perl Makefile.PL
 4 
 5 *** Module::AutoInstall version 1.06
 6 *** Checking for Perl dependencies...
 7 [Core Features]
 8 - DBI                   ...loaded. (1.627)
 9 - DBD::mysql            ...loaded. (4.023)
10 - Time::HiRes           ...loaded. (1.9725)
11 - Config::Tiny          ...loaded. (2.14)
12 - Log::Dispatch         ...loaded. (2.41)
13 - Parallel::ForkManager ...loaded. (1.18)
14 - MHA::NodeConst        ...loaded. (0.57)
15 *** Module::AutoInstall configuration finished.
16 Checking if your kit is complete...
17 Looks good
18 Writing Makefile for mha4mysql::manager
19 
20 make && make install
1 manager 安裝後在/usr/local/bin 下面會生成幾個工具,主要包括以下幾個:
2 masterha_check_ssh             檢查 MHA 的 SSH 配置狀況
3 masterha_check_repl            檢查 MySQL 複製狀況
4 masterha_manger                啟動 manager的指令碼
5 masterha_check_status          檢測當前 MHA 執行狀態
6 masterha_master_monitor        檢測 master 是否當機
7 masterha_master_switch         控制故障轉移(自動或者手動)
8 masterha_conf_host             新增或刪除配置的 server 資訊
9 masterha_stop                  關閉manager
1 node 安裝後也會在/usr/local/bin 下面會生成幾個指令碼(這些工具通常由 MHA-Manager 的指令碼觸發,無需人為操作)主要如下:
2 save_binary_logs               儲存和複製 master 的二進位制日誌
3 apply_diff_relay_logs          識別差異的中繼日誌事件並將其差異的事件應用於其他的 slave
4 filter_mysqlbinlog             去除不必要的 ROLLBACK 事件(MHA 已不再使用這個工具)
5 purge_relay_logs               清除中繼日誌(不會阻塞 SQL 執行緒)

3.5、配置無密碼認證

3.5.1、 在 manager伺服器 上配置到所有節點的無密碼認證

1 ssh-keygen -t rsa       #一路按Enter鍵
2 ssh-copy-id 20.0.0.10    #先輸入yes,再輸入20.0.0.10伺服器的密碼
3 ssh-copy-id 20.0.0.20
4 ssh-copy-id 20.0.0.30
5 驗證
6 ssh root@20.0.0.10
7 ssh root@20.0.0.20
8 ssh root@20.0.0.30

3.5.2、在主伺服器上配置到資料庫節點的無密碼認證

1 ssh-keygen -t rsa
2 ssh-copy-id 20.0.0.20
3 ssh-copy-id 20.0.0.30
4 驗證
5 ssh root@20.0.0.20
6 ssh root@20.0.0.30

3.5.3、在從1伺服器上配置到資料庫節點的無密碼認證

1 ssh-keygen -t rsa
2 ssh-copy-id 20.0.0.10
3 ssh-copy-id 20.0.0.30
4 驗證
5 ssh root@20.0.0.10
6 ssh root@20.0.0.30

3.5.4、在從2伺服器上配置到資料庫節點的無密碼認證

1 ssh-keygen -t rsa
2 ssh-copy-id 20.0.0.10
3 ssh-copy-id 20.0.0.20
4 驗證
5 ssh root@20.0.0.10
6 ssh root@20.0.0.20

 

3.6、 配置 MHA

3.6.1、在 manager 節點上覆制相關指令碼到/usr/local/bin 目錄,複製上述的自動切換時 VIP 管理的指令碼到/usr/local/bin 目錄,這裡使用指令碼管理 VIP

 1 cp -ra mha4mysql-manager-0.57/samples/scripts /usr/local/bin
 2 ll /usr/local/bin/scripts/
 3 cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
 4 vi /usr/local/bin/master_ip_failover
 5 刪除檔案裡的所有內容,複製下面的內容
 6 #!/usr/bin/env perl
 7 use strict;
 8 use warnings FATAL => 'all';
 9 
10 use Getopt::Long;
11 
12 my (
13 $command, $ssh_user, $orig_master_host, $orig_master_ip,
14 $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
15 );
16 #############################新增內容部分#########################################
17 my $vip = '20.0.0.200';
18 my $brdc = '20.0.0.255';
19 my $ifdev = 'ens33';
20 my $key = '1';
21 my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
22 my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
23 my $exit_code = 0;
24 #my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
25 #my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
26 ##################################################################################
27 GetOptions(
28 'command=s' => \$command,
29 'ssh_user=s' => \$ssh_user,
30 'orig_master_host=s' => \$orig_master_host,
31 'orig_master_ip=s' => \$orig_master_ip,
32 'orig_master_port=i' => \$orig_master_port,
33 'new_master_host=s' => \$new_master_host,
34 'new_master_ip=s' => \$new_master_ip,
35 'new_master_port=i' => \$new_master_port,
36 );
37 
38 exit &main();
39 
40 sub main {
41 
42 print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
43 
44 if ( $command eq "stop" || $command eq "stopssh" ) {
45 
46 my $exit_code = 1;
47 eval {
48 print "Disabling the VIP on old master: $orig_master_host \n";
49 &stop_vip();
50 $exit_code = 0;
51 };
52 if ($@) {
53 warn "Got Error: $@\n";
54 exit $exit_code;
55 }
56 exit $exit_code;
57 }
58 elsif ( $command eq "start" ) {
59 
60 my $exit_code = 10;
61 eval {
62 print "Enabling the VIP - $vip on the new master - $new_master_host \n";
63 &start_vip();
64 $exit_code = 0;
65 };
66 if ($@) {
67 warn $@;
68 exit $exit_code;
69 }
70 exit $exit_code;
71 }
72 elsif ( $command eq "status" ) {
73 print "Checking the Status of the script.. OK \n";
74 exit 0;
75 }
76 else {
77 &usage();
78 exit 1;
79 }
80 }
81 sub start_vip() {
82 `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
83 }
84 # A simple system call that disable the VIP on the old_master
85 sub stop_vip() {
86 `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
87 }
88 
89 sub usage {
90 print
91 "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";
92 }
 拷貝後會有四個執行檔案,檔案的含義如下:
 1 master_ip_failover              #自動切換時 VIP 管理的指令碼
 2 master_ip_online_change         #線上切換時 vip 的管理
 3 power_manager                   #故障發生後關閉主機的指令碼
 4 send_report                     #因故障切換後傳送報警的指令碼

3.6.2、建立 MHA 軟體目錄並拷貝配置檔案

 1 mkdir /etc/masterha
 2 cp mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
 3 vim /etc/masterha/app1.cnf
 4 [server default]
 5 manager_workdir=/var/log/masterha/app1
 6 manager_log=/var/log/masterha/app1/manager.log
 7 master_binlog_dir=/usr/local/mysql/data
 8 master_ip_failover_script= /usr/local/bin/master_ip_failover
 9 master_ip_online_change_script= /usr/local/bin/master_ip_online_change
10 password=manager
11 user=mha
12 ping_interval=1
13 remote_workdir=/tmp
14 repl_password=123
15 repl_user=myslave
16 secondary_check_script= /usr/local/bin/masterha_secondary_check -s 20.0.0.20 -s 20.0.0.30
17 shutdown_script=""
18 ssh_user=root
19 [server1]
20 hostname=20.0.0.10
21 port=3306
22 [server2]
23 hostname=20.0.0.20
24 port=3306
25 candidate_master=1
26 check_repl_delay=0
27 [server3]
28 hostname=20.0.0.30
29 port=3306

3.6.3、測試 ssh 無密碼認證,如果正常最後會輸出 successfully,如下所示


 1 masterha_check_ssh -conf=/etc/masterha/app1.cnf
 2 ......
 3 Wed Oct 28 00:07:09 2020 - [debug]  Connecting via SSH from root@20.0.0.30(20.0.0.30:22) to root@20.0.0.20(20.0.0.20:22)..
 4 Wed Oct 28 00:07:10 2020 - [debug]   ok.
 5 Wed Oct 28 00:07:10 2020 - [debug] 
 6 Wed Oct 28 00:07:08 2020 - [debug]  Connecting via SSH from root@20.0.0.20(20.0.0.20:22) to root@20.0.0.10(20.0.0.10:22)..
 7 Wed Oct 28 00:07:09 2020 - [debug]   ok.
 8 Wed Oct 28 00:07:09 2020 - [debug]  Connecting via SSH from root@20.0.0.20(20.0.0.20:22) to root@20.0.0.30(20.0.0.30:22)..
 9 Wed Oct 28 00:07:09 2020 - [debug]   ok.
10 Wed Oct 28 00:07:10 2020 - [info] All SSH connection tests passed successfully.

3.6.4、測試 MySQL 主從連線情況,最後出現 MySQL Replication Health is OK 字樣說明正常

 1 masterha_check_repl -conf=/etc/masterha/app1.cnf
 2 ......
 3 IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===
 4 
 5 Checking the Status of the script.. OK 
 6 Wed Oct 28 09:25:28 2020 - [info]  OK.
 7 Wed Oct 28 09:25:28 2020 - [warning] shutdown_script is not defined.
 8 Wed Oct 28 09:25:28 2020 - [info] Got exit code 0 (Not master dead).
 9 
10 MySQL Replication Health is OK.

3.6.5、啟動 MHA

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

3.6.6、檢視 MHA 狀態,可以看到當前的 master 是主伺服器節點

1 masterha_check_status --conf=/etc/masterha/app1.cnf
2 app1 (pid:24405) is running(0:PING_OK), master:20.0.0.10

3.6.7、檢視 MHA 日誌,也以看到當前的 master 是 20.0.0.10

1 cat /var/log/masterha/app1/manager.log
2 ......
3 Wed Oct 28 09:27:04 2020 - [info]     Replicating from 20.0.0.10(20.0.0.10:3306)
4 Wed Oct 28 09:27:04 2020 - [info] Current Alive Master: 20.0.0.10(20.0.0.10:3306)
5 Wed Oct 28 09:27:04 2020 - [info] Checking slave configurations..
6 Wed Oct 28 09:27:04 2020 - [warning]  relay_log_purge=0 is not set on slave 20.0.0.20(20.0.0.20:3306).
7 Wed Oct 28 09:27:04 2020 - [warning]  relay_log_purge=0 is not set on slave 20.0.0.30(20.0.0.30:3306).
8 Wed Oct 28 09:27:04 2020 - [warning]  log-bin is not set on slave 20.0.0.30(20.0.0.30:3306). This host cannot be a master.
9 ......

3.6.8、第一次配置vip的時候,需要在主伺服器上建立虛擬IP地址

1 ifconfig ens33:1 20.0.0.200/24
2 ifconfig

四、故障測試

 4.1、關閉主伺服器的mysql服務

1 pkill mysqld

4.2、檢視從1伺服器的虛擬IP地址有沒有轉移過來,從2上檢視主伺服器是不是從1

 1 從1伺服器
 2 ifconfig
 3 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
 4         inet 20.0.0.20  netmask 255.255.255.0  broadcast 20.0.0.255
 5         inet6 fe80::a140:18a2:d866:8f33  prefixlen 64  scopeid 0x20<link>
 6         ether 00:0c:29:42:10:0a  txqueuelen 1000  (Ethernet)
 7         RX packets 1315993  bytes 1324408060 (1.2 GiB)
 8         RX errors 0  dropped 0  overruns 0  frame 0
 9         TX packets 795190  bytes 69519345 (66.2 MiB)
10         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
11 
12 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
13         inet 20.0.0.200  netmask 255.0.0.0  broadcast 20.255.255.255
14         ether 00:0c:29:42:10:0a  txqueuelen 1000  (Ethernet)
15 
16 從2伺服器
17 mysql
18 mysql> show slave status \G
19 *************************** 1. row ***************************
20                Slave_IO_State: Waiting for master to send event
21                   Master_Host: 20.0.0.20
22                   Master_User: myslave
23                   Master_Port: 3306
24                 Connect_Retry: 60
25               Master_Log_File: master-bin.000003
26           Read_Master_Log_Pos: 120
27                Relay_Log_File: relay-log-bin.000002
28                 Relay_Log_Pos: 284
29         Relay_Master_Log_File: master-bin.000003
30              Slave_IO_Running: Yes
31             Slave_SQL_Running: Yes
32 ......

4.3、重新開啟manager伺服器

4.3.1、主伺服器開啟mysql服務

1 systemctl restart mysqld

4.3.2、檢視從1伺服器檢視二進位制檔案和同步點

1 mysql> show master status;
2 +-------------------+----------+--------------+------------------+-------------------+
3 | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
4 +-------------------+----------+--------------+------------------+-------------------+
5 | master-bin.000003 |      120 |              |                  |                   |
6 +-------------------+----------+--------------+------------------+-------------------+
7 1 row in set (0.00 sec)

4.3.3、在主伺服器上執行同步,檢視 IO 和 SQL 執行緒都是 yes 代表同步是否正常

 1 mysql>  change master to
 2 master_host='20.0.0.20',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120;
 3 mysql> start slave;
 4 mysql> show slave status \G
 5 *************************** 1. row ***************************
 6                Slave_IO_State: Waiting for master to send event
 7                   Master_Host: 20.0.0.20
 8                   Master_User: myslave
 9                   Master_Port: 3306
10                 Connect_Retry: 60
11               Master_Log_File: master-bin.000003
12           Read_Master_Log_Pos: 120
13                Relay_Log_File: mysqld-relay-bin.000002
14                 Relay_Log_Pos: 284
15         Relay_Master_Log_File: master-bin.000003
16              Slave_IO_Running: Yes
17             Slave_SQL_Running: Yes
18  ......

4.3.4、在從1上建立,主伺服器和從2上檢視

 1 從1伺服器
 2 mysql> create database aa;
 3 mysql> use aa;
 4 mysql> create table test(id int(3));
 5 mysql> insert into test values(1);
 6 mysql> select * from test;
 7 +------+
 8 | id   |
 9 +------+
10 |    1 |
11 +------+
12 
13 主伺服器
14 mysql> use aa;
15 mysql> select * from test;
16 +------+
17 | id   |
18 +------+
19 |    1 |
20 +------+
21 
22 從2伺服器
23 mysql> use aa;
24 mysql> select * from test;
25 +------+
26 | id   |
27 +------+
28 |    1 |
29 +------+

4.3.5、在manager伺服器上修改配置檔案(再把這個記錄新增進去,因為它檢測掉失效時候會自動消失)

1 vi /etc/masterha/app1.cnf
2 ......
3 [server1]
4 hostname=192.168.8.134
5 port=3306
6 ......

4.3.6、在manager伺服器上啟動manager

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

總結

MHA目前在MySQL高可用方面是一個相對成熟的解決方案,在進行故障切換的過程中,MHA能最大程度上保證資料庫的一致性,以達到真正意義上的高可用

 

1、從當機崩潰的master儲存二進位制日誌事件(binlog events);

2、識別含有最新更新的slave;

3、應用差異的中繼日誌(relay log)到其他slave;

4、應用從master儲存的二進位制日誌事件(binlog events);

5、提升一個slave為新master;

6、使用其他的slave連線新的master進行復制。

 

相關文章