MySQL -MHA高可用配置及故障切换
准备
三台做主从复制的安装MySQL数据库主机,一台要安装MHA的主机( MHA-manager)
从这些实验要求可以看出数据库必须都要做主从复制。
实验
(1)所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源
[root@localhost ~] # yum install -y epel-release
[root@localhost ~] # 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 软件包对于每个操作系统版本不一样,这里 CentOS7.3 必须选择 0.57 版本。 在所有服务器上必须先安装 node 组件,例如
[root@localhost ~] # tar zxvf mha4mysql-node-0.57.tar.gz
[root@localhost ~] # cd mha4mysql-node-0.57
[root@localhost mha4mysql-node-0.57]# perl Makefile.PL
[root@localhost mha4mysql-node-0.57]# make
[root@localhost mha4mysql-node-0.57]# make install
[root@localhost mha4mysql-node-0.57]#ls /usr/local/bin
save_binary_logs。
apply_diff_relay_logs
filter_mysqlbinlog
purge_relay_logs
(3)在 MHA-manager 上安装 manager 组件
[root@MHA-manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz
[root@MHA-manager ~]# cd mha4mysql-manager-0.57
[root@MHA-manager mha4mysql-manager-0.57]# perl Makefile.PL
[root@MHA-manager mha4mysql-manager-0.57]# make
[root@MHA-manager mha4mysql-manager-0.57]# make install
[root@MHA-manager mha4mysql-manager-0.57ls /usr/local/bin
masterha_check_ssh
masterha_check_repl
masterha_manger
masterha_check_status
masterha_master_monitor
masterha_master_switch
masterha_conf_host
在 manager 上配置到所有节点的无密码免交互
[root@MHA-manager ~]# hostnamectl set-hostname MHA-manager
[root@MHA-manager ~]# ssh-keygen -t rsa ###一路按回车键
[root@MHA-manager ~]# ssh-copy-id 20.0.0.5
[root@MHA-manager ~]# ssh-copy-id 20.0.0.6
[root@MHA-manager ~]# ssh-copy-id 20.0.0.7
在 MySQL1 上配置到数据节点的无密码免交互
[root@root ~]# hostnamectl set-hostname Mysql1
[root@Mysql1 ~]# ssh-keygen -t rsa ###一路按回车键
[root@Mysql1 ~]# ssh-copy-id 20.0.0.6
[root@Mysql1 ~]# ssh-copy-id 20.0.0.7
在 MySQL2 上配置到数据节点的无密码免交互
[root@root ~]# hostnamectl set-hostname Mysql2
[root@Mysql2 ~]# ssh-keygen -t rsa ###一路按回车键
[root@Mysql2 ~]# ssh-copy-id 20.0.0.5
[root@Mysql2 ~]# ssh-copy-id 20.0.0.7
在 MySQL2 上配置到数据节点的无密码免交互
[root@root ~]# hostnamectl set-hostname Mysql3
[root@Mysql3 ~]# ssh-keygen -t rsa ###一路按回车键
[root@Mysql3 ~]# ssh-copy-id 20.0.0.5
[root@Mysql3 ~]# ssh-copy-id 20.0.0.6
在 manager 节点上复制相关脚本到/usr/local/bin 目录
[root@mha-manager ~]# ll /opt/mha4mysql-manager-0.57/samples/scripts/
-rwxr-xr-x 1 mysql mysql 3648 5 月 31 2015 master_ip_failover
-rwxr-xr-x 1 mysql mysql 9872 5 月 25 09:07 master_ip_online_change
-rwxr-xr-x 1 mysql mysql 11867 5 月 31 2015 power_manager
-rwxr-xr-x 1 mysql mysql 1360 5 月 31 2015 send_report
[root@MHA-manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@MHA-manager ~]#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 ($@) {
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 软件目录并拷贝配置文件。
4. [root@MHA-manager ~]# mkdir /etc/masterha
[root@MHA-manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@MHA-manager ~]# 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.6 -s 20.0.0.7
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.5
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.6
port=3306
[server3]
hostname=20.0.0.7
port=3306
- 测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示。
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 20.0.0.200/24
[root@Mysql1 ~]# ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.255.255.0 broadcast 20.0.0.255
ether 00:0c:29:00:62:91 txqueuelen 1000 (Ethernet)
[root@MHA-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:09:45 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Nov 26 23:09:45 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Nov 26 23:09:45 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Nov 26 23:09:45 2019 - [info] Starting SSH connection tests..
Tue Nov 26 23:09:46 2019 - [debug]
Tue Nov 26 23:09:45 2019 - [debug] Connecting via SSH from root@20.0.0.6(20.0.0.6:22) to root@20.0.0.7(20.0.0.7:22)..
Tue Nov 26 23:09:46 2019 - [debug] ok.
Tue Nov 26 23:09:47 2019 - [debug]
Tue Nov 26 23:09:46 2019 - [debug] Connecting via SSH from root@20.0.0.7(20.0.0.7:22) to root@20.0.0.6(20.0.0.6:22)..
Tue Nov 26 23:09:47 2019 - [debug] ok.
Tue Nov 26 23:09:47 2019 - [info] All SSH connection tests passed successfully.
[root@MHA-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:10:29 2019 - [info] Slaves settings check done.
Tue Nov 26 23:10:29 2019 - [info]
20.0.0.620.0.0.6:3306) (current master)
+--20.0.0.7(20.0.0.7:3306)
Tue Nov 26 23:10:29 2019 - [info] Checking replication health on 20.0.0.6..
Tue Nov 26 23:10:29 2019 - [info] ok.
Tue Nov 26 23:10:29 2019 - [info] Checking master_ip_failover_script status:
Tue Nov 26 23:10:29 2019 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=20.0.0.5 --orig_master_ip=200.0.0.6 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===
Checking the Status of the script.. OK
Tue Nov 26 23:10:29 2019 - [info] OK.
Tue Nov 26 23:10:29 2019 - [warning] shutdown_script is not defined.
Tue Nov 26 23:10:29 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
6.启动 MHA
[root@MHA-manager ~]# 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] 129929
7.查看 MHA 状态,可以看到当前的 master 是 Mysql1 节点。
[root@MHA-manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:7763) is running(0:PING_OK), master:20.0.0.5
[root@Mysql1 ~]#systemctl stop mysqld
[root@mysql1 ~]# ifconfig
会发现master上的ens33:1/接口不见了,manager上的监控日志发生了变化,等待一会就能看到 VIP地址漂移到了从服务器20.0.0.6上,去从服务器20.0.0.6上,输入 ifconfig 会先发 vip地址就在上面,如此就完成了地址漂移
[root@mysql2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 20.0.0.6 netmask 255.255.255.0 broadcast 20.0.0.255 inet6 fe80::68ab:77b8:dfb0:9cf2 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:da:d4:93 txqueuelen 1000 (Ethernet) RX packets 87633 bytes 92667957 (88.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 40292 bytes 10435831 (9.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255 ether 00:0c:29:da:d4:93 txqueuelen 1000 (Ethernet)
在原先的master服务器上 systemctl start mysqld 启动数据库,然后进入 manager 中在配置文件 /etc/masterha/app1.con 中添加 [server1]段,由于manager中有移除挂掉的master的模块, 所以需要重新添加 [server1]段 ,添加如下:
[server1] hostname=20.0.0.5 port=3306
2、在master数据库中添加一条重新定位的语句 show master status;
###查看20.0.0.6 新master的定位值;并在20.0.0.5插入下面内容。要和master 的定位置,日志文件名相同 +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000003 | 1589 | | | | +-------------------+----------+--------------+------------------+-------------------+
change master to master_host='20.0.0.6',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=1589; start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event
Master_Host: 20.0.0.106
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 1189
Relay_Log_File: mysql1-relay-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这是MHA的配置步骤和故障模拟以及修复。