MySQL高可用MHA配置与故障切换
文章目录
解决的问题
主从复制、读写分离后,如果主服务器挂掉,则数据无法继续写入,即出现单点故障问题。
这时,只要从从服务器中选出一台新的主服务器,代替原主服务器继续进行工作,于是出现了MHA架构。
一、MHA概述
- 一套优秀的MySQL高可用环境下故障切换和主从复制的软件
- MySQL故障过程中,MHA能做到0-30秒内自动完成故障切换
二、MHA的组成
- MHA Manager (管理节点)
- MHA Node (数据节点)
三、MHA特点
- 自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
- 使用半同步复制,可以大大降低数据丢失的风险
- 目前MHA支持一主多从架构,最少三台服务器,即一主两从,一台为主备,也可以两台从的都做主备
四、实验拓扑与配置
4.1 前期环境建设
三台数据库服务器都装上mysql5.7
安装过程见MySQL数据库5.7的编译安装过程
清空防火墙规则 iptables -F
关闭核心防护 setenforce 0
把主机名改为mysql1,mysql2,mysql3后
进三台服务器的hosts文件添加映射关系
vim /etc/hosts
20.0.0.21 mysql1
20.0.0.22 mysql2
20.0.0.23 mysql3
修改完后能ping通就是ok
[aaa@qq.com ~]# ping mysql1
PING mysql1 (20.0.0.21) 56(84) bytes of data.
64 bytes from mysql1 (20.0.0.21): icmp_seq=1 ttl=64 time=0.607 ms
64 bytes from mysql1 (20.0.0.21): icmp_seq=2 ttl=64 time=1.92 ms
4.2 配置文件修改
主服务器
vim /etc/my.cnf
[client]
port = 3306
#default-character-set=utf8 //这边注释掉
socket = /usr/local/mysql/mysql.sock
[mysqld]
在这个模块最后
server-id = 11 //ID三台都不一样
log-bin = master-bin //加入下面两条
log-slave-updates = true
配置完成后重启数据库
[aaa@qq.com ~]# systemctl restart mysqld
从服务器
vim /etc/my.cnf
client]
port = 3306
#default-character-set=utf8 //这边注释掉
socket = /usr/local/mysql/mysql.sock
[mysqld]
server-id = 22
log_bin = master-bin //作为主备服务器,加入这条
relay-log = relay-log-bin //作为从服务器,加入这两条
relay-log-index = slave-relay-bin.index
[aaa@qq.com ~]# systemctl restart mysqld //重启数据库
4.3 三台服务器都做这两个软链接
[aaa@qq.com ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[aaa@qq.com ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
4.4 授权
在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是 manager 使用。
[aaa@qq.com ~]# mysql -uroot -p 进数据库
Enter password:
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identifiedtified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
授权从服务器
mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)
授权manager
mysql> flush privileges;
刷新权限
4.5 Mysql1 主机上查看二进制文件和同步点
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 | 885 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
4.6在 Mysql2 和 Mysql3 分别执行同步。
mysql> change master to master_host='20.0.0.21',master_user='myslave',master_password='123456',master_log_file='master-bin.000002',master_log_pos=885;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
看一下,这边yes,主从基本就OK啦,自己测试一下主从复制
必须设置两个从库为只读模式:
mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
4.7.安装MHA节点
现在四台服务器上安装node节点
注意:manager也要安装,而且要先装node,再装manager,因为 manager 依赖 node 组件
- 所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源。
(如果安装不上,yum仓库CentOS-Base.repo重新加载,如果有问题见
配置yum 源,yum makecache时报错
要联网
[aaa@qq.com yum.repos.d]# 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
2.所有服务器安装node节点
注意:CentOS7以上版本MHA包的版本为0.57
7以前版本,MHA包用0.56的
[aaa@qq.com ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[aaa@qq.com ~]# cd mha4mysql-node-0.57
[aaa@qq.com mha4mysql-node-0.57]# perl Makefile.PL
[aaa@qq.com mha4mysql-node-0.57]# make
[aaa@qq.com mha4mysql-node-0.57]# make install
3.manager服务器安装manager节点
[aaa@qq.com ~]# tar zxvf mha4mysql-manager-0.57.tar.gz
[aaa@qq.com ~]# cd mha4mysql-manager-0.57/
[aaa@qq.com mha4mysql-manager-0.57]# perl Makefile.PL
[aaa@qq.com mha4mysql-manager-0.57]# make
[aaa@qq.com mha4mysql-manager-0.57]# make install
4.配置无密码认证
- 在 manager 上配置到所有数据库节点的无密码认证
[aaa@qq.com ~]# ssh-****** -t rsa
[aaa@qq.com ~]# ssh-copy-id 20.0.0.21
[aaa@qq.com ~]# ssh-copy-id 20.0.0.22
[aaa@qq.com ~]# ssh-copy-id 20.0.0.23
- 在 Mysql1 上配置到数据库节点Mysql2和Mysql3的无密码认证
[aaa@qq.com ~]# ssh-****** -t rsa
[aaa@qq.com ~]# ssh-copy-id 20.0.0.22
[aaa@qq.com ~]# ssh-copy-id 20.0.0.23
- 在 Mysql2 上配置到数据库节点Mysql1和Mysql3的无密码认证
[aaa@qq.com ~]# ssh-****** -t rsa
[aaa@qq.com ~]# ssh-copy-id 20.0.0.21
[aaa@qq.com ~]# ssh-copy-id 20.0.0.23
- 在 Mysql3 上配置到数据库节点Mysql1和Mysql2的无密码认证
[aaa@qq.com ~]# ssh-****** -t rsa
[aaa@qq.com ~]# ssh-copy-id 20.0.0.21
[aaa@qq.com ~]# ssh-copy-id 20.0.0.22
4.8配置MHA
在 manager 节点上复制相关脚本到/usr/local/bin 目录。
[aaa@qq.com ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后会有四个执行文件
[aaa@qq.com ~]# ll /usr/local/bin/scripts/
total 32
-rwxr-xr-x 1 mysql mysql 3648 May 31 2015 master_ip_failover
-rwxr-xr-x 1 mysql mysql 9870 May 31 2015 master_ip_online_change
-rwxr-xr-x 1 mysql mysql 11867 May 31 2015 power_manager
-rwxr-xr-x 1 mysql mysql 1360 May 31 2015 send_report
复制上述的自动切换时 VIP 管理的脚本到/usr/local/bin 目录,这里使用脚本管理 VIP,
[aaa@qq.com ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[aaa@qq.com ~]# 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.200';
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;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
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 (aaa@qq.com) {
warn "Got Error: aaa@qq.com\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 (aaa@qq.com) {
warn aaa@qq.com;
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";
}
把开头的#注释去掉,别忘了!/usr/bin/env perl 前#加回去
创建 MHA 软件目录并拷贝配置文件
[aaa@qq.com ~]# mkdir /etc/masterha
[aaa@qq.com ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[aaa@qq.com ~]# 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
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=myslavesecondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.22 -s 20.0.0.23
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.21
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.22
port=3306
[server3]
hostname=20.0.0.23
port=3306
测试 ssh 无密码认证,如果正常最后会输出 successfully
[aaa@qq.com ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
进行健康状态检查,如果不ok,就是两个配置文件出问题,根据报错仔细检查,直接粘贴上面的脚本那一般是没粘好或没改对
[aaa@qq.com ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
4.9测试
注意:第一次配置需要去master上手动开启虚拟IP
[aaa@qq.com ~]# /sbin/ifconfig ens33:1 20.0.0.200/24
启动MHA
启动MHA
[aaa@qq.com ~]# 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] 33370
查看 MHA 状态,可以看到当前的 master 是 Mysql1 节点。
[aaa@qq.com ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:33370) is running(0:PING_OK), master:20.0.0.21
查看 MHA 日志,也以看到当前的 master
[aaa@qq.com ~]# cat /var/log/masterha/app1/manager.log
Thu Oct 22 16:28:25 2020 - [info] Current Alive Master: 20.0.0.21(20.0.0.21:3306)
Thu Oct 22 16:28:25 2020 - [info] Checking slave configurations..
Thu Oct 22 16:28:25 2020 - [warning] relay_log_purge=0 is not set on slave 20.0.0.22(20.0.0.22:3306).
Thu Oct 22 16:28:25 2020 - [warning] relay_log_purge=0 is not set on slave 20.0.0.23(20.0.0.23:3306).
[aaa@qq.com ~]#tailf /var/log/masterha/app1/manager.log //启用监控观察日志记录
[aaa@qq.com ~]# systemctl stop mysqld //主数据库关掉
观察到master变更
飘到2上了