6、MHA
MHA含义
什么MHA,master high availability
建立在主从复制基础之上的故障切换的软件系统。
主从复制的单点问题:
当主从复制当中,主服务器发生故障,会自动切换到一台从服务器,然后把从服务器升格为主,继续主从的架构。
主从复制+读写分离+MHA,mysql初始化进行的操作。
relay_log_recovery=1
启用之后,从库崩溃或者重启时,会自动尝试从日志当中恢复。
安装MHA的组件:
NODE MANAGER
node表示:监控每台机器上mysql的状态,传回给manager。
manager:管理节点,控制MHA的状态
操作过程
mysql1 主 31
同步日期
vim /etc/my.cnf
#从数据库同步使用
CREATE USER 'myslave'@'192.168.32.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.32.%';
#manager 使用
CREATE USER 'mha'@'192.168.32.%' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.32.%' WITH GRANT OPTION;
#防止从库通过主机名连接不上主库
CREATE USER 'mha'@'master' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'master';
CREATE USER 'mha'@'slave1' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave1';
CREATE USER 'mha'@'slave2' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave2';
flush privileges;
添加MHA安装包
apt install -y libdbd-mysql-perl \
libconfig-tiny-perl \
liblog-dispatch-perl \
libparallel-forkmanager-perl \
libextutils-cbuilder-perl \
libmodule-install-perl \
make
cd 进去
perl Makefile.PL
make && make install
mysql2 从1----主备 32
#从数据库同步使用
CREATE USER 'myslave'@'192.168.233.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.233.%';
#manager 使用
CREATE USER 'mha'@'192.168.233.%' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.233.%' WITH GRANT OPTION;
#防止从库通过主机名连接不上主库
CREATE USER 'mha'@'master' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'master';
CREATE USER 'mha'@'slave1' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave1';
CREATE USER 'mha'@'slave2' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave2';
flush privileges;
添加MHA安装包
apt install -y libdbd-mysql-perl \
libconfig-tiny-perl \
liblog-dispatch-perl \
libparallel-forkmanager-perl \
libextutils-cbuilder-perl \
libmodule-install-perl \
make
cd 进去
perl Makefile.PL
make && make install
mysql3 从2 32
#从数据库同步使用
CREATE USER 'myslave'@'192.168.233.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.233.%';
#manager 使用
CREATE USER 'mha'@'192.168.233.%' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.233.%' WITH GRANT OPTION;
#防止从库通过主机名连接不上主库
CREATE USER 'mha'@'master' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'master';
CREATE USER 'mha'@'slave1' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave1';
CREATE USER 'mha'@'slave2' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave2';
flush privileges;
添加MHA安装包
apt install -y libdbd-mysql-perl \
libconfig-tiny-perl \
liblog-dispatch-perl \
libparallel-forkmanager-perl \
libextutils-cbuilder-perl \
libmodule-install-perl \
make
cd 进去
perl Makefile.PL
make && make install
nginx MHA的组件 20
添加MHA安装包
apt install -y libdbd-mysql-perl \
libconfig-tiny-perl \
liblog-dispatch-perl \
libparallel-forkmanager-perl \
libextutils-cbuilder-perl \
libmodule-install-perl \
make
cd 进去
perl Makefile.PL
make && make install
添加manager安装包
配置所有节点之间的MHA的ssh通信
免密登入彼此之间的服务器
nginx1 -------mysql 1 2 3
mysql1 -----------mysql 2 3
mysql2-------------mysql 1 3
mysql3 ------------- mysql 1 2
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
清空内容复制脚本
#!/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.233.100';
my $brdc = '192.168.233.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;
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";
}
清空数据复制脚本
[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=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.32.32 -s 192.168.32.33
#从对主监听
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.32.31
#主服务器
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.32.32
#备用主服务器
port=3306
[server3]
hostname=192.168.32.33
#从服务器2
port=3306
mysql1 2 3
nginx1
$str =~ /(\d+)\.(\d+)/;
my $strmajor = "$1.$2";
my $result = sprintf( '%03d%03d', $1, $2 ) if $str =~ m/(\d+)\.(\d+)/;
检查ssh通信状态
masterha_check_ssh -conf=/etc/masterha/app1.cnf
启动
nginx1
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 &
nohup:执行记录,把执行记录保存
检查master节点
masterha_check_status --conf=/etc/masterha/app1.cnf
test1
配置所有节点之间的MHA的ssh通信
故障切换的过程
关闭manager 关闭mysql1
故障恢复
nginx1
mysql1
恢复主从 mysql1为主 mysql1为从
mysql2
nginx1
检查正常后
重新开启
test1