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

八雲家的小幼紫發表於2020-11-05

一、案例前置知識

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。安裝過程如下。
3.2.1、在主伺服器上

主伺服器
yum -y install ncurses-devel gcc-c++ perl-Module-Install
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake && gmake install
cd
tar zxvf mysql-5.6.36.tar.gz
cd mysql-5.6.36
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
make && make install
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql.mysql /usr/local/mysql
mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
echo '
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
log_bin = master-bin
log-slave-updates = true

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
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
systemctl start mysqld
netstat -anpt | grep 3306

3.2.2、在伺服器1上

從1伺服器
yum -y install ncurses-devel gcc-c++ perl-Module-Install
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake && gmake install
cd
tar zxvf mysql-5.6.36.tar.gz
cd mysql-5.6.36
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
make && make install
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql.mysql /usr/local/mysql
mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
echo '
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

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
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
systemctl start mysqld
netstat -anpt | grep 3306

3.2.3、在伺服器2上

從2伺服器
yum -y install ncurses-devel gcc-c++ perl-Module-Install
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake && gmake install
cd
tar zxvf mysql-5.6.36.tar.gz
cd mysql-5.6.36
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
make && make install
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql.mysql /usr/local/mysql
mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
echo '
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 3
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

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
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
systemctl start mysqld
netstat -anpt | grep 3306

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

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

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

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      608 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

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

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; 
mysql> start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

1 I/O執行緒顯示為NO: 主庫與從庫網路不通、主庫未授權給從庫
2 SQL執行緒顯示為NO:從庫日誌和位置點與主不同步
3 若從庫檢視連線主庫I/0執行緒狀態為conneting,一直是這個狀態,考慮雙方的防火牆是否開啟。
3.3.4、必須設定兩個從庫為只讀模式

mysql> set global read_only=1;

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

mysql> create database test_xuhao;
mysql> use test_xuhao;
mysql> create table test(id int);
mysql> insert into test(id) values (1);
mysql> select * from test;

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

從1
mysql> select * from test_xuhao.test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
從2
mysql> select * from test_xuhao.test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

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

 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 元件

tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install

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

tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL

*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.627)
- DBD::mysql            ...loaded. (4.023)
- Time::HiRes           ...loaded. (1.9725)
- Config::Tiny          ...loaded. (2.14)
- Log::Dispatch         ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst        ...loaded. (0.57)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::manager

make && make install
manager 安裝後在/usr/local/bin 下面會生成幾個工具,主要包括以下幾個:
masterha_check_ssh             檢查 MHA 的 SSH 配置狀況
masterha_check_repl            檢查 MySQL 複製狀況
masterha_manger                啟動 manager的指令碼
masterha_check_status          檢測當前 MHA 執行狀態
masterha_master_monitor        檢測 master 是否當機
masterha_master_switch         控制故障轉移(自動或者手動)
masterha_conf_host             新增或刪除配置的 server 資訊
masterha_stop                  關閉manager
node 安裝後也會在/usr/local/bin 下面會生成幾個指令碼(這些工具通常由 MHA-Manager 的指令碼觸發,無需人為操作)主要如下:
save_binary_logs               儲存和複製 master 的二進位制日誌
apply_diff_relay_logs          識別差異的中繼日誌事件並將其差異的事件應用於其他的 slave
filter_mysqlbinlog             去除不必要的 ROLLBACK 事件(MHA 已不再使用這個工具)
purge_relay_logs               清除中繼日誌(不會阻塞 SQL 執行緒)

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

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

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

ssh-keygen -t rsa
ssh-copy-id 20.0.0.20
ssh-copy-id 20.0.0.30
驗證
ssh root@20.0.0.20
ssh root@20.0.0.30

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

ssh-keygen -t rsa
ssh-copy-id 20.0.0.10
ssh-copy-id 20.0.0.30
驗證
ssh root@20.0.0.10
ssh root@20.0.0.30

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

ssh-keygen -t rsa
ssh-copy-id 20.0.0.10
ssh-copy-id 20.0.0.20
驗證
ssh root@20.0.0.10
ssh root@20.0.0.20

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

cp -ra mha4mysql-manager-0.57/samples/scripts /usr/local/bin
ll /usr/local/bin/scripts/
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
vi /usr/local/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 = '20.0.0.200';
my $brdc = '20.0.0.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#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;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_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 \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`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";
}
拷貝後會有四個執行檔案,檔案的含義如下:
 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_change10 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,如下所示

masterha_check_ssh -conf=/etc/masterha/app1.cnf
......
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)..
Wed Oct 28 00:07:10 2020 - [debug]   ok.
Wed Oct 28 00:07:10 2020 - [debug]
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)..
Wed Oct 28 00:07:09 2020 - [debug]   ok.
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)..
Wed Oct 28 00:07:09 2020 - [debug]   ok.
Wed Oct 28 00:07:10 2020 - [info] All SSH connection tests passed successfully.

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

masterha_check_repl -conf=/etc/masterha/app1.cnf
......
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===

Checking the Status of the script.. OK
Wed Oct 28 09:25:28 2020 - [info]  OK.
Wed Oct 28 09:25:28 2020 - [warning] shutdown_script is not defined.
Wed Oct 28 09:25:28 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

3.6.5、啟動 MHA

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 是主伺服器節點

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

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

cat /var/log/masterha/app1/manager.log
......
Wed Oct 28 09:27:04 2020 - [info]     Replicating from 20.0.0.10(20.0.0.10:3306)
Wed Oct 28 09:27:04 2020 - [info] Current Alive Master: 20.0.0.10(20.0.0.10:3306)
Wed Oct 28 09:27:04 2020 - [info] Checking slave configurations..
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).
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).
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.
......

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

ifconfig ens33:1 20.0.0.200/24
ifconfig

四、故障測試
4.1、關閉主伺服器的mysql服務

pkill mysqld

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

從1伺服器
ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.20  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::a140:18a2:d866:8f33  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:42:10:0a  txqueuelen 1000  (Ethernet)
        RX packets 1315993  bytes 1324408060 (1.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 795190  bytes 69519345 (66.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.200  netmask 255.0.0.0  broadcast 20.255.255.255
        ether 00:0c:29:42:10:0a  txqueuelen 1000  (Ethernet)

從2伺服器
mysql
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.20
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 120
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
......

4.3、重新開啟manager伺服器
4.3.1、主伺服器開啟mysql服務

systemctl restart mysqld

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

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

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

mysql>  change master to
master_host='20.0.0.20',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.20
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes18  ......

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

從1伺服器
mysql> create database aa;
mysql> use aa;
mysql> create table test(id int(3));
mysql> insert into test values(1);
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+

主伺服器
mysql> use aa;
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+

從2伺服器
mysql> use aa;
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+

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

vi /etc/masterha/app1.cnf
......
[server1]
hostname=192.168.8.134
port=3306
......

4.3.6、在manager伺服器上啟動manager

masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf
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 &
masterha_check_status --conf=/etc/masterha/app1.cnf

相關文章