MHA
1.概述
MHA(Master High Availability)是一种用于MySQL数据库的高可用性解决方案,旨在实现自动故障切换和最小化数据丢失。它由MHA Manager和MHA Node组成,适用于一主多从的架构。
是建立在主从复制基础之上的故障切换的软件系统。
主从复制的单点问题
当主从复制当中,主服务器发生故障,会自动切换到一台从服务器,然后把从服务器升格为主,继续主从的架构。
2.MHA的工作原理
- master开启二进制日志,并允许从节点复制主节点的二进制日志的内容
- 通过vip地址,当主宕机之后,vip会自动的漂移到从节点
- 从节点会升级为主服务器,然后从宕机的master保存二进制日志,将更新的内容同步到新主,然后再同步到从节点。
3.MHA实验
mysql1 192.168.206.40 主
mysql2 192.168.206.50 从1--主备
mysql3 192.168.206.55 从2
nginx1 192.168.206.60 MHA的组件
vip 192.168.206.100
nginx2 192.168.206.70 mariadb,进行验证
(1)主从复制
三台mysql时间同步:
ntpdate ntp.aliyun.com
如果没有ntpdate就用apt -y install ntpdate安装一下
vim /etc/my.cnf
主mysql1:
log_bin=master_bin
binlog_format=MIXED
log-slave-updates=true
relay_log_recovery=1 #启用之后,从库崩溃或者重启时,会自动尝试从日志当中恢复。
mysql2:
server-id = 2
log_bin = master_bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
relay_log_recovery = 1
mysql3
server-id=3
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
relay_log_recovery = 1
三台服务器一起重启,并添加用户
systemctl restart mysqld
#从数据库同步使用 CREATE USER 'myslave'@'192.168.206.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON . TO 'myslave'@'192.168.206.%'; #manager 使用 CREATE USER 'mha'@'192.168.206.%' IDENTIFIED WITH mysql_native_password BY 'manager'; GRANT ALL PRIVILEGES ON . TO 'mha'@'192.168.206.%' WITH GRANT OPTION;
#防止从库通过主机名连接不上主库 CREATE USER 'mha'@'master' IDENTIFIED WITH mysql_native_password BY 'manager'; GRANT ALL PRIVILEGES ON . TO 'mha'@'master';
CREATE USER 'mha'@'slave1' IDENTIFIED WITH mysql_native_password BY 'manager'; GRANT ALL PRIVILEGES ON . TO 'mha'@'slave1';
CREATE USER 'mha'@'slave2' IDENTIFIED WITH mysql_native_password BY 'manager'; GRANT ALL PRIVILEGES ON . TO 'mha'@'slave2';
flush privileges;
mysql1:show master status;
mysql2和mysql3:
CHANGE master to master_host='192.168.206.40',master_user='myslave', \
master_password='123456',master_log_file='master_bin.000001', \
master_log_pos=157;
start slave;
show slave status\G;
set global read_only=1; #两个从库
(2)安装MHA的组件
安装node
三台mysql和nginx1都要有node
安装依赖环境
apt install -y libdbd-mysql-perl \ libconfig-tiny-perl \ liblog-dispatch-perl \ libparallel-forkmanager-perl \ libextutils-cbuilder-perl \ libmodule-install-perl \ make
#安装不了就apt-get update
#一起解压node压缩包
tar -xf 压缩包名称
perl Makefile.PL #编译
make && make install #安装
相关文件
- masterha_check_ssh:检查mha节点之间ssh的配置和通信
- masterha_manager:manager的启动脚本
- masterha check status:检查mha的运行状态
- masterha_stop:关闭manager
- masterha_master_switch:控制故障转移的方式
- save_binary_logs: 检查,保存,复制master节点的二进制日志
- apply_diff_relay_logs:识别二进制日志当中的差异部分,把差异的部分同步到slave
- master_ip_failover 自动切换vip的管理脚本
- master_ip_online_change 在线切换vip的管理脚本
- power_manager 故障发生后关闭主机的脚本
- send_report 故障发生时,发送报警的脚本
配置所有节点之间的MHA的ssh通信
所有节点之间免密登录
nignx1:
cd /opt
ssh-keygen -t rsa
ssh-copy-id 192.168.206.40
ssh-copy-id 192.168.206.50
ssh-copy-id 192.168.206.55
mysql1:
cd /opt
ssh-keygen -t rsa
ssh-copy-id 192.168.206.50
ssh-copy-id 192.168.206.55
mysql2:
cd /opt
ssh-keygen -t rsa
ssh-copy-id 192.168.206.40
ssh-copy-id 192.168.206.55
mysql3:
cd /opt
ssh-keygen -t rsa
ssh-copy-id 192.168.206.40
ssh-copy-id 192.168.206.50
三台mysql添加软连接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
配置manager
cd /opt/mha4mysql-manager-0.57/samples/scripts
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/
vim /usr/local/bin/master_ip_failover
#里面内容全部删除,粘贴下面的内容
bash
#!/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 = '192.168.206.100';
my $brdc = '192.168.206.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
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";
}
mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
vim /etc/masterha/app1.cnf
#里面内容全部删除,粘贴下面的内容
bash
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
#指向到myql的默认位置
master_ip_failover_script=/usr/local/bin/master_ip_failover
#切换vip的脚本
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.206.50 -s 192.168.206.55
#从对主监听
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.206.40
#主服务器
port=3306
[server2]
candidate_master=1
#声明server2备服务器
check_repl_delay=0
#立刻切换
hostname=192.168.206.50
#备用主服务器
port=3306
[server3]
hostname=192.168.206.55
#从服务器2
port=3306
vim /usr/local/share/perl/5.34.0/MHA/NodeUtil.pm
#set nu 第190行,删除整行,再添加下面的内容
$str =~ /(\d+).(\d+)/;
my $strmajor = "$1.$2";
my $result = sprintf( '%03d%03d', $1, $2 ) if $str =~ m/(\d+).(\d+)/;
检查健康状态
#检查MHA的状态
masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf
启动
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 &
#nohup:执行记录,把执行记录保存到指定的文件
#是否启动成功
masterha_check_status --conf=/etc/masterha/app1.cnf
cat /var/log/masterha/app1/manager.log | grep "current master"
测试
用nginx2作为客户端
apt -y install mariadb-server
systemctl start mariadb
mysql -h 192.168.206.100 -u root -p123456
show databases;
(3)故障切换
mysql1: systemctl stop mysqld #模拟主服务器故障
tail -f /var/log/masterha/app1/manager.log #追踪manager状态
此时vip飘到192.168.206.50
重新启动MHA:
vim /etc/masterha/app1.cnf
#修改的内容如图中框起来的部分
修改mysql1和mysql2的/etc/my.cnf
把msyql1的log-slave-updates=true删除
加上
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
relay_log_recovery = 1
mysql2:
添加log-slave-updates=true
#mysql1和mysql2一起重启
systemctl restart mysqld
进入数据库
对mysql1:
stop slave;
reset slave;
CHANGE master to master_host='192.168.206.50',master_user='myslave',master_password='123456',\
master_log_file='master_bin.000002',master_log_pos=157;
start slave;
show slave status\G;
#重新检查manager状态
masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf
#后台启动服务
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 &
#是否启动成功
masterha_check_status --conf=/etc/masterha/app1.cnf
#关闭MHA
master_stop --conf=/etc/masterha/app1.cnf