文章目录
MHA的作用
MHA是一套优秀的MySQL高可用环境下故障切换和主从复制的软件
MHA的出现就是解决MySQL单点的问题
MySQL故障过程中,MHA能做到0-30秒内自动完成故障切换操作
MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用
MHA的组成:MHA Node (数据节点);MHA Manager ( 管理节点)
环境
MHA Manager 192.168.43.11 MHA Node 组件、MHA Manager 组件
Master 192.168.64.12 MHAnode 组件
Slave1 192.168.64.13 MHAnode 组件
Slave2 192.168.64.14 MHAnode 组件
实验步骤
1先修改Master、Slave1、Slave2 的主机名
修该Master、Slave1、Slave2 的主配置文件 /etc/my.cnf
Master
slave1
slave2
配置 MySQL 一主两从
mysql -uroot -p123456
#登录 MySQL 数据库
grant replication slave on *.* to 'myslave'@'192.168.43.%' identified by '123456';
#从数据库同步使用
grant all privileges on *.* to 'mha'@'192.168.43.%' identified by 'manager';
#manager 使用
grant all privileges on *.* to 'mha'@'mysql1' identified by 'manager';
grant all privileges on *.* to 'mha'@'mysql2' identified by 'manager';
grant all privileges on *.* to 'mha'@'mysql3' identified by 'manager';
#防止从库通过主机名连接不上主库
flush privileges;
Slave1、Slave2
change master to master_host='192.168.43.11',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=xxxx;
#指定 master 服务器 IP,用户名及密码,日志名和偏移量
start slave;
#启动同步
show slave status\G" | awk '/Running:/{print}
#查看从服务器线程相关参数信息
#确保 IO 和 SQL 线程都是 Yes,代表同步正常
mysql -e "set global read_only=1;"
#两个从库必须设置为只读模式
安装 MHA
安装 node 组件
cd /opt
#将 node 放至 opt 目录下
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 传至 opt 目录下
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
Manager 节点上配置 MHA
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 = '192.168.64.200'; #指定vip的地址
my $brdc = '192.168.64.255'; #指定vip的广播地址
my $ifdev = 'ens33'; #指定vip绑定的网卡
my $key = '1'; #指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #代表此变量值为ifconfig ens33:1 192.168.126.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #代表此变量值为ifconfig ens33:1 192.168.126.200 down
my $exit_code = 0; #指定退出状态码为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";
}
app1.cnf配置文件来管理 mysql 节点服务器
mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
#删除原有内容,直接复制并修改节点服务器的 IP 地址
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=123123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.64.130 -s 192.168.64.131
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.64.129
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.64.130
port=3306
[server3]
hostname=192.168.64.131
port=3306
Master 节点上手动开启虚拟IP
ifconfig ens33:1 192.168.64.200/24
在 Manager 节点上启动 MHA
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 &
故障修复步骤
修复修复主从
#在现主库服务器 Mysql2 查看二进制文件和同步点
show master status;
#在原主库服务器 mysql1 执行同步操作
change master to master_host='192.168.121.16',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1745;
start slave;
在 manager 节点上修改配置文件app1.cnf(再把这个记录添加进去,因为它检测掉失效时候会自动消失)
vi /etc/masterha/app1.cnf
......
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.121.14 -s 192.168.121.17
......
[server1]
hostname=192.168.80.16
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.80.14
port=3306
[server3]
hostname=192.168.80.17
port=3306
在 manager 节点上启动 MHA
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 &
#解决中英字不兼容报错的问题
dos2unix /usr/local/bin/master_ip_failover