MHA架构原理
(1)从宕机崩溃的master保存二进制日志事件(binlog events);
(2)识别含有最新更新的slave;
(3)应用差异的中继日志(relay log)到其他的slave;
(4)应用从master保存的二进制日志事件(binlog events);
(5)提升一个slave为新的master;
(6)使其他的slave连接新的master进行复制
搭建:
配置server1 server2 server3 ptid复制
在server4上安装
yum install -y mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm perl-*
设置ssh免密码登陆
ssh-keygen
ssh-copy-id server1
ssh-copy-id server2
ssh-copy-id server3
传送节点安装包
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server1:~
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2:~
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server3:~
并在各主机中安装
设置mha管理文件
mkdir -p /etc/masterha
cd /etc/masterha/
vim app1.cnf
[server default]
manager_workdir=/etc/masterha
manager_log=/var/log/masterha.log
master_binlog_dir=/etc/masterha
#master_ip_failover_script= /usr/local/bin/master_ip_failover
#master_ip_online_change_script= /usr/local/bin/master_ip_online_change
password=Zcx971203.
user=root
ping_interval=1
remote_workdir=/tmp
repl_password=Zcx971203.
repl_user=repl
#report_script=/usr/local/send_report
#secondary_check_script= /usr/local/bin/masterha_secondary_check -s server03 -s server02
#shutdown_script=""
ssh_user=root
[server1]
hostname=172.25.24.1
port=3306
[server2]
hostname=172.25.24.2
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.24.3
port=3306
no_master=1
使主从服务器之间可以互相连接
scp -r .ssh server1:
scp -r .ssh server2:
scp -r .ssh server3:
masterha_check_ssh --conf=/etc/masterha/app1.cnf ##检查能否互连
mysql主服务器中:
mysql> grant all on *.* to root@'%' identified by 'Zcx971203.';
masterha_check_repl --conf=/etc/masterha/app1.cnf ##检测主从复制是否正常 需要能登陆主服务器root
启动MHA服务
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha.log 2>&1 &
关闭MHA服务
masterha_stop --conf=/etc/masterha/app1.cnf
关闭mysql主服务器server1:
在server1上
systemctl stop mysql
手动切换主服务器到server2上:
在MHA服务器上:
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=172.25.24.1 --dead_master_port=3306 --new_master_host=172.25.24.2 --new_master_port=3306
在没有服务器关闭的情况切换主服务器
masterha_master_switch --master_state=alive --conf=/etc/masterha/app1.cnf --new_master_host=172.25.24.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
测试MHA服务能否自动切换主服务器
[root@server4 masterha]# nohup masterha_manager --conf=/etc/masterha/app1.cnf &> /dev/null &
[1] 11783
[root@server1 .ssh]# systemctl stop mysqld
[root@server4 masterha]#
[1]+ Done nohup masterha_manager --conf=/etc/masterha/app1.cnf &>/dev/null
###若遇到此报错
Tue May 7 21:02:08 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm, ln310] Last failover was done at 2019/05/07 20:54:10. Current time is too early to do failover again. If you want to do failover, manually remove /etc/masterha/app1.failover.complete and run this script again.
rm -rf /etc/masterha/app1.failover.complete
主服务器使用VIP 并使用脚本完成切换主服务器
先开启server1上的mysql并设置为server2的从服务器
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='172.25.24.2',master_user='repl',master_password='Zcx971203.',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
vim 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 = '172.25.24.100/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
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 \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`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";
}
vim master_ip_online_change
#!/usr/bin/env perl
use strict;
use warnings FATAL =>'all';
use Getopt::Long;
my $vip = '172.25.24.100/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
my $exit_code = 0;
my (
$command, $orig_master_is_new_slave, $orig_master_host,
$orig_master_ip, $orig_master_port, $orig_master_user,
$orig_master_password, $orig_master_ssh_user, $new_master_host,
$new_master_ip, $new_master_port, $new_master_user,
$new_master_password, $new_master_ssh_user,
);
GetOptions(
'command=s' => \$command,
'orig_master_is_new_slave' => \$orig_master_is_new_slave,
'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,
'orig_master_ssh_user=s' => \$orig_master_ssh_user,
'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,
'new_master_ssh_user=s' => \$new_master_ssh_user,
);
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 "\n\n\n***************************************************************\n";
print "Disabling the VIP - $vip on old master: $orig_master_host\n";
print "***************************************************************\n\n\n\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 "\n\n\n***************************************************************\n";
print "Enabling the VIP - $vip on new master: $new_master_host \n";
print "***************************************************************\n\n\n\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 $orig_master_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 $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $orig_master_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";
}
cp master_ip_* /usr/local/bin/
chmod +x /usr/local/bin/master_ip_*
vim /etc/masterha/app1.cnf ##将使用脚本的那两行取消注释
在当前的主服务器server2中加入VIP
ip addr add 172.25.24.100/24 dev eth0
在MHA服务器server4中
cd /etc/masterha/
rm -rf app1.failover.complete
手动将server1变为主服务器
masterha_master_switch --master_state=alive --conf=/etc/masterha/app1.cnf --new_master_host=172.25.24.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
可看到VIP飘移
查看MHA能否自动故障转移
cd /etc/masterha/
rm -rf app1.failover.complete
[root@server4 ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf &> /dev/null &
[1] 11885
[root@server1 .ssh]# systemctl stop mysqld
[root@server4 ~]#
[1]+ Done nohup masterha_manager --conf=/etc/masterha/app1.cnf &>/dev/null
可看到server2转化为主服务器