企業(yè)信息管理系統(tǒng)er圖青島網(wǎng)絡(luò)優(yōu)化代理
MHA概述
-
一套優(yōu)秀的MySQL高可用環(huán)境下故障切換和主從復(fù)制的軟件
-
MHA的出現(xiàn)就是解決MySQL 單點(diǎn)的問(wèn)題
-
MySQL故障過(guò)程中,MHA能做到0-30秒內(nèi)自動(dòng)完成故障切換
-
MHA能在故障切換的過(guò)程中最大程度上保證數(shù)據(jù)的一致性以達(dá)到真正意義上的高可用
MHA的組成(核心部分)
-
MHA Manager :管理節(jié)點(diǎn) 主要供MHA啟動(dòng),關(guān)閉管理和檢測(cè)mysql各種健康狀態(tài)
-
MHA Node :數(shù)據(jù)節(jié)點(diǎn)? 再發(fā)生故障時(shí),盡可能的保存二進(jìn)制日志,比能實(shí)現(xiàn)故障切換
MHA特點(diǎn)
-
自動(dòng)故障切換過(guò)程中,MHA試圖從宕機(jī)的主服務(wù)器上保存二進(jìn)制日志,最大程度的保證數(shù)據(jù)不丟失
-
使用半同步復(fù)制,可以大大降低數(shù)據(jù)丟失的風(fēng)險(xiǎn)
-
目前MHA支持一主多從架構(gòu),最少三臺(tái)服務(wù),即一主兩從
?
MHA工作原理總結(jié)如下:
-
從宕機(jī)崩潰的master 保存二進(jìn)制日志事件 (binlogevents) :
-
識(shí)別含有最新的更新slave 日志
-
應(yīng)用差異的中繼日志 (relay log) 到其他的slave
-
應(yīng)用從master保存的二進(jìn)制日志事件
-
提升一個(gè) salve 為新的master
-
使其他的slave連接行的master 進(jìn)行復(fù)制
MHA的兩個(gè)配置文件
1、masteerha_ip_failover 命令工作定義的是基于VIP檢測(cè)和故障轉(zhuǎn)移 【VIP從master----> new master】 ? 2、app1.cnf: mha的主要配置文件,主要定義了MHA的工作目錄、日志 mysql得到而精致日志位置 使用MHA登錄MYSQL的用戶、密碼使用從服務(wù)器 身份同步master的賬號(hào)、密碼
故障切換時(shí)MHA會(huì)做哪些動(dòng)作
-
MHA會(huì)多次嘗試檢測(cè)master的存活狀態(tài)
-
MHA會(huì)多次嘗試,盡可能地保存二進(jìn)制日志
-
MHA會(huì)根據(jù)app1.cnf 中的配置部分,進(jìn)行從服務(wù)器——>主服務(wù)器的篩選和切換
-
MHA最后,會(huì)將源master的VIP地址切換到從的服務(wù)器的位置MHA最后再選擇新的master之后,會(huì)在其余的 slave之上執(zhí)行 change master的操作,指向新的 master,來(lái)保證mysql集群的健康性
MHA注意問(wèn)題
-
軟連接必須得做
-
mysql配置文件,建議將中文字符集給注釋掉
-
ssh免密鑰交互必須得做
-
MHA的內(nèi)存建議4G 【否則肯能會(huì)導(dǎo)致VIP地址漂移無(wú)效】
-
app1.cnf配置文件中mysql二進(jìn)制文件位置,一定要指對(duì)
實(shí)驗(yàn):
manager:192.168.86.66 安裝MHA node 和 manager 組件
master:192.168.86.11 安裝mysql5.7、MHA node 組件
slave1::192.168.86.22 安裝mysql5.7、MHA node 組件
slave2::192.168.86.33 安裝mysql5.7、MHA node 組件
將master、slave1、slave2三臺(tái)服務(wù)器安裝mysql
關(guān)閉所有服務(wù)器的防火墻
?
用 master、slave1、slave2三臺(tái)服務(wù)器搭建主從復(fù)制 配置etc/my.cnf 【三臺(tái)服務(wù)器的server-id要不同】
master?
slave1?
slave2?
systemctl restart mysqld 重啟服務(wù)
在 Master、Slave1、Slave2 節(jié)點(diǎn)上都創(chuàng)建兩個(gè)軟鏈接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
配置mysql一主兩從
(1)所有數(shù)據(jù)庫(kù)節(jié)點(diǎn)進(jìn)行 mysql 授權(quán)
mysql -uroot -p
grant replication slave on *.* to 'myslave'@'192.168.86.%' identified by '123456'; #從數(shù)據(jù)庫(kù)同步使用
grant all privileges on *.* to 'mha'@'192.168.86.%' identified by 'manager'; #manager 使用
grant all privileges on *.* to 'mha'@'master' identified by 'manager';
#防止從庫(kù)通過(guò)主機(jī)名連接不上主庫(kù)
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
flush privileges; #刷新權(quán)限
在 Master 節(jié)點(diǎn)查看二進(jìn)制文件和同步點(diǎn) 命令:show master status;
在 Slave1、Slave2 節(jié)點(diǎn)執(zhí)行同步操作?
change master to master_host='192.168.86.11',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1811;
這里的數(shù)值要和maste中查看的值保持一致
start slave; #開(kāi)啟同步
show slave status\G #查詢 確保 IO 和 SQL 線程都是 Yes,代表同步正常
兩個(gè)從庫(kù)必須設(shè)置為只讀模式:set global read_only=1;
測(cè)試主從復(fù)制是否同步,在master上創(chuàng)建數(shù)據(jù)庫(kù)
?在slave1和slave2上查看
二、安裝MHA所有組件
(1)所有服務(wù)器上都安裝 MHA 依賴的環(huán)境,首先安裝 epel 源
yum install epel-release --nogpgcheck -yyum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN在所有服務(wù)器上必須先安裝 node 組件,最后在 MHA-manager 節(jié)點(diǎn)上安裝 manager 組件,因?yàn)?manager 依賴 node 組件。
cd /opt
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
在 MHA manager 節(jié)點(diǎn)上安裝 manager 組件
cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
#manager 組件安裝后在/usr/local/bin 下面會(huì)生成幾個(gè)工具,主要包括以下幾個(gè):
masterha_check_ssh 檢查 MHA 的 SSH 配置狀況
masterha_check_repl 檢查 MySQL 復(fù)制狀況
masterha_manger 啟動(dòng) manager的腳本
masterha_check_status 檢測(cè)當(dāng)前 MHA 運(yùn)行狀態(tài)
masterha_master_monitor 檢測(cè) master 是否宕機(jī)
masterha_master_switch 控制故障轉(zhuǎn)移(自動(dòng)或者 手動(dòng))
masterha_conf_host 添加或刪除配置的 server 信息
masterha_stop 關(guān)閉manager#node 組件安裝后也會(huì)在/usr/local/bin 下面會(huì)生成幾個(gè)腳本(這些工具通常由 MHAManager 的腳本觸發(fā),無(wú)需人為操作)主要如下:
save_binary_logs 保存和復(fù)制 master 的二進(jìn)制日志
apply_diff_relay_logs 識(shí)別差異的中繼日志事件并將其差異的事件應(yīng)用于其他的 slave
filter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用這個(gè)工具)
purge_relay_logs 清除中繼日志(不會(huì)阻塞 SQL 線程)
在 manager 節(jié)點(diǎn)上配置到所有數(shù)據(jù)庫(kù)節(jié)點(diǎn)的無(wú)密碼認(rèn)證
ssh-keygen -t rsa
ssh-copy-id 192.168.86.11
ssh-copy-id 192.168.86.22
ssh-copy-id 192.168.86.33
在 master 上配置到數(shù)據(jù)庫(kù)節(jié)點(diǎn) slave1 和 slave2 的無(wú)密碼認(rèn)證
ssh-keygen -t rsa
ssh-copy-id 192.168.86.22
ssh-copy-id 192.168.86.33
在 slave1 上配置到數(shù)據(jù)庫(kù)節(jié)點(diǎn) master 和 slave2 的無(wú)密碼認(rèn)證
ssh-keygen -t rsa
ssh-copy-id 192.168.86.11
ssh-copy-id 192.168.86.33
在 slave2 上配置到數(shù)據(jù)庫(kù)節(jié)點(diǎn) master 和 slave1 的無(wú)密碼認(rèn)證
ssh-keygen -t rsa
ssh-copy-id 192.168.86.11
ssh-copy-id 192.168.86.22
?在 manager 節(jié)點(diǎn)上配置 MHA
(1)在 manager 節(jié)點(diǎn)上復(fù)制相關(guān)腳本到/usr/local/bin 目錄
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷貝后會(huì)有四個(gè)執(zhí)行文件
ll /usr/local/bin/scripts/
master_ip_failover #自動(dòng)切換時(shí) VIP 管理的腳本
master_ip_online_change #在線切換時(shí) vip 的管理
power_manager #故障發(fā)生后關(guān)閉主機(jī)的腳本
send_report #因故障切換后發(fā)送報(bào)警的腳本
(2)復(fù)制上述的自動(dòng)切換時(shí) VIP 管理的腳本到 /usr/local/bin 目錄,這里使用master_ip_failover腳本來(lái)管理 VIP 和故障切換
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
(3)修改內(nèi)容如下:(刪除原有內(nèi)容,直接復(fù)制并修改vip相關(guān)參數(shù))
vim /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
);
#############################添加內(nèi)容部分#########################################
my $vip = '192.168.86.180'; #指定vip的地址
my $brdc = '192.168.86.255'; #指定vip的廣播地址
my $ifdev = 'ens33'; #指定vip綁定的網(wǎng)卡
my $key = '1'; #指定vip綁定的虛擬網(wǎng)卡序列號(hào)
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #代表此變量值為ifconfig ens33:1 192.168.10.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #代表此變量值為ifconfig ens33:1 192.168.10.200 down
my $exit_code = 0; #指定退出狀態(tài)碼為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";
}
(4)創(chuàng)建 MHA 軟件目錄并拷貝配置文件,這里使用app1.cnf配置文件來(lái)管理 mysql 節(jié)點(diǎn)服務(wù)器
mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterhavim /etc/masterha/app1.cnf #刪除原有內(nèi)容,直接復(fù)制并修改節(jié)點(diǎn)服務(wù)器的IP地址
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.86.22 -s 192.168.86.33
shutdown_script=""
ssh_user=root
user=mha[server1]
hostname=192.168.86.11
port=3306[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.86.22
port=3306[server3]
hostname=192.168.86.33
port=3306
第一次配置需要在 Master 節(jié)點(diǎn)上手動(dòng)開(kāi)啟虛擬IP
/sbin/ifconfig ens33:1 192.168.86.180/24
?在manager 節(jié)點(diǎn)上測(cè)試 ssh 無(wú)密碼認(rèn)證,能否正常輸出 successfully?
? 命令:masterha_check_ssh -conf=/etc/masterha/app1.cnf
在 manager 節(jié)點(diǎn)上測(cè)試 mysql 主從連接情況,最后出現(xiàn) MySQL Replication Health is OK 字樣說(shuō)明正常?
?命令:?masterha_check_repl -conf=/etc/masterha/app1.cnf
在 manager 節(jié)點(diǎn)上啟動(dòng) 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 &?--remove_dead_master_conf:該參數(shù)代表當(dāng)發(fā)生主從切換后,老的主庫(kù)的 ip 將會(huì)從配置文件中移除。
--manger_log:日志存放位置。
--ignore_last_failover:在缺省情況下,如果 MHA 檢測(cè)到連續(xù)發(fā)生宕機(jī),且兩次宕機(jī)間隔不足 8 小時(shí)的話,則不會(huì)進(jìn)行 Failover, 之所以這樣限制是為了避免 ping-pong 效應(yīng)。該參數(shù)代表忽略上次 MHA 觸發(fā)切換產(chǎn)生的文件,默認(rèn)情況下,MHA 發(fā)生切換后會(huì)在日志記錄,也就是上面設(shè)置的日志app1.failover.complete文件,下次再次切換的時(shí)候如果發(fā)現(xiàn)該目錄下存在該文件將不允許觸發(fā)切換,除非在第一次切換后收到刪除該文件,為了方便,這里設(shè)置為--ignore_last_failover
查看 MHA 狀態(tài),可以看到當(dāng)前的 master 是 master 節(jié)點(diǎn)。?
命令:masterha_check_status --conf=/etc/masterha/app1.cnf
查看 MHA 日志,也以看到當(dāng)前的 master?
命令:cat /var/log/masterha/app1/manager.log | grep "current master"
若要關(guān)閉 manager 服務(wù),可以使用如下命令?
masterha_stop --conf=/etc/masterha/app1.cnf
或者可以直接采用 kill 進(jìn)程 ID 的方式關(guān)閉