配置MHA(在manager节点上操作)
#创建目录
mkdir -p /etc/mha/scripts
mkdir -p /etc/mha/app1
在三个虚拟机上都创建用户
grant all privileges on . to ‘root’@‘192.168.%’ identified by ‘123’;
配置全局配置文件
vi /etc/masterha_default.cnf#一定要是这个路径,不然后期masterha_check_ssh会提示未找到全局文件
[server default]
#此用户为数据库用户
user=root
password=123
#系统用户root
ssh_user=root
#主从复制用户systop01
repl_user=systop01
repl_password=systop
ping_interval=1
#master_binlog_dir= /data/3306/
secondary_check_script=masterha_secondary_check -s 192.168.31.144 -s 192.168.31.145 -s 192.168.31.146
master_ip_failover_script="/etc/mha/scripts/master_ip_failover"
master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change"
#report_script="/etc/mha/scripts/send_report"
第十一:配置主配置文件
vi /etc/mha/app1.cnf
[server default]
manager_workdir=/etc/mha/app1
manager_log=/etc/mha/app1/manager.log
[server1]
hostname=192.168.31.144
candidate_master=1
master_binlog_dir="/data/3306"
#查看方式 find / -name mysqlmaster-bin*
[server2]
hostname=192.168.31.145
candidate_master=1
master_binlog_dir="/data/3306"
[server3]
hostname=192.168.31.146
master_binlog_dir="/data/3306"
#表示没有机会成为master
no_master=1
第十二:配置VIP
#为了防止脑裂发生,推荐生产环境采用脚本的方式来管理虚拟 ip,而不是使用 keepalived来完成。
vi /etc/mha/scripts/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => ‘all’;
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host,
o
r
i
g
m
a
s
t
e
r
i
p
,
orig_master_ip,
origmasterip,orig_master_port, $new_master_host,
n
e
w
m
a
s
t
e
r
i
p
,
new_master_ip,
newmasterip,new_master_port
);
#定义VIP变量
my $vip = ‘192.168.31.160/24’;
my $key = ‘1’;
my
s
s
h
s
t
a
r
t
v
i
p
=
"
/
s
b
i
n
/
i
f
c
o
n
f
i
g
e
n
s
32
:
ssh_start_vip = "/sbin/ifconfig ens32:
sshstartvip="/sbin/ifconfigens32:key $vip";
my
s
s
h
s
t
o
p
v
i
p
=
"
/
s
b
i
n
/
i
f
c
o
n
f
i
g
e
n
s
32
:
ssh_stop_vip = "/sbin/ifconfig ens32:
sshstopvip="/sbin/ifconfigens32: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====
s
s
h
s
t
o
p
v
i
p
=
=
ssh_stop_vip==
sshstopvip==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();
KaTeX parse error: Expected 'EOF', got '}' at position 16: 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”;
ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"
;
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”;
}
配置编写VIP脚本
vi /etc/mha/scripts/master_ip_online_change
#!/bin/bash
source /root/.bash_profile
vip=echo '192.168.31.160/24'
#设置VIP
key=echo '1'
command=echo "$1" | awk -F = '{print $2}'
orig_master_host=echo "$2" | awk -F = '{print $2}'
new_master_host=echo "$7" | awk -F = '{print $2}'
orig_master_ssh_user=echo "${12}" | awk -F = '{print $2}'
new_master_ssh_user=echo "${13}" | awk -F = '{print $2}'
#要求服务的网卡识别名一样,都为ens32(这里是)
stop_vip=echo "ssh root@$orig_master_host /usr/sbin/ifconfig ens32:$key down"
start_vip=echo "ssh root@$new_master_host /usr/sbin/ifconfig ens32:$key $vip"
if [ $command = ‘stop’ ]
then
echo -e “\n\n\n****************************\n”
echo -e “Disabled thi VIP - $vip on old master: $orig_master_host \n”
$stop_vip
if [ $? -eq 0 ]
then
echo “Disabled the VIP successfully”
else
echo “Disabled the VIP failed”
fi
echo -e “***************************\n\n\n”
fi
if [ $command = ‘start’ -o $command = ‘status’ ]
then
echo -e “\n\n\n*************************\n”
echo -e “Enabling the VIP - $vip on new master: $new_master_host \n”
$start_vip
if [ $? -eq 0 ]
then
echo “Enabled the VIP successfully”
else
echo “Enabled the VIP failed”
fi
echo -e “***************************\n\n\n”
fi
第十五:将脚本赋予可执行权限
chmod +x /etc/mha/scripts/master_ip_failover
chmod +x /etc/mha/scripts/master_ip_online_change
第十六:通过 masterha_check_ssh 验证 ssh 信任登录是否成功
masterha_check_ssh --conf=/etc/mha/app1.cnf
Wed May 16 23:17:58 2018 - [info] All SSH connection tests passed successfully. #表示所有都成功
第十七:通过 masterha_check_repl 验证 mysql 主从复制是否成功(下面输出表示测试通过)
masterha_check_repl --conf=/etc/mha/app1.cnf
IN SCRIPT TEST====/sbin/ifconfig ens192:1 down==/sbin/ifconfig ens192:1 192.168.1.59/24===
Checking the Status of the script… OK
Wed May 16 23:59:42 2018 - [info] OK.
Wed May 16 23:59:42 2018 - [warning] shutdown_script is not defined.
Wed May 16 23:59:42 2018 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
Bash
启动MHA(注意:MHA监控脚本切换一次就会退出,需要再次启动)
第十八:先在master上绑定vip,(只需要在master绑定这一次,以后会自动切换)
/usr/sbin/ifconfig ens32:1 192.168.31.160/24
第十九:然后通过 masterha_manager 启动 MHA 监控(在manager角色上执行)
mkdir /etc/mha/app1 -p
touch /etc/mha/app1/manager.log
#启动mha监控进程,下面是一条命令
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/app1/manager.log 2>&1 &
#这样 MHA 的日志保存在/etc/mha/app1/manager.log 下
检查MHA的启动状态
tail -f /etc/mha/app1/manager.log
#如果最后一行是如下,表明启动成功
[info] Ping(SELECT) succeeded, waiting until MySQL doesn’t respond…
第二十:检查集群状态
masterha_check_status --conf=/etc/mha/app1.cnf
## 第二十一:故障切换
模拟故障切换
停掉31.144机器的mysql服务,salve2上的mha状态监控进程检测到master不可访问,启动故障转移脚本master_ip_failover,master_ip_online_change,将VIP重新绑定到31.145的ens32网卡上,同时将主从关系改为slave1(主)–slave2(从),将app1.cnf里server1的信息删除。
故障恢复,将master重新加入集群
1、在master上恢复mysql服务
/data/3306/mysql start
2、 在master上配置主从复制
master_host指向31.145
CHANGE MASTER TO MASTER_HOST=‘192.168.31.145’,MASTER_USER=‘systop01’,MASTER_PASSWORD=‘systop’,MASTER_LOG_FILE=‘mysql-bin.000020’,MASTER_LOG_POS=234;
CHANGE MASTER TO MASTER_HOST=‘192.168.31.144’,MASTER_USER=‘systop01’,MASTER_PASSWORD=‘systop’,MASTER_LOG_FILE=‘mysqlmaster-bin.000011’,MASTER_LOG_POS=234;
3、在slave2上恢复mha配置
编辑vim app1.cnf
添加server1部分配置
[server default]
manager_log=/etc/mha/app1/manager.log
manager_workdir=/etc/mha/app1
[server1]
hostname=192.168.31.144
candidate_master=1
master_binlog_dir="/data/3306"
[server2]
candidate_master=1
hostname=192.168.31.145
master_binlog_dir="/data/3306"
[server3]
hostname=192.168.31.146
master_binlog_dir="/data/3306"
no_master=1
4、检查主从复制状态和启动mha监控进程
masterha_check_repl --conf=/etc/mha/app1.cnf
masterha_check_ssh --conf=/etc/mha/app1.cnf
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/app1/manager.log 2>&1 &
vim /etc/mha/start_mha.sh
添加:
nohup masterha_manager --conf=/etc/mha/app1.cnf --ignore_last_failover < /dev/null > /etc/mha/app1/manager.log 2>&1 &
chmod +x /etc/mha/start_mha.sh
执行文件来启动
/etc/mha/start_mha.sh
5、回切
将slave1停掉,VIP和主从信息转到master
6、恢复集群
重新在salve1上执行1、2、3、4步,注意添加的信息要修改