MySQL5.6基於MHA方式高可用搭建


master 10.205.22.185 #MHA node

slave1 10.205.22.186  #MHA node+MHA manager

slave2 10.205.22.187  #MHA node

三台服務器的MySQL已經搭建好主從架構,並互相配置好ssh免密碼登錄。

1.下載MHA,並安裝MHA包的依賴軟件

https://downloads.mariadb.com/MHA(或者 https://github.com/yotoobo/linux/tree/master/mha
yum install perl-Config-Tiny perl-Log-Dispatch perl-Time-HiRes perl-Parallel-ForkManager

2.安裝MHA軟件包

rpm -ivh mha4mysql-node-0.56-0.el6.noarch #所有節點安裝
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch #186管理節點安裝

3.創建用戶mha管理的賬號,在所有mysql服務器上都需要執行:

GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'10.205.22.%' IDENTIFIED BY '123456';

如果是在slave服務器上安裝的manager,則需要創建以本機hostname名連接的賬號,不然masterha_check_repl測試通不過。
GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'master(主機名)' IDENTIFIED BY '123456'

4.在MHA manager服務器添加配置文件/etc/masterha/app1.cnf(自定義),並創建相關目錄和修改權限

[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
secondary_check_script= masterha_secondary_check -s 10.205.22.185 -s 10.205.22.186 #實現多路由監測Master的可用性
master_ip_failover_script=/usr/local/scripts/master_ip_failover

user=mha_rep
password=123456

ssh_user=root
repl_user=repl
repl_password=password
ping_interval=1

[server1]
hostname=10.205.22.185
candidate_master=1
master_binlog_dir=/home/data/mysql/binlog

[server2]
hostname=10.205.22.186
candidate_master=1
master_binlog_dir=/home/data/mysql/binlog

[server3]
hostname=10.205.22.187
no_master=1
master_binlog_dir=/home/data/mysql/binlog
mkdir -p /home/data/mysql/binlog #創建相關目錄
chown -R mysql.mysql /home/data/mysql/binglog #修改權限

 

5.編寫自動切換VIP的腳本,/usr/local/scripts/master_ip_failover ,並加上可執行權限+x。

!/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.205.22.111/24'; # Virtual IP
my $gateway = '10.205.22.1'; #Gateway IP
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
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" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
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";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
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";
}

6.檢查配置文件

利用mha工具檢測ssh
masterha_check_ssh --conf=/etc/masterha_application.cnf

使用mha工具check檢查repl環境
masterha_check_repl --conf=/etc/masterha_application.cnf

7.開啟MHA manager,並查看狀態

開啟masterha_manager
nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log  2>&1 &
(masterha_stop --conf=/etc/masterha/app1.cnf #關閉時使用的命令)

查看
masterha_check_status --conf=/etc/masterha/app1.cnf

8.模擬MySQL故障,查看VIP漂移和MySQL自動切換情況(切換后MHA服務會自動停止)

/etc/init.d/mysqld stop #master上操作
在manager上查看切換日志:tail -f /var/log/masterha/app1/manager.log 
查看備用節點情況:show master status \G

9.MySQL故障服務器重新加入MHA環境

1.把故障服務器設為新的slave
2.重新啟動MHA manager
3.查看MHA狀態

10.在線手動切換主從,沒有啟用MHA自動切換功能

1.原master出現故障
masterha_stop --conf=/etc/masterha/app1.cnf #停止
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=10.205.22.185 --dead_master_port=3306 --new_master_host=10.205.22.186 --new_master_port=3306 --ignore_last_failover

2.把原master變為slave切換
masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=10.205.22.185 --new_master_port=3306 --orig_master_is_new_slave

為了保證數據完全一致性,在最快的時間內完成切換,MHA的在線切換必須滿足以下條件才會切換成功,否則會切換失敗。

1.所有slave的IO線程都在運行

2.所有slave的SQL線程都在運行

3.所有的show slave status的輸出中Seconds_Behind_Master參數小於或者等於running_updates_limit秒,如果在切換過程中不指定running_updates_limit,那么默認情況下running_updates_limit為1秒。

4.在master端,通過show processlist輸出,沒有一個更新花費的時間大於running_updates_limit秒。

 


免責聲明!

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



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