一、MHA工作原理和架构
- MHA利用 SELECT 1 As Value 指令判断master服务器的健康性,一旦master 宕机,MHA 从宕机崩溃的master保存二进制日志事件(binlog events)
- 识别含有最新更新的slave
- 应用差异的中继日志(relay log)到其他的slave
- 应用从master保存的二进制日志事件(binlog events)
- 提升一个slave为新的master
- 使其他的slave连接新的master进行复制
二、实现mha
mha服务器: 192.168.91.103
master: 192.168.91.100
slave1: 192.168.91.101
slave2: 192.168.91.102
1.mha服务器安装mha,主服务器,从服务器安装mha
####################主节点需要安装两个###############
yum install epel-release.noarch -y
#有依赖性用yum安装 需要先安装 epel源
yum -y install mha4mysql-*.rpm
################从节点仅安装node包################
yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
##############主节点上 基于key验证:#####################
ssh-keygen
cd
ssh-copy-id 127.0.0.1
#自己和自己实现免密钥登录
rsync -a .ssh 192.168.91.100:/root/
rsync -a .ssh 192.168.91.101:/root/
rsync -a .ssh 192.168.91.102:/root/
#注意.ssh 后不能加/
2.建立mha 文件夹 和配置文件
mkdir /etc/mastermha
vim /etc/mastermha/app1.cnf
[server default]
user=mhauser
password=Admin@123
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=test
repl_password=Admin@123
ping_interval=1
master_ip_failover_script=/usr/local/bin/master_ip_failover
#report_script=/usr/local/bin/sendmail.sh 可以不加
check_repl_delay=0
master_binlog_dir=/data/mysql/
[server1]
hostname=192.168.91.100
candidate_master=1
[server2]
hostname=192.168.91.101
candidate_master=1
[server3]
hostname=192.168.91.102
#实现主节点ip切换的脚本
vim master_ip_failove
#!/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.91.188/24'; #设置Virtual IP 此处注释需要删除 *****
my $gateway = '192.168.91.2'; #网关Gateway IP 此处注释需要删除 *****
my $interface = 'ens33';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
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" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
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" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
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";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
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";
}
mv master_ip_failover /usr/local/bin/
chmod +x /usr/local/bin/master_ip_failover
准备主从复制
vim /etc/my.cnf
#修改文件
[mysqld]
server_id=100
log-bin=/data/mysql/mysql-bin
skip_name_resolve=1
general_log
mkdir /data/mysql/ -p
#建立文件夹
chown mysql.mysql /data/ -R
#注意修改权限
systemctl restart mysqld
grant replication slave on *.* to test@'192.168.91.%' identified by 'Admin@123';
#建立复制用户
grant all on *.* to mhauser@'192.168.91.%' identified by 'Admin@123';
#建立 mha管理账户
show processlist;
#查看线程
vim /etc/my.cnf
#修改文件
[mysqld]
server_id=102
log-bin=/data/mysql/mysql-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
skip_name_resolve=1
general_log
#read only #只读可加
#skip_name_resolve=1 禁用反向解析
mkdir /data/mysql/ -p
#建立文件夹
chown mysql.mysql /data/ -R
#注意修改权限
systemctl restart mysqld
CHANGE MASTER TO
MASTER_HOST='192.168.91.100',
MASTER_USER='test',
MASTER_PASSWORD='Admin@123',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
show slave status\G;
#查看设置的状态
start slave;
#开启线程,开启主从复制
配置虚拟ip 在mysql 主节点上
ifconfig ens33:1 192.168.91.188/24
检查 mha 环境
masterha_check_ssh --conf=/etc/mastermha/app1.cnf
#connection tests passed successfully.
masterha_check_repl --conf=/etc/mastermha/app1.cnf
开启mha
#开启MHA,默认是前台运行,生产环境一般为后台执行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null
#非后台
masterha_manager --conf=/etc/mastermha/app1.cnf
#查看状态
masterha_check_status --conf=/etc/mastermha/app1.cnf
测试实验是否成功
在主服务器上可以看到主服务器不停地给mha服务器发送心跳线
tail -f /var/lib/mysql/localhost.log
在主服务器上出现故障后,在mha服务器日志里可以看到mha已经成功切换从服务器为主
tail -f /data/mastermha/app1/manager.log