Mysql的MHA
一、MHA
1、什么是MHA?
高可用模式下的故障切换,基于主从复制
单点故障和主从复制不能切换的问题
至少需要3台
故障切换过程0~30秒
vip地址,根据vip地址所在的主机,确定主备
主 vip 备 vip
主和备不是优先确定的,主从复制的时候确定了主,备是在MHA的过程中确定
2、MHA的组件
MHA NODE:数据节点,每台的mysql的管理服都要安装,监控服务器状态以及收集数据
MHA的 manager 管理节点 管理mysql的高可用集群
可以单独部署在一台单独的服务器,也部署多个
实现主备之间切换,主发生故障,切换到备
3、MHA的特点
1.1 manager来实现主备切换
1.2 数据同步还是依靠二进制日志,最大程度上保证数据的完整
1.3 半同步的方式,实现数据完整
支持一主多从的架构,最少要三台
4、主备故障切换
1.1 主宕机,保存二进制日志
1.2 备从主的二进制日志当中更新到自己的slave日志当中
1.3 备成为主,同步到master的二进制文件
1.4 其他备服务器从新的主同步数据
1.5 原来的备成为主,其他的备的服务器都和主继续同步数据
1.6 主备切换之后,mysql模式下,一般是继续以现有主作为集群的主,重新把服务器加入到集群
二、MHA实验
架构:一主两从
搭建完成MHA的架构
主备之间的切换
故障恢复
4台机器
master 192.168.100.17 mysql8.0 node组件
slave1 192.168.100.18 mysql8.0 node组件
slave2 192.168.100.19 mysql8.0 node组件
管理节点 192.168.100.14 manager组件 node组件
1.1 主从复制
bash
# mysql7、8、9
systemctl stop firewalld
setenforce 0
# 时间同步
yum -y install ntpdate
ntpdate ntp.aliyun.com
date
# mysql7
hostnamectl set-hostname master
# mysql8
hostnamectl set-hostname slave1
# mysql9
hostnamectl set-hostname slave2
# mysql7、8、9
vim /etc/hosts
192.168.100.17 master
192.168.100.18 slave1
192.168.100.19 slave2
# mysql7
vim /etc/my.cnf
server-id = 1
log_bin = master-bin
binlog_format = MIXED
log-slave-updates = true
relay_log_recovery = 1
wq!
systemctl restart mysqld
# mysql8
vim /etc/my.cnf
log-bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
relay_log_recovery = 1
wq!
systemctl restart mysqld
# mysql9
vim /etc/my.cnf
server-id = 3
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
relay_log_recovery = 1
wq!
systemctl restart mysqld
mysql7、8、9
ln -s /usr/local/mysql/bin/mysql /usr/sbin
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin
mysql -uroot -p123456
#从数据库同步使用
CREATE USER 'myslave'@'192.168.233.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.233.%';
#manager 使用
CREATE USER 'mha'@'192.168.233.%' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.233.%' 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;
# mysql7
show master status;
# mysql8、9
change master to master_host='192.168.100.17',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=???;
start slave;
show slave status\G;
# navicate
在主数据库上创建
create database test1;
# mysql8、9终端数据库
set global read_only=1;
# 设置成只读模式
1.2 部署MHA
bash
# mysql7、8、9 test4
# 同步操作 #
cd /opt #把软件包拖进去
yum install epel-release --nogpgcheck -y
# 安装依赖环境
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
tar -xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
# 开始编译安装
perl Makefile.PL # 编译
make && make install # 安装
# 此时node节点安装完毕 #
# test4
tar -xf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
cd /usr/local/bin
# 查看这个目录下的文件
# masterha_check_ssh 所有的数据库节点和管理节点通过ssh来进行互相通信,检查集群的ssh配置
# masterha_check_repl 检查mysql的复制情况 数据同步
# masterha_manager 是manager文件的启动脚本
# masterha_check_status 检查MHA集群状态的文件
# masterha_master_switch 控制故障转移
# masterha_stop 关闭manager服务
ssh-keygen -t rsa 一直回车即可 #指定算法
ssh-copy-id 192.168.100.17 # yes # 123
ssh-copy-id 192.168.100.18 # yes # 123
ssh-copy-id 192.168.100.19 # yes # 123
# 此时manager节点安装完毕 #
# mysql7
ssh-keygen -t rsa 一直回车即可 #指定算法
ssh-copy-id 192.168.100.18 # yes # 123
ssh-copy-id 192.168.100.19 # yes # 123
# mysql8
ssh-keygen -t rsa 一直回车即可 #指定算法
ssh-copy-id 192.168.100.17 # yes # 123
ssh-copy-id 192.168.100.19 # yes # 123
# mysql9
ssh-keygen -t rsa 一直回车即可 #指定算法
ssh-copy-id 192.168.100.17 # yes # 123
ssh-copy-id 192.168.100.18 # yes # 123
# test4
cd /opt/mha4mysql-manager-0.57
cd samples
cd scripts
# 解释scripts目录下的文件
# power_manager 故障发生后,关闭主机的脚本
# send_report 故障切换之后,发送报警的脚本
cd .. #到samples目录
cp -rp scripts/ /usr/local/bin
cd /usr/local/bin # 到这个目录下ll检查一下是否复制到这个目录下
cd /opt
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/
cd /usr/local/bin/
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 = '192.168.100.100';
my $brdc = '192.168.100.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";
}
wq!
mkdir /etc/masterha
pwd #/usr/local/bin
cd /opt/mha4mysql-manager-0.57/samples
cd conf
cp app1.cnf /etc/masterha/
cd /etc/masterha/ # 这是管理配置文件用来管理mysql的管理服务器
vim app1.cnf
# 清空所有复制粘贴以下文本
[server default]
manager_log=/var/log/masterha/app1/manager.log
#主日志文件,报错看这个
manager_workdir=/var/log/masterha/app1
#manager的工作目录
master_binlog_dir=/usr/local/mysql/data
#mysql主服务器的binlog二进制文件的保存目录
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
#ping主库的时间间隔,每一秒ping一次,ping不通就进行failover自动切换,生产环境一般3秒,实验环境设置成一秒即可
remote_workdir=/tmp
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.100.18 -s 192.168.100.19
#从对主监听
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.100.17
#主服务器
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.100.18
#备用主服务器
port=3306
[server3]
hostname=192.168.100.19
#从服务器2
port=3306
wq!
# 此时所有配置全部完成
# mysql7
ifconfig ens33:1 192.168.100.100/24
ifconfig
# test4
cd /etc/msaterha
1.1
masterha_check_ssh -conf=/etc/masterha/app1.cnf
1.2
masterha_check_repl -conf=/etc/masterha/app1.cnf
1.3
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 &
1.4
masterha_check_status --conf=/etc/masterha/app1.cnf
1.5
cat /var/log/masterha/app1/manager.log | grep "current master"
1.3 模拟故障切换
bash
# 在navicate的mysql1-7上模拟
create database test2;
# 此时主库和从库上都出现test2
# 在终端上打开另外一台test4查看日志文件
tail -f /var/log/masterha/app1/manager.log
# mysql7
systemctl stop mysqld
# 关闭主的mysql查看test4的日志文件记录
# 此时可以从test4的日志文件中看出,主挂掉后,自动切换,此时备主成为主
ip addr # 192.168.100.100的ip消失
# mysql8
ip addr #192.168.100.100出现
# test4
1.1
masterha_check_status --conf=/etc/masterha/app1.cnf
1.2
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 &
1.3
masterha_check_status --conf=/etc/masterha/app1.cnf
# 此时的运行的主是192.168.100.18
# 到navicate
# 关闭mysql1-7,在mysql8上创建数据库test3看是否同步到mysql9
create database test3;
# 刷新,可同步到mysql9,主备切换成功
1.4 故障恢复
bash
# test4
1.1 关闭manager
masterha_stop --conf=/etc/masterha/app1.cnf
1.2 修改配置
vim /etc/masterha/app1.cnf
# 修改配置文件如下
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.100.17 -s 192.168.100.19
[server1]
candidate_master=1
check_repl_delay=0
hostname=192.168.100.17
port=3306
[server2]
hostname=192.168.100.18
port=3306
wq!
1.3 修改mysql7的配置文件
vim /etc/my.cnf
# 在配置文件里面增加下面的命令
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
wq!
systemctl restart mysqld
1.4 进入主mysql8的数据库
mysql -u root -p123456
set global read_only=0;
# 关闭只读模式
show master status;
1.5 进入mysql7的数据库
mysql -u root -p123456
set global read_only=1;
stop slave;
reset slave;
change master to master_host='192.168.100.18',master_user='myslave',master_password='123456',master_log_file='master-bin.000004',master_log_pos=3666;
start slave;
show slave status\G;
# test4
1.1
masterha_check_status --conf=/etc/masterha/app1.cnf
1.2
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 &
1.3
masterha_check_status --conf=/etc/masterha/app1.cnf
1.4
masterha_check_repl -conf=/etc/masterha/app1.cnf
# 在navicate上面操作
# 开启mysql1-7,在mysql8上创建数据库test4看是否同步到mysql7和mysql9
create database test4;
# 刷新,可同步到mysql7和mysql9,故障恢复成功