MHA Node 运行在每台 MySQL 服务器上。
1.MHA架构 ①数据库安装 ②一主两从 ③MHA搭建
2.故障模拟 ①主库失效 ②备选主库成为主库 ③原故障主库恢复重新加入到MHA成为从库
节点服务器 | 主机名 | IP地址 | 安装服务及工具 |
---|---|---|---|
MHA manager节点服务 器 | manager | 192.168.137.105 | 安装MHA node和manager组 件 |
Master节点服务器 | master1 | 192.168.137.101 | 安装mysql5.7和MHA node组件 |
Slave1节点服务器 | mysql2 | 192.168.137.102 | 安装mysql5.7和MHA node组件 |
Slave2节点服务器 | mysql3 | 192.168.137.103 | 安装mysql5.7和MHA node组件 |
#在四台服务器上初始化环境
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#在Master上
hostnamectl set-hostname Mysql1
su
#在Slave1
hostnamectl set-hostname Mysql2
su
#在Slave2
hostnamectl set-hostname Mysql3
su
vim /etc/hosts
192.168.137.101 master1
192.168.137.102 master2
192.168.137.103 master3
#修改 Master、Slave1、Slave2 节点的 Mysql主配置文件/etc/my.cnf
##Master 节点##
vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
systemctl restart mysqld
##Slave1 节点##
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
###Slave2 节点##
vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
systemctl restart mysqld
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
(1)所有数据库节点进行 mysql 授权
mysql> grant replication slave on *.* to 'myslave'@'192.168.137.%' identified by 'abc123';
mysql> grant all on *.* to 'mha'@'mysql1' identified by 'abc123';
mysql> grant all on *.* to 'mha'@'mysql2' identified by 'abc123';
mysql> grant all on *.* to 'mha'@'mysql3' identified by 'abc123';
mysql> grant all on *.* to 'mha'@'192.168.137.%' identified by 'abc123';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1899 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> change master to
-> master_host='192.168.137.101',
-> master_user='myslave',
-> master_password='abc123',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1899;
start slave;
show slave status\G
//确保 IO 和 SQL 线程都是 Yes,代表同步正常。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
(5)两个从库必须设置为只读模式
set global read_only=1;
mysql> create database zzx;
mysql> use zzx;
mysql> create table niu(id int,name varchar(5),hobby varchar(20));
mysql> insert into niu values (1,'李','玩');
mysql> insert into niu values (1,'王','叫');
进行master插入数据
查看两个从库是否同步成功
6.安装 MHA 软件
(1)所有服务器上都安装 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
(2)安装 MHA 软件包,先在所有服务器上必须先安装 node 组件
对于每个操作系统版本不一样,这里 CentOS7.4 必须选择 0.57 版本。
在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。
cd /opt
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
(3)在 MHA manager 节点上安装 manager 组件
cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
manager 数据库节点
mysql 1数据库
mysql2 数据库
mysql3 数据库
(1)在 manager 节点上复制相关脚本到/usr/local/bin 目录
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后会有四个执行文件
ll /usr/local/bin/scripts/
----------------------------------------------------------------------------------------------------------
master_ip_failover #自动切换时 VIP 管理的脚本
master_ip_online_change #在线切换时 VIP 的管理
power_manager #故障发生后关闭主机的脚本
send_report #因故障切换后发送报警的脚本
(2)复制上述的自动切换时 VIP 管理的脚本到 /usr/local/bin 目录,这里使用master_ip_failover脚本来管理 VIP 和故障切换
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
3)修改内容如下:(删除原有内容,直接复制并修改vip相关参数。可在拷贝前输入 :set paste 解决vim粘贴乱序问题)
vim /usr/local/bin/master_ip_failover
删除原内容添加下面内容
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $orig_master_host, $orig_master_ip,$ssh_user,
$orig_master_port, $new_master_host, $new_master_ip,$new_master_port,
$orig_master_ssh_port,$new_master_ssh_port,$new_master_user,$new_master_password
);
# 这里定义的虚拟IP配置要注意,这个ip必须要与你自己的集群在同一个网段,否则无效
my $vip = '192.168.137.200/24';
my $key = '1';
# 这里的网卡名称 “ens33” 需要根据你机器的网卡名称进行修改
# 如果多台机器直接的网卡名称不统一,有两种方式,一个是改脚本,二是把网卡名称修改成统一
# 我这边实际情况是修改成统一的网卡名称
my $ssh_start_vip = "sudo /sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "sudo /sbin/ifconfig ens33:$key down";
my $ssh_Bcast_arp= "sudo /sbin/arping -I ens33 -c 3 -A $vip";
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,
'orig_master_ssh_port=i' => \$orig_master_ssh_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
'new_master_ssh_port' => \$new_master_ssh_port,
'new_master_user' => \$new_master_user,
'new_master_password' => \$new_master_password
);
exit &main();
sub main {
$ssh_user = defined $ssh_user ? $ssh_user : 'root';
print "\n\nIN SCRIPT TEST====$ssh_user|$ssh_stop_vip==$ssh_user|$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();
&start_arp();
$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 \"`;
}
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub start_arp() {
`ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --ssh_user=user --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";
}
(4)创建 MHA 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器
#创建相关目录(所有节点)
mkdir -p /opt/mysql-mha/mha-node
# manager节点
mkdir -p /opt/mysql-mha/mha
#编写配置文件
vim /opt/mysql-mha/mysql_mha.cnf
[server default]
manager_log=/opt/mysql-mha/manager.log
manager_workdir=/opt/mysql-mha/mha
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
user=mha
password=abc123
port=3306
ping_interval=1
remote_workdir=/opt/mysql-mha/mha-node
repl_user=myslave
repl_password=abc123
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.137.102 -s 192.168.137.103
shutdown_script=""
ssh_user=root
[server1]
hostname=192.168.137.101
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.137.102
port=3306
[server3]
hostname=192.168.137.103
port=3306
masterha_check_ssh -conf=/opt/mysql-mha/mysql_mha.cnf
nohup masterha_manager \
--conf=/opt/mysql-mha/mysql_mha.cnf \
--remove_dead_master_conf \
--ignore_last_failover < /dev/null > /var/log/mha_manager.log 2>&1 &
ifconfig
//若要关闭 manager 服务,可以使用如下命令。
masterha_stop --conf=/opt/mysql-mha/mysql_mha.cnf
或者可以直接采用 kill 进程 ID 的方式关闭。
tail -f /opt/mysql-mha/manager.log
#在 Master 节点 mysql1 上停止mysql服务
systemctl stop mysqld
或
pkill -9 mysql
#正常自动切换一次后,MHA 进程会退出。HMA 会自动修改 app1.cnf 文件内容,将宕机的 mysql1 节点删除。查看 mysql2 是否接管 VIP
ifconfig
故障切换备选主库的算法:
1.一般判断从库的是从(position/GTID)判断优劣,数据有差异,最接近于master的slave,成为备选主。
2.数据一致的情况下,按照配置文件顺序,选择备选主库。
3.设定有权重(candidate_master=1),按照权重强制指定备选主。
(1)默认情况下如果一个slave落后master 100M的relay logs的话,即使有权重,也会失效。
(2)如果check_repl_delay=0的话,即使落后很多日志,也强制选择其为备选主。
1.修复mysql
systemctl restart mysqld
2.修复主从
#在现主库服务器 mysql2 查看二进制文件和同步点
show master status;
#在原主库服务器 mysql1 执行同步操作
change master to master_host='192.168.137.102',master_user='myslave',master_password='abc123',master_log_file='mysql-bin.000001',master_log_pos=1745;
start slave;
3.在 manager 节点上修改配置文件app1.cnf(再把这个记录添加进去,因为它检测掉失效时候会自动消失)
vi /opt/mysql-mha/mysql_mha.cnf
......
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.137.101 -s 192.168.137.103
......
[server1]
hostname=192.168.137.102
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.137.101
port=3306
[server3]
hostname=192.168.137.103
port=3306
4.在 manager 节点上启动 MHA
nohup masterha_manager \
--conf=/opt/mysql-mha/mysql_mha.cnf \
--remove_dead_master_conf \
--ignore_last_failover < /dev/null > /var/log/mha_manager.log 2>&1 &
#解决中英字不兼容报错的问题
dos2unix /usr/local/bin/master_ip_failover