MHA概述
MHA(MasterHigh Availability)
基于主库的高可用环境下,可以实现主从复制、故障切换; 主从的架构,最少需要一主两从。
作用
解决Mysql的单点故障问题,一旦主库崩溃,MHA可以在0-30s内自动完成故障切换。
原理
MHA使用的是半同步复制,只要有一台从服务器写入数据,就会自动提交给客户端,
如果master崩溃,slave就会从主的二进制日志保存文件,
并且会识别最新更新的日志,差异部分会同步到slave,
将一个新的slave提升为master,其他的slave继续和新的master同步。
搭建Mysql MHA
实验配置:
20.0.0.10:MHA manager
20.0.0.81:master
20.0.0.82:salve1
20.0.0.83:salve2
node组件-->需要部署在所有服务器上(manager组件依赖于node组件,node组件监控mysql的状态)
node组件靠ssh来进行通信
1.修改主机名
81:
hostnamectl set-hostname Master
su
82:
hostnamectl set-hostname Slave1
su
83:
hostnamectl set-hostname Slave2
su
2.修改Master的Mysql配置文件
81:
vim /etc/my.cnf
--
server-id = 1
log_bin = master-bin
log-slave-updates = true
--
systemctl restart mysqld
3.修改Slave的Mysql配置文件
82:
vim /etc/my.cnf
--
server-id = 2 #三台服务器的 server-id 不能一样
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
--
systemctl restart mysqld
83:
vim /etc/my.cnf
--
server-id = 3 #三台服务器的 server-id 不能一样
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
--
systemctl restart mysqld
4.三个节点创建软链接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
5.配置Mysql一主两从
//所有数据库节点进行mysql授权
mysql -uroot -p123456
#从数据库同步使用
grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123456';
#manager 使用
grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
#防止从库通过主机名连接不上主库
grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
flush privileges;
//在Master节点查看二进制文件和同步点
show master status;
//在Slave1、Slave2节点执行同步操作
change master to master_host='20.0.0.81',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1737;
start slave;
//在Slave1、Slave2节点查看数据同步结果
show slave status\G;
//两个从库必须设置为只读模式
set global read_only=1;
//在Master主库新建库,测试是否同步
create database test_db;
6.安装MHA软件
//所有服务器上都安装MHA依赖的环境,首先安装epel源
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
//安装MHA软件包,先在所有服务器上安装node组件
cd /opt
--拖入node包--
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
//在MHA manager节点上安装manager组件
cd /opt
--拖入manager包--
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
//manager组件安装后在/usr/local/bin下面会生成几个工具,主要包括以下:
masterha_check_ssh:检测MHA的ssh的配置情况
masterha_check_repl:检测Mysql的复制情况
masterha_manager:启动manager的脚本
masterha_check_status:检测MHA的运行状态
masterha_master_monitor:检测master主机的情况(是否宕机)
masterha_master_switch:控制故障转移
masterha_conf_host:添加或者删除配置的server信息
masterha_stop:停止MHA的脚本
//node组件安装后也会在/usr/local/bin下面会生成几个脚本
save_binary_logs:保存和复制master的二进制日志
apply_diff_relay_logs:识别二进制日志当中的差异事件,然后发送给其他的slave
filter_mysqlbinlog:去除不必要的回滚事件(MHA已经不用)
purge_relay_logs:同步之后清除中继日志(不会阻塞sql的限制)
7.所有服务器配置免密认证
//在manager节点上配置到所有数据库节点的无密码认证
ssh-keygen -t rsa #一路按回车键
ssh-copy-id 20.0.0.81
ssh-copy-id 20.0.0.82
ssh-copy-id 20.0.0.83
//在master上配置到数据库节点 slave1 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 20.0.0.82
ssh-copy-id 20.0.0.83
//在slave1上配置到数据库节点 master 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 20.0.0.81
ssh-copy-id 20.0.0.83
//在slave2上配置到数据库节点 master 和 slave1 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 20.0.0.81
ssh-copy-id 20.0.0.82
8.在manager节点上配置MHA
//在 manager 节点上复制相关脚本到/usr/local/bin 目录
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
#拷贝后会有四个执行文件
cd /usr/local/bin/scripts/
ll
master_ip_failover 自动切换时 VIP 管理的脚本
master_ip_online_change 在线切换时 vip 的管理
power_manager 故障发生后关闭主机的脚本
send_report 因故障切换后发送报警的脚本
//复制上述的自动切换时VIP管理的脚本到/usr/local/bin目录,
这里使用master_ip_failover脚本来管理VIP和故障切换
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
//修改内容
vim /usr/local/bin/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 = '20.0.0.100';
my $brdc = '20.0.0.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";
}
--
//创建 MHA 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器
mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
vim /etc/masterha/app1.cnf
--
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
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
remote_workdir=/tmp
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.82 -s 20.0.0.83
#从对主监听
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.81
#主服务器
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.82
#备用主服务器
port=3306
[server3]
hostname=20.0.0.83
#从服务器2
port=3306
--
9.在Master节点上手动开启虚拟IP
81:
/sbin/ifconfig ens33:1 20.0.0.100/24
10.在manager节点上测试ssh无密码认证
10:
masterha_check_ssh -conf=/etc/masterha/app1.cnf
11.在manager节点上测试mysql主从连接情况
10:
masterha_check_repl -conf=/etc/masterha/app1.cnf
12.在manager节点上启动MHA
10:
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 &
13.查看MHA状态
10:
masterha_check_status --conf=/etc/masterha/app1.cnf
14.查看MHA日志
10:
cat /var/log/masterha/app1/manager.log | grep "current master"
15.查看Master的VIP地址是否存在
81:
ifconfig
故障模拟
//在manager节点上监控观察日志记录
tail -f /var/log/masterha/app1/manager.log
//在Master节点master上停止mysql服务
systemctl stop mysqld
故障修复
1.修改Master的Mysql配置文件
81:
vim /etc/my.cnf
--
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
--
systemctl restart mysqld.service
2.修改Slave1的Mysql配置文件
82:
vim /etc/my.cnf
--
log_bin = master-bin
log-slave-updates = true
--
systemctl restart mysqld.service
3.关闭manager服务并修改配置文件
10:
masterha_stop --conf=/etc/masterha/app1.cnf
vim /etc/masterha/app1.cnf
--
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
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
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.81 -s 20.0.0.83
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.82
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.81
port=3306
[server3]
hostname=20.0.0.83
port=3306
--
4.在manager节点上启动MHA
10:
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 &
5.配置主从复制
82:
mysql -u root -p123456
show master status;
81:
mysql -u root -p123456
stop slave;
change master to master_host='20.0.0.82',master_user='myslave',master_password='123456',master_log_file='master-bin.000003',master_log_pos=154;
reset slave;
start slave;
6.测试
82(新主):
create database test1_db;