一.MHA簡介
MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案诚亚,它由日本DeNA公司的youshimaton(現(xiàn)就職于Facebook公司)開發(fā)跃闹,是一套優(yōu)秀的作為MySQL高可用性環(huán)境下故障切換和主從提升的高可用軟件纪他。在MySQL故障切換過程中,MHA能做到在0~30秒之內(nèi)自動完成數(shù)據(jù)庫的故障切換操作卵迂,并且在進(jìn)行故障切換的過程中苏章,MHA能在最大程度上保證數(shù)據(jù)的一致性,以達(dá)到真正意義上的高可用遏暴。
MHA架構(gòu)圖
MHA工作原理總結(jié)為以下幾條:
- 從宕機(jī)崩潰的master保存二進(jìn)制日志事件(binlog events);
- 識別含有最新更新的slave指黎;
- 應(yīng)用差異的中繼日志(relay log)到其他slave朋凉;
- 應(yīng)用從master保存的二進(jìn)制日志事件(binlog events);
- 提升一個slave為新master醋安;
- 使用其他的slave連接新的master進(jìn)行復(fù)制杂彭。
MHA特點:
- 故障切換時間 10-30秒
- 自動監(jiān)控Master以及故障轉(zhuǎn)移
- 在線切換Master到異機(jī)
二.MHA搭建
2.1 基本環(huán)境
操作系統(tǒng)版本:CentOS Linux release 7.8.2003 (Core)
MySQL版本 :5.7.31
VIP :10.31.1.241
主機(jī)信息:
角色 | IP | 主機(jī)名 | 網(wǎng)卡 | server_id | 功能 |
---|---|---|---|---|---|
Monitor Host | 10.31.1.119 | hp1 | ens192 | -- | 監(jiān)控復(fù)制組 |
Master | 10.31.1.120 | hp2 | ens192 | 101 | 響應(yīng)寫請求 |
Candidate Master | 10.31.1.121 | hp3 | ens192 | 102 | 響應(yīng)讀請求 |
Slave | 10.31.1.122 | hp4 | ens192 | 103 | 響應(yīng)讀請求 |
2.2 配置主從
配置 hp3墓毒、hp4兩臺同步hp2的主從復(fù)制,參考主從復(fù)制blog亲怠,此處略過
2.3 安裝Perl等依賴模塊
用root用戶在所有四個節(jié)點執(zhí)行下面的操作所计。
# 安裝一個epel源
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 用yum安裝依賴包
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
2.4 配置SSH登錄無密碼驗證
在hdp1 10.31.1.119(Monitor)上用root用戶執(zhí)行:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.120
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.121
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.122
在hdp2 10.31.1.120(Master)上用root用戶執(zhí)行:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.119
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.121
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.122
在hdp3 10.31.1.121(Candidate Master)上用root用戶執(zhí)行:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.119
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.120
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.122
在hdp4 10.31.1.122(Slave)上用root用戶執(zhí)行:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.119
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.120
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.31.1.121
2.5 安裝MHA Node
下載地址:https://github.com/yoshinorim/mha4mysql-manager/wiki/Downloads
在hdp2、hdp3团秽、hdp4上用root用戶執(zhí)行下面的操作主胧。
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
安裝完成后,在/usr/bin/目錄下有如下MHA相關(guān)文件:
apply_diff_relay_logs
filter_mysqlbinlog
purge_relay_logs
save_binary_logs
這些腳本工具通常由MHA Manager的腳本觸發(fā)徙垫,無需人為操作。腳本說明:
apply_diff_relay_logs:識別差異的中繼日志事件并將其差異的事件應(yīng)用于其它slave放棒。
filter_mysqlbinlog:去除不必要的ROLLBACK事件(MHA已不再使用這個工具)姻报。
purge_relay_logs:清除中繼日志(不會阻塞SQL線程)。
save_binary_logs:保存和復(fù)制master的二進(jìn)制日志间螟。
2.6 安裝MHA Manager
下載地址:https://github.com/yoshinorim/mha4mysql-manager/wiki/Downloads
在hdp1上用root用戶執(zhí)行下面的操作吴旋。
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
安裝報錯
[root@10-31-1-119 src]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
錯誤:依賴檢測失敗:
mha4mysql-node >= 0.54 被 mha4mysql-manager-0.56-0.el6.noarch 需要
perl(MHA::BinlogManager) 被 mha4mysql-manager-0.56-0.el6.noarch 需要
perl(MHA::NodeConst) 被 mha4mysql-manager-0.56-0.el6.noarch 需要
perl(MHA::NodeUtil) 被 mha4mysql-manager-0.56-0.el6.noarch 需要
perl(MHA::SlaveUtil) 被 mha4mysql-manager-0.56-0.el6.noarch 需要
[root@10-31-1-119 src]#
[root@10-31-1-119 src]#
[root@10-31-1-119 src]# yum -y install mha4mysql-manager-0.56-0.el6.noarch.rpm
已加載插件:fastestmirror, langpacks
正在檢查 mha4mysql-manager-0.56-0.el6.noarch.rpm: mha4mysql-manager-0.56-0.el6.noarch
mha4mysql-manager-0.56-0.el6.noarch.rpm 將被安裝
正在解決依賴關(guān)系
--> 正在檢查事務(wù)
---> 軟件包 mha4mysql-manager.noarch.0.0.56-0.el6 將被 安裝
--> 正在處理依賴關(guān)系 mha4mysql-node >= 0.54厢破,它被軟件包 mha4mysql-manager-0.56-0.el6.noarch 需要
Loading mirror speeds from cached hostfile
--> 正在處理依賴關(guān)系 perl(MHA::BinlogManager)荣瑟,它被軟件包 mha4mysql-manager-0.56-0.el6.noarch 需要
--> 正在處理依賴關(guān)系 perl(MHA::NodeConst),它被軟件包 mha4mysql-manager-0.56-0.el6.noarch 需要
--> 正在處理依賴關(guān)系 perl(MHA::NodeUtil)摩泪,它被軟件包 mha4mysql-manager-0.56-0.el6.noarch 需要
--> 正在處理依賴關(guān)系 perl(MHA::SlaveUtil)笆焰,它被軟件包 mha4mysql-manager-0.56-0.el6.noarch 需要
--> 解決依賴關(guān)系完成
錯誤:軟件包:mha4mysql-manager-0.56-0.el6.noarch (/mha4mysql-manager-0.56-0.el6.noarch)
需要:mha4mysql-node >= 0.54
錯誤:軟件包:mha4mysql-manager-0.56-0.el6.noarch (/mha4mysql-manager-0.56-0.el6.noarch)
需要:perl(MHA::NodeUtil)
錯誤:軟件包:mha4mysql-manager-0.56-0.el6.noarch (/mha4mysql-manager-0.56-0.el6.noarch)
需要:perl(MHA::BinlogManager)
錯誤:軟件包:mha4mysql-manager-0.56-0.el6.noarch (/mha4mysql-manager-0.56-0.el6.noarch)
需要:perl(MHA::NodeConst)
錯誤:軟件包:mha4mysql-manager-0.56-0.el6.noarch (/mha4mysql-manager-0.56-0.el6.noarch)
需要:perl(MHA::SlaveUtil)
您可以嘗試添加 --skip-broken 選項來解決該問題
您可以嘗試執(zhí)行:rpm -Va --nofiles --nodigest
[root@10-31-1-119 src]#
監(jiān)控節(jié)點需要先安裝node再安裝manager
[root@10-31-1-119 src]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:mha4mysql-node-0.56-0.el6 ################################# [100%]
[root@10-31-1-119 src]#
[root@10-31-1-119 src]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:mha4mysql-manager-0.56-0.el6 ################################# [100%]
[root@10-31-1-119 src]#
安裝完成后,在/usr/bin/目錄下有如下MHA相關(guān)文件:
masterha_check_repl
masterha_check_ssh
masterha_check_status
masterha_conf_host
masterha_manager
masterha_master_monitor
masterha_master_switch
masterha_secondary_check
masterha_stop
apply_diff_relay_logs
filter_mysqlbinlog
purge_relay_logs
save_binary_logs
2.7 配置MHA
在hdp1上用root用戶執(zhí)行下面(1)见坑、(2)嚷掠、(3)的操作。
(1)建立配置文件目錄
mkdir -p /etc/masterha
(2)創(chuàng)建配置文件/etc/masterha/app1.cnf荞驴,內(nèi)容如下:
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1.log
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/bin/master_ip_failover
master_ip_online_change_script=/usr/bin/master_ip_online_change
password=abc123
ping_interval=1
remote_workdir=/tmp
repl_password=abc123
repl_user=repl
secondary_check_script=/usr/bin/masterha_secondary_check -s hp2 -s hp3 --user=root --master_host=hp2 --master_ip=10.31.1.120 --master_port=3306
shutdown_script=""
ssh_user=root
user=root
[server1]
hostname=10.31.1.120
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=10.31.1.121
port=3306
[server3]
hostname=10.31.1.122
port=3306
server default段是manager的一些基本配置參數(shù)不皆,server1、server2熊楼、server3分別對應(yīng)復(fù)制中的master霹娄、第一個slave、第二個slave鲫骗。該文件的語法要求嚴(yán)格犬耻,變量值后不要有多余的空格。主要配置項說明如下执泰。
manager_log:設(shè)置manager的日志文件香追。
manager_workdir:設(shè)置manager的工作目錄。
master_binlog_dir:設(shè)置master保存binlog的位置坦胶,以便MHA可以找到master的日志透典,這里的也就是mysql的數(shù)據(jù)目錄晴楔。
master_ip_failover_script:設(shè)置自動failover時候的切換腳本。
master_ip_online_change_script:設(shè)置手動切換時候的切換腳本峭咒。
password:設(shè)置mysql中root用戶的密碼怎静。
ping_interval:設(shè)置監(jiān)控主庫,發(fā)送ping包的時間間隔卒密,默認(rèn)是3秒忌锯,嘗試三次沒有回應(yīng)的時候自動進(jìn)行railover。
remote_workdir:設(shè)置遠(yuǎn)端mysql在發(fā)生切換時binlog的保存位置漩氨。
repl_password:設(shè)置復(fù)制用戶的密碼西壮。
repl_user:設(shè)置復(fù)制環(huán)境中的復(fù)制用戶名
secondary_check_script:一旦MHA到hdp4的監(jiān)控之間出現(xiàn)問題,MHA Manager將會嘗試從hdp3登錄到hdp4叫惊。
shutdown_script:設(shè)置故障發(fā)生后關(guān)閉故障主機(jī)腳本款青。該腳本的主要作用是關(guān)閉主機(jī)放在發(fā)生腦裂,這里沒有使用霍狰。
ssh_user:設(shè)置ssh的登錄用戶名抡草。
user:設(shè)置監(jiān)控用戶為root。
candidate_master:設(shè)置為候選master蔗坯。設(shè)置該參數(shù)以后康震,發(fā)生主從切換以后將會將此從庫提升為主庫,即使這個主庫不是集群中事件最新的slave宾濒。
check_repl_delay:默認(rèn)情況下如果一個slave落后master 100M的relay logs的話腿短,MHA將不會選擇該slave作為一個新的master,因為對于這個slave的恢復(fù)需要花費很長時間绘梦,通過設(shè)置check_repl_delay=0答姥,MHA觸發(fā)切換在選擇一個新的master的時候?qū)雎詮?fù)制延時,這個參數(shù)對于設(shè)置了candidate_master=1的主機(jī)非常有用谚咬,因為這個候選主在切換的過程中一定是新的master鹦付。
(3)建立軟連接
-- 這一步如果 mysqlbinlog和mysql已經(jīng)可以直接使用就沒問題
ln -s /home/mysql/mysql-5.6.14/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /home/mysql/mysql-5.6.14/bin/mysql /usr/bin/mysql
(4)設(shè)置復(fù)制中Slave的relay_log_purge參數(shù)
在hdp3和hdp4上用mysql用戶執(zhí)行:
mysql -uroot -pabc123 -e "set global relay_log_purge=0"
注意,MHA在發(fā)生切換的過程中择卦,從庫的恢復(fù)過程中依賴于relay log的相關(guān)信息敲长,所以這里要將relay log的自動清除設(shè)置為OFF,采用手動清除relay log的方式秉继。默認(rèn)情況下祈噪,從服務(wù)器上的中繼日志會在SQL線程執(zhí)行完畢后被自動刪除。但是在MHA環(huán)境中尚辑,這些中繼日志在恢復(fù)其他從服務(wù)器時可能會被用到辑鲤,因此需要禁用中繼日志的自動刪除功能。定期清除中繼日志需要考慮到復(fù)制延時的問題杠茬。在ext3的文件系統(tǒng)下月褥,刪除大的文件需要一定的時間弛随,會導(dǎo)致嚴(yán)重的復(fù)制延時。為了避免復(fù)制延時宁赤,需要暫時為中繼日志創(chuàng)建硬鏈接舀透,因為在linux系統(tǒng)中通過硬鏈接刪除大文件速度會很快。(在mysql數(shù)據(jù)庫中决左,刪除大表時愕够,通常也采用建立硬鏈接的方式)
2.8 創(chuàng)建相關(guān)腳本
2.8.1 創(chuàng)建定期清理relay腳本
在hdp3、hdp4兩臺slave上建立/root/purge_relay_log.sh文件佛猛,內(nèi)容如下:
#!/bin/bash
. /home/mysql/.bashrc
user=root
passwd=abc123
port=3306
log_dir='/data'
work_dir='/data'
purge='/usr/bin/purge_relay_logs'
if [ ! -d $log_dir ]
then
mkdir $log_dir -p
fi
$purge --user=$user --password=$passwd --disable_relay_log_purge --port=$port --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1
purge_relay_logs的參數(shù)說明:
user mysql:MySQL用戶名惑芭。
password mysql:MySQL用戶密碼。
port:MySQL端口號继找。
workdir:指定創(chuàng)建relay log的硬鏈接的位置遂跟,默認(rèn)是/var/tmp。由于系統(tǒng)不同分區(qū)創(chuàng)建硬鏈接文件會失敗码荔,故需要執(zhí)行硬鏈接具體位置漩勤,成功執(zhí)行腳本后感挥,硬鏈接的中繼日志文件被刪除缩搅。
disable_relay_log_purge:默認(rèn)情況下,如果relay_log_purge=1触幼,腳本會什么都不清理硼瓣,自動退出。通過設(shè)定這個參數(shù)置谦,當(dāng)relay_log_purge=1的情況下會將relay_log_purge設(shè)置為0堂鲤。清理relay log之后,最后將參數(shù)設(shè)置為OFF媒峡。
改模式為可執(zhí)行:
mkdir -p /data
chmod 755 purge_relay_log.sh
添加到crontab中:
0 4 * * * /bin/bash /root/purge_relay_log.sh
2.8.2 創(chuàng)建自動failover腳本
在hdp1上創(chuàng)建/usr/bin/master_ip_failover文件瘟栖,內(nèi)容如下:
#!/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.31.1.241';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens192:$key $vip/24";
my $ssh_stop_vip = "/sbin/ifconfig ens192:$key down";
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";
}
注意腳本中的vip部分
chmod u+x /usr/bin/master_ip_failover
2.8.3 創(chuàng)建手動failover腳本
在hdp1上創(chuàng)建/usr/bin/master_ip_online_change文件,內(nèi)容如下:
#!/usr/bin/env perl
## Note: This is a sample script and is notcomplete. Modify the script based on your environment.
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
# use Time::HiRes qw( sleep gettimeofdaytv_interval );
use Time::HiRes qw(sleep gettimeofday tv_interval);
use Data::Dumper;
my $_tstart;
my $_running_interval = 0.1;
my (
$command, $orig_master_host, $orig_master_ip,
$orig_master_port, $orig_master_user,
$new_master_host, $new_master_ip, $new_master_port,
$new_master_user,
);
my $vip = '10.31.1.241'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens32:$key $vip/24";
my $ssh_stop_vip = "/sbin/ifconfig ens160:$key down";
my $ssh_user = "root";
my $new_master_password = "123456";
my $orig_master_password = "123456";
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,
'orig_master_user=s' =>\$orig_master_user,
#'orig_master_password=s' => \$orig_master_password,
'new_master_host=s' =>\$new_master_host,
'new_master_ip=s' =>\$new_master_ip,
'new_master_port=i' =>\$new_master_port,
'new_master_user=s' =>\$new_master_user,
#'new_master_password=s' =>\$new_master_password,
);
exit &main();
sub current_time_us {
my ($sec, $microsec ) = gettimeofday();
my$curdate = localtime($sec);
return $curdate . " " . sprintf( "%06d", $microsec);
}
sub sleep_until {
my$elapsed = tv_interval($_tstart);
if ($_running_interval > $elapsed ) {
sleep( $_running_interval - $elapsed );
}
}
sub get_threads_util {
my$dbh = shift;
my$my_connection_id = shift;
my$running_time_threshold = shift;
my$type = shift;
$running_time_threshold = 0 unless ($running_time_threshold);
$type = 0 unless($type);
my@threads;
my$sth = $dbh->prepare("SHOW PROCESSLIST");
$sth->execute();
while ( my $ref = $sth->fetchrow_hashref() ) {
my$id = $ref->{Id};
my$user = $ref->{User};
my$host = $ref->{Host};
my$command = $ref->{Command};
my$state = $ref->{State};
my$query_time = $ref->{Time};
my$info = $ref->{Info};
$info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
next if ( $my_connection_id == $id );
next if ( defined($query_time) && $query_time <$running_time_threshold );
next if ( defined($command) && $command eq "Binlog Dump" );
next if ( defined($user) && $user eq "system user" );
next
if ( defined($command)
&& $command eq "Sleep"
&& defined($query_time)
&& $query_time >= 1 );
if( $type >= 1 ) {
next if ( defined($command) && $command eq "Sleep" );
nextif ( defined($command) && $command eq "Connect" );
}
if( $type >= 2 ) {
next if ( defined($info) && $info =~ m/^select/i );
next if ( defined($info) && $info =~ m/^show/i );
}
push @threads, $ref;
}
return @threads;
}
sub main {
if ($command eq "stop" ) {
##Gracefully killing connections on the current master
#1. Set read_only= 1 on the new master
#2. DROP USER so that no app user can establish new connections
#3. Set read_only= 1 on the current master
#4. Kill current queries
#* Any database access failure will result in script die.
my$exit_code = 1;
eval {
## Setting read_only=1 on the new master (to avoid accident)
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error(die_on_error)_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
print current_time_us() . " Set read_only on the new master..";
$new_master_handler->enable_read_only();
if ( $new_master_handler->is_read_only() ) {
print "ok.\n";
}
else {
die "Failed!\n";
}
$new_master_handler->disconnect();
# Connecting to the orig master, die if any database error happens
my $orig_master_handler = new MHA::DBHelper();
$orig_master_handler->connect( $orig_master_ip, $orig_master_port,
$orig_master_user, $orig_master_password, 1 );
## Drop application user so that nobodycan connect. Disabling per-session binlog beforehand
#$orig_master_handler->disable_log_bin_local();
#print current_time_us() . " Drpping app user on the origmaster..\n";
#FIXME_xxx_drop_app_user($orig_master_handler);
## Waiting for N * 100 milliseconds so that current connections can exit
my $time_until_read_only = 15;
$_tstart = [gettimeofday];
my @threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
while ( $time_until_read_only > 0 && $#threads >= 0 ) {
if ( $time_until_read_only % 5 == 0 ) {
printf "%s Waiting all running %d threads aredisconnected.. (max %d milliseconds)\n",
current_time_us(), $#threads + 1, $time_until_read_only * 100;
if ( $#threads < 5 ) {
print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump ."\n"
foreach (@threads);
}
}
sleep_until();
$_tstart = [gettimeofday];
$time_until_read_only--;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
}
## Setting read_only=1 on the current master so that nobody(exceptSUPER) can write
print current_time_us() . " Set read_only=1 on the orig master..";
$orig_master_handler->enable_read_only();
if ( $orig_master_handler->is_read_only() ) {
print "ok.\n";
}
else {
die "Failed!\n";
}
## Waiting for M * 100 milliseconds so that current update queries cancomplete
my $time_until_kill_threads = 5;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
if ( $time_until_kill_threads % 5 == 0 ) {
printf "%s Waiting all running %d queries aredisconnected.. (max %d milliseconds)\n",
current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
if ( $#threads < 5 ) {
print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump ."\n"
foreach (@threads);
}
}
sleep_until();
$_tstart = [gettimeofday];
$time_until_kill_threads--;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
}
print "Disabling the VIPon old master: $orig_master_host \n";
&stop_vip();
## Terminating all threads
print current_time_us() . " Killing all applicationthreads..\n";
$orig_master_handler->kill_threads(@threads) if ( $#threads >= 0);
print current_time_us() . " done.\n";
#$orig_master_handler->enable_log_bin_local();
$orig_master_handler->disconnect();
## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
$exit_code = 0;
};
if($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
##Activating master ip on the new master
#1. Create app user with write privileges
#2. Moving backup script if needed
#3. Register new master's ip to the catalog database
# We don't return error even thoughactivating updatable accounts/ip failed so that we don't interrupt slaves'recovery.
# If exit code is 0 or 10, MHA does notabort
my$exit_code = 10;
eval{
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
## Set read_only=0 on the new master
#$new_master_handler->disable_log_bin_local();
print current_time_us() . " Set read_only=0 on the newmaster.\n";
$new_master_handler->disable_read_only();
## Creating an app user on the new master
#print current_time_us() . " Creating app user on the newmaster..\n";
#FIXME_xxx_create_app_user($new_master_handler);
#$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
print "Enabling the VIP -$vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
#do nothing
exit 0;
}
else{
&usage();
exit 1;
}
}
# A simple system call that enable the VIPon the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIPon the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_online_change --command=start|stop|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";
die;
}
授權(quán)
chmod u+x /usr/bin/master_ip_online_change
三.檢查MHA配置
3.1 檢查SSH配置
在hdp1上用root用戶操作谅阿。
[root@10-31-1-119 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
Mon Aug 24 17:47:04 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug 24 17:47:04 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug 24 17:47:04 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug 24 17:47:04 2020 - [info] Starting SSH connection tests..
Mon Aug 24 17:47:06 2020 - [debug]
Mon Aug 24 17:47:04 2020 - [debug] Connecting via SSH from root@10.31.1.120(10.31.1.120:22) to root@10.31.1.121(10.31.1.121:22)..
Mon Aug 24 17:47:05 2020 - [debug] ok.
Mon Aug 24 17:47:05 2020 - [debug] Connecting via SSH from root@10.31.1.120(10.31.1.120:22) to root@10.31.1.122(10.31.1.122:22)..
Mon Aug 24 17:47:06 2020 - [debug] ok.
Mon Aug 24 17:47:07 2020 - [debug]
Mon Aug 24 17:47:05 2020 - [debug] Connecting via SSH from root@10.31.1.122(10.31.1.122:22) to root@10.31.1.120(10.31.1.120:22)..
Mon Aug 24 17:47:06 2020 - [debug] ok.
Mon Aug 24 17:47:06 2020 - [debug] Connecting via SSH from root@10.31.1.122(10.31.1.122:22) to root@10.31.1.121(10.31.1.121:22)..
Mon Aug 24 17:47:07 2020 - [debug] ok.
Mon Aug 24 17:47:07 2020 - [debug]
Mon Aug 24 17:47:05 2020 - [debug] Connecting via SSH from root@10.31.1.121(10.31.1.121:22) to root@10.31.1.120(10.31.1.120:22)..
Mon Aug 24 17:47:06 2020 - [debug] ok.
Mon Aug 24 17:47:06 2020 - [debug] Connecting via SSH from root@10.31.1.121(10.31.1.121:22) to root@10.31.1.122(10.31.1.122:22)..
Mon Aug 24 17:47:06 2020 - [debug] ok.
Mon Aug 24 17:47:07 2020 - [info] All SSH connection tests passed successfully.
3.2 檢查整個復(fù)制環(huán)境狀況
hp2上授權(quán)
create user 'root'@'10.31.1.%' identified by 'abc123';
grant all privileges on *.* to 'root'@'10.31.1.%';
flush privileges;
在hdp1上用root用戶操作半哟。
[root@10-31-1-119 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug 24 17:57:22 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug 24 17:57:22 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug 24 17:57:22 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug 24 17:57:22 2020 - [info] MHA::MasterMonitor version 0.56.
Mon Aug 24 17:57:23 2020 - [info] GTID failover mode = 0
Mon Aug 24 17:57:23 2020 - [info] Dead Servers:
Mon Aug 24 17:57:23 2020 - [info] Alive Servers:
Mon Aug 24 17:57:23 2020 - [info] 10.31.1.120(10.31.1.120:3306)
Mon Aug 24 17:57:23 2020 - [info] 10.31.1.121(10.31.1.121:3306)
Mon Aug 24 17:57:23 2020 - [info] 10.31.1.122(10.31.1.122:3306)
Mon Aug 24 17:57:23 2020 - [info] Alive Slaves:
Mon Aug 24 17:57:23 2020 - [info] 10.31.1.121(10.31.1.121:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Mon Aug 24 17:57:23 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Mon Aug 24 17:57:23 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Mon Aug 24 17:57:23 2020 - [info] 10.31.1.122(10.31.1.122:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Mon Aug 24 17:57:23 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Mon Aug 24 17:57:23 2020 - [info] Current Alive Master: 10.31.1.120(10.31.1.120:3306)
Mon Aug 24 17:57:23 2020 - [info] Checking slave configurations..
Mon Aug 24 17:57:23 2020 - [info] read_only=1 is not set on slave 10.31.1.121(10.31.1.121:3306).
Mon Aug 24 17:57:23 2020 - [info] read_only=1 is not set on slave 10.31.1.122(10.31.1.122:3306).
Mon Aug 24 17:57:23 2020 - [info] Checking replication filtering settings..
Mon Aug 24 17:57:23 2020 - [info] binlog_do_db= , binlog_ignore_db=
Mon Aug 24 17:57:23 2020 - [info] Replication filtering check ok.
Mon Aug 24 17:57:23 2020 - [info] GTID (with auto-pos) is not supported
Mon Aug 24 17:57:23 2020 - [info] Starting SSH connection tests..
Mon Aug 24 17:57:26 2020 - [info] All SSH connection tests passed successfully.
Mon Aug 24 17:57:26 2020 - [info] Checking MHA Node version..
Mon Aug 24 17:57:27 2020 - [info] Version check ok.
Mon Aug 24 17:57:27 2020 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug 24 17:57:27 2020 - [info] HealthCheck: SSH to 10.31.1.120 is reachable.
Mon Aug 24 17:57:27 2020 - [info] Master MHA Node version is 0.56.
Mon Aug 24 17:57:27 2020 - [info] Checking recovery script configurations on 10.31.1.120(10.31.1.120:3306)..
Mon Aug 24 17:57:27 2020 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=10-31-1-120-bin.000001
Mon Aug 24 17:57:27 2020 - [info] Connecting to root@10.31.1.120(10.31.1.120:22)..
Creating /tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to 10-31-1-120-bin.000001
Mon Aug 24 17:57:28 2020 - [info] Binlog setting check done.
Mon Aug 24 17:57:28 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Mon Aug 24 17:57:28 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=10.31.1.121 --slave_ip=10.31.1.121 --slave_port=3306 --workdir=/tmp --target_version=5.7.31-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx
Mon Aug 24 17:57:28 2020 - [info] Connecting to root@10.31.1.121(10.31.1.121:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to 10-31-1-121-relay-bin.000005
Temporary relay log file is /var/lib/mysql/10-31-1-121-relay-bin.000005
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Mon Aug 24 17:57:28 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=10.31.1.122 --slave_ip=10.31.1.122 --slave_port=3306 --workdir=/tmp --target_version=5.7.31-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx
Mon Aug 24 17:57:28 2020 - [info] Connecting to root@10.31.1.122(10.31.1.122:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to 10-31-1-122-relay-bin.000005
Temporary relay log file is /var/lib/mysql/10-31-1-122-relay-bin.000005
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Mon Aug 24 17:57:29 2020 - [info] Slaves settings check done.
Mon Aug 24 17:57:29 2020 - [info]
10.31.1.120(10.31.1.120:3306) (current master)
+--10.31.1.121(10.31.1.121:3306)
+--10.31.1.122(10.31.1.122:3306)
Mon Aug 24 17:57:29 2020 - [info] Checking replication health on 10.31.1.121..
Mon Aug 24 17:57:29 2020 - [info] ok.
Mon Aug 24 17:57:29 2020 - [info] Checking replication health on 10.31.1.122..
Mon Aug 24 17:57:29 2020 - [info] ok.
Mon Aug 24 17:57:29 2020 - [info] Checking master_ip_failover_script status:
Mon Aug 24 17:57:29 2020 - [info] /usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=10.31.1.120 --orig_master_ip=10.31.1.120 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig ens192:1 down==/sbin/ifconfig ens192:1 10.31.1.241/24===
Checking the Status of the script.. OK
Mon Aug 24 17:57:29 2020 - [info] OK.
Mon Aug 24 17:57:29 2020 - [warning] shutdown_script is not defined.
Mon Aug 24 17:57:29 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
[root@10-31-1-119 ~]#
3.3 檢查MHA Manager的狀態(tài)
在hdp1上用root用戶操作。
[root@10-31-1-119 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
[root@10-31-1-119 ~]#
顯示"NOT_RUNNING"签餐,這代表MHA監(jiān)控沒有開啟寓涨。執(zhí)行下面的命令后臺啟動MHA。
mkdir -p /var/log/masterha/app1/
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 &
啟動參數(shù)說明:
remove_dead_master_conf:該參數(shù)代表當(dāng)發(fā)生主從切換后氯檐,老的主庫的ip將會從配置文件中移除戒良。
manger_log:日志存放位置。
ignore_last_failover:在缺省情況下冠摄,如果MHA檢測到連續(xù)發(fā)生宕機(jī)糯崎,且兩次宕機(jī)間隔不足8小時的話几缭,則不會進(jìn)行Failover,之所以這樣限制是為了避免ping-pong效應(yīng)拇颅。該參數(shù)代表忽略上次MHA觸發(fā)切換產(chǎn)生的文件奏司,默認(rèn)情況下,MHA發(fā)生切換后會在日志目錄樟插,也就是上面設(shè)置的/data產(chǎn)生app1.failover.complete文件韵洋,下次再次切換的時候如果發(fā)現(xiàn)該目錄下存在該文件將不允許觸發(fā)切換,除非在第一次切換后收到刪除該文件黄锤。為了方便搪缨,這里設(shè)置為--ignore_last_failover。
再次檢查MHA Manager的狀態(tài):
[root@10-31-1-119 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 monitoring program is now on initialization phase(10:INITIALIZING_MONITOR). Wait for a while and try checking again.
[root@10-31-1-119 ~]#
3.4 查看啟動日志
在hdp1上用root用戶操作鸵熟。
[root@10-31-1-119 ~]# tail -n20 /var/log/masterha/app1/manager.log
Cleaning up test file(s).. done.
Mon Aug 24 18:06:00 2020 - [info] Slaves settings check done.
Mon Aug 24 18:06:00 2020 - [info]
10.31.1.120(10.31.1.120:3306) (current master)
+--10.31.1.121(10.31.1.121:3306)
+--10.31.1.122(10.31.1.122:3306)
Mon Aug 24 18:06:00 2020 - [info] Checking master_ip_failover_script status:
Mon Aug 24 18:06:00 2020 - [info] /usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=10.31.1.120 --orig_master_ip=10.31.1.120 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig eth1:1 down==/sbin/ifconfig eth1:1 10.31.1.241/24===
Checking the Status of the script.. OK
Mon Aug 24 18:06:00 2020 - [info] OK.
Mon Aug 24 18:06:00 2020 - [warning] shutdown_script is not defined.
Mon Aug 24 18:06:00 2020 - [info] Set master ping interval 1 seconds.
Mon Aug 24 18:06:00 2020 - [info] Set secondary check script: /usr/bin/masterha_secondary_check -s hdp2 -s hdp3 --user=root --master_host=hdp2 --master_ip=10.31.1.120 --master_port=3306
Mon Aug 24 18:06:00 2020 - [info] Starting ping health check on 10.31.1.120(10.31.1.120:3306)..
Mon Aug 24 18:06:00 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
四.功能測試
4.1 初始綁定VIP
在hdp2 10.31.1.112(master)上用root用戶執(zhí)行:
/sbin/ifconfig ens192:1 10.31.1.241/24
執(zhí)行記錄
[root@10-31-1-120 src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:54:f4:e5 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.120/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 fe80::b3f8:a2f7:5ba0:b68d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
[root@10-31-1-120 src]#
[root@10-31-1-120 src]#
[root@10-31-1-120 src]# /sbin/ifconfig ens192:1 10.31.1.241/24
[root@10-31-1-120 src]#
[root@10-31-1-120 src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:54:f4:e5 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.120/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet 10.31.1.241/24 brd 10.31.1.255 scope global secondary ens192:1
valid_lft forever preferred_lft forever
inet6 fe80::b3f8:a2f7:5ba0:b68d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
[root@10-31-1-120 src]#
也可以手工解除綁定
[root@10-31-1-120 keepalived-2.0.15]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:54:f4:e5 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.120/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet 10.31.1.241/24 brd 10.31.1.255 scope global secondary ens192:1
valid_lft forever preferred_lft forever
inet6 fe80::b3f8:a2f7:5ba0:b68d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
[root@10-31-1-120 keepalived-2.0.15]#
[root@10-31-1-120 keepalived-2.0.15]#
[root@10-31-1-120 keepalived-2.0.15]# ifconfig ens192:10.31.1.241/24 down
SIOCSIFFLAGS: 無法指定被請求的地址
[root@10-31-1-120 keepalived-2.0.15]#
[root@10-31-1-120 keepalived-2.0.15]#
[root@10-31-1-120 keepalived-2.0.15]# ifconfig ens192:1 down
[root@10-31-1-120 keepalived-2.0.15]#
[root@10-31-1-120 keepalived-2.0.15]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:54:f4:e5 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.120/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 fe80::b3f8:a2f7:5ba0:b68d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
[root@10-31-1-120 keepalived-2.0.15]#
4.2 測試自動切換
4.2.1 通過vip登陸mysql
[root@uattest ~]# mysql -uroot -p -h10.31.1.241
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.31-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>
4.2.2 停止主庫hp1的mysql服務(wù)器
service mysqld stop
ip addr
[root@10-31-1-120 src]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
[root@10-31-1-120 src]#
[root@10-31-1-120 src]#
[root@10-31-1-120 src]#
-- 可以看到 10.31.1.241的vip已經(jīng)不再hp1(master)上了
[root@10-31-1-120 src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:54:f4:e5 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.120/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 fe80::b3f8:a2f7:5ba0:b68d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
4.2.3 hp2和hp3查看
hp2
-- 可以看到 10.31.1.241這個vip飄到hp2上了
[root@10-31-1-121 src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:e1:fe:b4 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.121/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet 10.31.1.241/24 brd 10.255.255.255 scope global ens192:1
valid_lft forever preferred_lft forever
inet6 fe80::9470:f61e:6e0e:48e4/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::1aa1:d23e:e28a:fb62/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::f974:3f03:d1f0:1672/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
-- hp2已經(jīng)由從庫變?yōu)橹鲙炝?[root@10-31-1-121 src]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.31-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>
mysql> show slave status\G
Empty set (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: 10-31-1-121-bin.000001
Position: 507
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
hp3
hp3變?yōu)榱薶p2的從庫
[root@10-31-1-122 bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.7.31-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> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.31.1.121
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: 10-31-1-121-bin.000001
Read_Master_Log_Pos: 507
Relay_Log_File: 10-31-1-122-relay-bin.000002
Relay_Log_Pos: 679
Relay_Master_Log_File: 10-31-1-121-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: 507
Relay_Log_Space: 892
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: 102
Master_UUID: 5ad1f5aa-e2cb-11ea-ba89-000c29e1feb4
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 more updates
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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
4.3 還原環(huán)境
還原數(shù)據(jù)庫復(fù)制:
-- 在hdp2副编、hdp3、hdp4上重置master流强、slave
stop slave;
reset master;
reset slave all;
-- 在hdp3痹届、hdp4上重新指向hdp2為master
change master to
master_host='10.31.1.120',
master_port=3306,
master_user='repl',
master_password='abc123',
master_log_file='10-31-1-120-bin.000001',
master_log_pos=154;
start slave;
show slave status\G
還原VIP綁定:
# 在hdp3上用root用戶執(zhí)行
/sbin/ifconfig ens192:1 down
# 在hdp2上用root用戶執(zhí)行
/sbin/ifconfig ens192:1 10.31.1.241/24
還原配置文件:
編輯在hdp1上/etc/masterha/app1.cnf,將[server1]段添加回去打月。
啟動MHA Manage:
# 在hdp1上用root用戶執(zhí)行
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 &
至此環(huán)境還原完畢队腐。
4.4 測試手工切換
4.4.1 停止MHA Manage
在hp1用root用戶操作
masterha_stop --conf=/etc/masterha/app1.cnf
4.4.2 關(guān)閉master
在hdp2上用root用戶操作。
service mysqld stop
4.4.3 執(zhí)行手工切換
在hdp1上用root用戶操作奏篙。
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=10.31.1.120 --dead_master_port=3306 --new_master_host=10.31.1.121 --new_master_port=3306 --ignore_last_failover
執(zhí)行記錄
[root@10-31-1-119 ~]# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=10.31.1.120 --dead_master_port=3306 --new_master_host=10.31.1.121 --new_master_port=3306 --ignore_last_failover
--dead_master_ip=<dead_master_ip> is not set. Using 10.31.1.120.
Tue Aug 25 16:18:55 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 25 16:18:55 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Aug 25 16:18:55 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Aug 25 16:18:55 2020 - [info] MHA::MasterFailover version 0.56.
Tue Aug 25 16:18:55 2020 - [info] Starting master failover.
Tue Aug 25 16:18:55 2020 - [info]
Tue Aug 25 16:18:55 2020 - [info] * Phase 1: Configuration Check Phase..
Tue Aug 25 16:18:55 2020 - [info]
Tue Aug 25 16:18:56 2020 - [info] GTID failover mode = 0
Tue Aug 25 16:18:56 2020 - [info] Dead Servers:
Tue Aug 25 16:18:56 2020 - [info] 10.31.1.120(10.31.1.120:3306)
Tue Aug 25 16:18:56 2020 - [info] Checking master reachability via MySQL(double check)...
Tue Aug 25 16:18:56 2020 - [info] ok.
Tue Aug 25 16:18:56 2020 - [info] Alive Servers:
Tue Aug 25 16:18:56 2020 - [info] 10.31.1.121(10.31.1.121:3306)
Tue Aug 25 16:18:56 2020 - [info] 10.31.1.122(10.31.1.122:3306)
Tue Aug 25 16:18:56 2020 - [info] Alive Slaves:
Tue Aug 25 16:18:56 2020 - [info] 10.31.1.121(10.31.1.121:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Tue Aug 25 16:18:56 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Tue Aug 25 16:18:56 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Aug 25 16:18:56 2020 - [info] 10.31.1.122(10.31.1.122:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Tue Aug 25 16:18:56 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Master 10.31.1.120(10.31.1.120:3306) is dead. Proceed? (yes/NO): yes
Tue Aug 25 16:19:01 2020 - [info] Starting Non-GTID based failover.
Tue Aug 25 16:19:01 2020 - [info]
Tue Aug 25 16:19:01 2020 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Aug 25 16:19:01 2020 - [info]
Tue Aug 25 16:19:01 2020 - [info] * Phase 2: Dead Master Shutdown Phase..
Tue Aug 25 16:19:01 2020 - [info]
Tue Aug 25 16:19:01 2020 - [info] HealthCheck: SSH to 10.31.1.120 is reachable.
Tue Aug 25 16:19:01 2020 - [info] Forcing shutdown so that applications never connect to the current master..
Tue Aug 25 16:19:01 2020 - [info] Executing master IP deactivation script:
Tue Aug 25 16:19:01 2020 - [info] /usr/bin/master_ip_failover --orig_master_host=10.31.1.120 --orig_master_ip=10.31.1.120 --orig_master_port=3306 --command=stopssh --ssh_user=root
IN SCRIPT TEST====/sbin/ifconfig ens192:1 down==/sbin/ifconfig ens192:1 10.31.1.241/24===
Disabling the VIP on old master: 10.31.1.120
Tue Aug 25 16:19:02 2020 - [info] done.
Tue Aug 25 16:19:02 2020 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Tue Aug 25 16:19:02 2020 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Tue Aug 25 16:19:02 2020 - [info]
Tue Aug 25 16:19:02 2020 - [info] * Phase 3: Master Recovery Phase..
Tue Aug 25 16:19:02 2020 - [info]
Tue Aug 25 16:19:02 2020 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Tue Aug 25 16:19:02 2020 - [info]
Tue Aug 25 16:19:02 2020 - [info] The latest binary log file/position on all slaves is 10-31-1-120-bin.000001:154
Tue Aug 25 16:19:02 2020 - [info] Latest slaves (Slaves that received relay log files to the latest):
Tue Aug 25 16:19:02 2020 - [info] 10.31.1.121(10.31.1.121:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Tue Aug 25 16:19:02 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Tue Aug 25 16:19:02 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Aug 25 16:19:02 2020 - [info] 10.31.1.122(10.31.1.122:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Tue Aug 25 16:19:02 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Tue Aug 25 16:19:02 2020 - [info] The oldest binary log file/position on all slaves is 10-31-1-120-bin.000001:154
Tue Aug 25 16:19:02 2020 - [info] Oldest slaves:
Tue Aug 25 16:19:02 2020 - [info] 10.31.1.121(10.31.1.121:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Tue Aug 25 16:19:02 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Tue Aug 25 16:19:02 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Aug 25 16:19:02 2020 - [info] 10.31.1.122(10.31.1.122:3306) Version=5.7.31-log (oldest major version between slaves) log-bin:enabled
Tue Aug 25 16:19:02 2020 - [info] Replicating from 10.31.1.120(10.31.1.120:3306)
Tue Aug 25 16:19:02 2020 - [info]
Tue Aug 25 16:19:02 2020 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Tue Aug 25 16:19:02 2020 - [info]
Tue Aug 25 16:19:02 2020 - [info] Fetching dead master's binary logs..
Tue Aug 25 16:19:02 2020 - [info] Executing command on the dead master 10.31.1.120(10.31.1.120:3306): save_binary_logs --command=save --start_file=10-31-1-120-bin.000001 --start_pos=154 --binlog_dir=/var/lib/mysql --output_file=/tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56
Creating /tmp if not exists.. ok.
Concat binary/relay logs from 10-31-1-120-bin.000001 pos 154 to 10-31-1-120-bin.000001 EOF into /tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog ..
Binlog Checksum enabled
Dumping binlog format description event, from position 0 to 154.. ok.
Dumping effective binlog data from /var/lib/mysql/10-31-1-120-bin.000001 position 154 to tail(177).. ok.
Binlog Checksum enabled
Concat succeeded.
saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog 100% 177 140.4KB/s 00:00
Tue Aug 25 16:19:03 2020 - [info] scp from root@10.31.1.120:/tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog to local:/var/log/masterha/app1.log/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog succeeded.
Tue Aug 25 16:19:04 2020 - [info] HealthCheck: SSH to 10.31.1.121 is reachable.
Tue Aug 25 16:19:05 2020 - [info] HealthCheck: SSH to 10.31.1.122 is reachable.
Tue Aug 25 16:19:05 2020 - [info]
Tue Aug 25 16:19:05 2020 - [info] * Phase 3.3: Determining New Master Phase..
Tue Aug 25 16:19:05 2020 - [info]
Tue Aug 25 16:19:05 2020 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Tue Aug 25 16:19:05 2020 - [info] All slaves received relay logs to the same position. No need to resync each other.
Tue Aug 25 16:19:05 2020 - [info] 10.31.1.121 can be new master.
Tue Aug 25 16:19:05 2020 - [info] New master is 10.31.1.121(10.31.1.121:3306)
Tue Aug 25 16:19:05 2020 - [info] Starting master failover..
Tue Aug 25 16:19:05 2020 - [info]
From:
10.31.1.120(10.31.1.120:3306) (current master)
+--10.31.1.121(10.31.1.121:3306)
+--10.31.1.122(10.31.1.122:3306)
To:
10.31.1.121(10.31.1.121:3306) (new master)
+--10.31.1.122(10.31.1.122:3306)
Starting master switch from 10.31.1.120(10.31.1.120:3306) to 10.31.1.121(10.31.1.121:3306)? (yes/NO): yes
Tue Aug 25 16:19:09 2020 - [info] New master decided manually is 10.31.1.121(10.31.1.121:3306)
Tue Aug 25 16:19:09 2020 - [info]
Tue Aug 25 16:19:09 2020 - [info] * Phase 3.3: New Master Diff Log Generation Phase..
Tue Aug 25 16:19:09 2020 - [info]
Tue Aug 25 16:19:09 2020 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
Tue Aug 25 16:19:09 2020 - [info] Sending binlog..
saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog 100% 177 176.2KB/s 00:00
Tue Aug 25 16:19:10 2020 - [info] scp from local:/var/log/masterha/app1.log/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog to root@10.31.1.121:/tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog succeeded.
Tue Aug 25 16:19:10 2020 - [info]
Tue Aug 25 16:19:10 2020 - [info] * Phase 3.4: Master Log Apply Phase..
Tue Aug 25 16:19:10 2020 - [info]
Tue Aug 25 16:19:10 2020 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
Tue Aug 25 16:19:10 2020 - [info] Starting recovery on 10.31.1.121(10.31.1.121:3306)..
Tue Aug 25 16:19:10 2020 - [info] Generating diffs succeeded.
Tue Aug 25 16:19:10 2020 - [info] Waiting until all relay logs are applied.
Tue Aug 25 16:19:10 2020 - [info] done.
Tue Aug 25 16:19:10 2020 - [info] Getting slave status..
Tue Aug 25 16:19:10 2020 - [info] This slave(10.31.1.121)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(10-31-1-120-bin.000001:154). No need to recover from Exec_Master_Log_Pos.
Tue Aug 25 16:19:10 2020 - [info] Connecting to the target slave host 10.31.1.121, running recover script..
Tue Aug 25 16:19:10 2020 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user='root' --slave_host=10.31.1.121 --slave_ip=10.31.1.121 --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog --workdir=/tmp --target_version=5.7.31-log --timestamp=20200825161855 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56 --slave_pass=xxx
Tue Aug 25 16:19:10 2020 - [info]
MySQL client version is 5.7.31. Using --binary-mode.
Applying differential binary/relay log files /tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog on 10.31.1.121:3306. This may take long time...
Applying log files succeeded.
Tue Aug 25 16:19:10 2020 - [info] All relay logs were successfully applied.
Tue Aug 25 16:19:10 2020 - [info] Getting new master's binlog name and position..
Tue Aug 25 16:19:10 2020 - [info] 10-31-1-121-bin.000001:154
Tue Aug 25 16:19:10 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.31.1.121', MASTER_PORT=3306, MASTER_LOG_FILE='10-31-1-121-bin.000001', MASTER_LOG_POS=154, MASTER_USER='repl', MASTER_PASSWORD='xxx';
Tue Aug 25 16:19:10 2020 - [info] Executing master IP activate script:
Tue Aug 25 16:19:10 2020 - [info] /usr/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=10.31.1.120 --orig_master_ip=10.31.1.120 --orig_master_port=3306 --new_master_host=10.31.1.121 --new_master_ip=10.31.1.121 --new_master_port=3306 --new_master_user='root' --new_master_password='abc123'
Unknown option: new_master_user
Unknown option: new_master_password
IN SCRIPT TEST====/sbin/ifconfig ens192:1 down==/sbin/ifconfig ens192:1 10.31.1.241/24===
Enabling the VIP - 10.31.1.241 on the new master - 10.31.1.121
Tue Aug 25 16:19:10 2020 - [info] OK.
Tue Aug 25 16:19:10 2020 - [info] ** Finished master recovery successfully.
Tue Aug 25 16:19:10 2020 - [info] * Phase 3: Master Recovery Phase completed.
Tue Aug 25 16:19:10 2020 - [info]
Tue Aug 25 16:19:10 2020 - [info] * Phase 4: Slaves Recovery Phase..
Tue Aug 25 16:19:10 2020 - [info]
Tue Aug 25 16:19:10 2020 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
Tue Aug 25 16:19:10 2020 - [info]
Tue Aug 25 16:19:10 2020 - [info] -- Slave diff file generation on host 10.31.1.122(10.31.1.122:3306) started, pid: 14489. Check tmp log /var/log/masterha/app1.log/10.31.1.122_3306_20200825161855.log if it takes time..
Tue Aug 25 16:19:11 2020 - [info]
Tue Aug 25 16:19:11 2020 - [info] Log messages from 10.31.1.122 ...
Tue Aug 25 16:19:11 2020 - [info]
Tue Aug 25 16:19:10 2020 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
Tue Aug 25 16:19:11 2020 - [info] End of log messages from 10.31.1.122.
Tue Aug 25 16:19:11 2020 - [info] -- 10.31.1.122(10.31.1.122:3306) has the latest relay log events.
Tue Aug 25 16:19:11 2020 - [info] Generating relay diff files from the latest slave succeeded.
Tue Aug 25 16:19:11 2020 - [info]
Tue Aug 25 16:19:11 2020 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
Tue Aug 25 16:19:11 2020 - [info]
Tue Aug 25 16:19:11 2020 - [info] -- Slave recovery on host 10.31.1.122(10.31.1.122:3306) started, pid: 14491. Check tmp log /var/log/masterha/app1.log/10.31.1.122_3306_20200825161855.log if it takes time..
saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog 100% 177 170.8KB/s 00:00
Tue Aug 25 16:19:13 2020 - [info]
Tue Aug 25 16:19:13 2020 - [info] Log messages from 10.31.1.122 ...
Tue Aug 25 16:19:13 2020 - [info]
Tue Aug 25 16:19:11 2020 - [info] Sending binlog..
Tue Aug 25 16:19:12 2020 - [info] scp from local:/var/log/masterha/app1.log/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog to root@10.31.1.122:/tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog succeeded.
Tue Aug 25 16:19:12 2020 - [info] Starting recovery on 10.31.1.122(10.31.1.122:3306)..
Tue Aug 25 16:19:12 2020 - [info] Generating diffs succeeded.
Tue Aug 25 16:19:12 2020 - [info] Waiting until all relay logs are applied.
Tue Aug 25 16:19:12 2020 - [info] done.
Tue Aug 25 16:19:12 2020 - [info] Getting slave status..
Tue Aug 25 16:19:12 2020 - [info] This slave(10.31.1.122)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(10-31-1-120-bin.000001:154). No need to recover from Exec_Master_Log_Pos.
Tue Aug 25 16:19:12 2020 - [info] Connecting to the target slave host 10.31.1.122, running recover script..
Tue Aug 25 16:19:12 2020 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user='root' --slave_host=10.31.1.122 --slave_ip=10.31.1.122 --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog --workdir=/tmp --target_version=5.7.31-log --timestamp=20200825161855 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56 --slave_pass=xxx
Tue Aug 25 16:19:13 2020 - [info]
MySQL client version is 5.7.31. Using --binary-mode.
Applying differential binary/relay log files /tmp/saved_master_binlog_from_10.31.1.120_3306_20200825161855.binlog on 10.31.1.122:3306. This may take long time...
Applying log files succeeded.
Tue Aug 25 16:19:13 2020 - [info] All relay logs were successfully applied.
Tue Aug 25 16:19:13 2020 - [info] Resetting slave 10.31.1.122(10.31.1.122:3306) and starting replication from the new master 10.31.1.121(10.31.1.121:3306)..
Tue Aug 25 16:19:13 2020 - [info] Executed CHANGE MASTER.
Tue Aug 25 16:19:13 2020 - [info] Slave started.
Tue Aug 25 16:19:13 2020 - [info] End of log messages from 10.31.1.122.
Tue Aug 25 16:19:13 2020 - [info] -- Slave recovery on host 10.31.1.122(10.31.1.122:3306) succeeded.
Tue Aug 25 16:19:13 2020 - [info] All new slave servers recovered successfully.
Tue Aug 25 16:19:13 2020 - [info]
Tue Aug 25 16:19:13 2020 - [info] * Phase 5: New master cleanup phase..
Tue Aug 25 16:19:13 2020 - [info]
Tue Aug 25 16:19:13 2020 - [info] Resetting slave info on the new master..
Tue Aug 25 16:19:13 2020 - [info] 10.31.1.121: Resetting slave info succeeded.
Tue Aug 25 16:19:13 2020 - [info] Master failover to 10.31.1.121(10.31.1.121:3306) completed successfully.
Tue Aug 25 16:19:13 2020 - [info]
----- Failover Report -----
app1: MySQL Master failover 10.31.1.120(10.31.1.120:3306) to 10.31.1.121(10.31.1.121:3306) succeeded
Master 10.31.1.120(10.31.1.120:3306) is down!
Check MHA Manager logs at 10-31-1-119 for details.
Started manual(interactive) failover.
Invalidated master IP address on 10.31.1.120(10.31.1.120:3306)
The latest slave 10.31.1.121(10.31.1.121:3306) has all relay logs for recovery.
Selected 10.31.1.121(10.31.1.121:3306) as a new master.
10.31.1.121(10.31.1.121:3306): OK: Applying all logs succeeded.
10.31.1.121(10.31.1.121:3306): OK: Activated master IP address.
10.31.1.122(10.31.1.122:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.31.1.122(10.31.1.122:3306): OK: Applying all logs succeeded. Slave started, replicating from 10.31.1.121(10.31.1.121:3306)
10.31.1.121(10.31.1.121:3306): Resetting slave info succeeded.
Master failover to 10.31.1.121(10.31.1.121:3306) completed successfully.
[root@10-31-1-119 ~]#
4.4.4 驗證VIP漂移到10.31.1.121
[root@10-31-1-121 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:e1:fe:b4 brd ff:ff:ff:ff:ff:ff
inet 10.31.1.121/24 brd 10.31.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet 10.31.1.241/24 brd 10.31.1.255 scope global secondary ens192:1
valid_lft forever preferred_lft forever
inet6 fe80::9470:f61e:6e0e:48e4/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::1aa1:d23e:e28a:fb62/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::f974:3f03:d1f0:1672/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:c4:b9:a5 brd ff:ff:ff:ff:ff:ff
4.4.5 驗證復(fù)制關(guān)系
[root@10-31-1-122 bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 81
Server version: 5.7.31-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> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.31.1.121
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: 10-31-1-121-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: 10-31-1-122-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: 10-31-1-121-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: 154
Relay_Log_Space: 539
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: 102
Master_UUID: 5ad1f5aa-e2cb-11ea-ba89-000c29e1feb4
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 more updates
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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql>
4.4.6 驗證客戶端VIP訪問
[root@uattest ~]# mysql -uroot -p -h10.31.1.241
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 99
Server version: 5.7.31-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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql>
4.5 修復(fù)宕機(jī)的Master
通常情況下自動切換以后柴淘,原master可能已經(jīng)廢棄掉,待原master主機(jī)修復(fù)后秘通,如果數(shù)據(jù)完整的情況下为严,可能想把原來master重新作為新主庫的slave。這時我們可以借助當(dāng)時自動切換時刻的MHA日志來完成對原master的修復(fù)肺稀。下面是提取相關(guān)日志的命令:
grep -i "All other slaves should start" /var/log/masterha/app1/manager.log
可以看到類似下面的信息:
All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.31.1.120', MASTER_PORT=3306, MASTER_LOG_FILE='10-31-1-121-bin.000005', MASTER_LOG_POS=120, MASTER_USER='repl', MASTER_PASSWORD='abc123';
意思是說第股,如果Master主機(jī)修復(fù)好了,可以在修復(fù)好后的Master執(zhí)行CHANGE MASTER操作话原,作為新的slave庫夕吻。
參考文獻(xiàn):
1.https://blog.csdn.net/wzy0623/article/details/81304654
2.https://blog.csdn.net/leshami/article/details/43229733
3.https://code.google.com/archive/p/mysql-master-ha/