mysql--MHA

什么是MHA

masterhight availabulity:基于主库的高可用环境下:主从复制 故障切换

主从的架构

MHA:最少要一主两从

mysql的单点故障问题,一旦主库崩溃,MHA可以在0-30秒内自动完成故障切换

MHA使用的是半同步复制,只要有一台从服务器写入数据,就会自动提交给客户端

master崩溃,slave就会从主的二进制日志保存文件

slave识别

其他的slave继续和新的master同步

实验准备

1、MHA架构搭建

2、模拟故障

3、故障恢复

MHA manager 20.0.0.10

master 20.0.0.50

slave1 51

slave2 52

node组件----->需要部署在所有服务器上,manager组件依赖node组件,node组件,node组件监控mysql的状态

node组件是依赖ssh通信

所有服务器
关闭防火墙和安全机制
修改mysql服务器的主机名
master slave1 slave2

主从复制
master
vim /etc/my.cnf
log_bin=master -bin
log-slave-updates=ture

slave1
server-id =2
lo_bin=master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index

slave3
server-id=3
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index

所有mysql服务器重启服务
systemctl restart mysqld

所有mysql都要做软连接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

进入mysql数据库
mysql -uroot -p123456
赋权
grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123456';
#manager 使用
grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
#防止从库通过主机名连接不上主库
grant all privileges on *.* to 'mha'@'master' identified by 'manager';				
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
flush privileges;
#在 Master 节点查看二进制文件和同步点
show master status;

在 Slave1、Slave2 节点执行同步操作
change master to master_host='20.0.0.50',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=2741; 

start slave;

在 Slave1、Slave2 节点查看数据同步结果
show slave status\G;
	
//确保 IO 和 SQL 线程都是 Yes,代表同步正常。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

把两个从库设置为只读模式
set  global read_only=1;

测试主从复制
create database test_db;
查看master slave1 slave2是否同步

所有的服务器都要安装MHA的依赖环境,必须有epel源

安装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

安装MHA(先安装node组件,再装MHA)
cd /opt
tar -xf  mha4mysql-node-0.57.tar.gz
perl Makefile.PL
make && make install

在 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 组件安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
masterha_check_ssh 检查 MHA 的 SSH 配置状况
masterha_check_repl 检查 MySQL 复制状况
masterha_manger 启动 manager的脚本
masterha_check_status 检测当前 MHA 运行状态
masterha_master_monitor 检测 master 是否宕机
masterha_master_switch 控制故障转移(自动或者 手动)
masterha_conf_host 添加或删除配置的 server 信息
masterha_stop  关闭manager

node的重要组件
sava_binary_logs:保存和复制master的二进制日志
apply_diff_relay_logs:识别二进制日志当中的差异事件,然后发送给其他的slave
filter_mysqlbinlog:去除不必要的回滚(MHA已经不用了)
purge_relay_logs:同步之后清楚中继日志(不会阻塞sql的线程)

在所有服务器上配置无密码认证
在 manager 节点上配置到所有数据库节点的无密码认证
ssh-keygen -t rsa 				
ssh-copy-id 20.0.0.50
ssh-copy-id 20.0.0.51
ssh-copy-id 20.0.0.52

在 master 上配置到数据库节点 slave1 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 20.0.0.51
ssh-copy-id 20.0.0.52

在 slave1 上配置到数据库节点 master 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 20.0.0.50
ssh-copy-id 20.0.0.52

在 slave2 上配置到数据库节点 master 和 slave1 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 20.0.0.50
ssh-copy-id 20.0.0.51

在 manager 节点上配置 MHA
在 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 				#因故障切换后发送报警的脚本

复制上述的自动切换时 VIP 管理的脚本到 /usr/local/bin 目录,
这里使用master_ip_failover脚本来管理 VIP 和故障切换

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

修改内容如下:(删除原有内容,直接复制并修改vip相关参数)
vim /usr/local/bin/master_ip_failover
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.100';
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;
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 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器
mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha

vim /etc/masterha/app1.cnf						#删除原有内容,直接复制并修改节点服务器的IP地址


[server default]
manager_log=/var/log/masterha/app1/manager.log
#manager的管理日志
manager_workdir=/var/log/masterha/app1
#manager的工作目录
master_binlog_dir=/usr/local/mysql/data
#保存master的二进制日志的位置,必须要和master保存的路径一致
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
#监控主库,发送ping包的时间间隔,1秒,尝试三次之后会切换到failover脚本进行自动切换
remote_workdir=/tmp
#mysql在发生切换时,binlog的保存位置
repl_password=123456
#用户密码
repl_user=myslave
#用户名
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.51 -s 20.0.0.52 
#从对主监听,从和主之间互相监听,主无须声明
shutdown_script=""
#设置切换时,告警的脚本
ssh_user=root
#远程登录的用户名
user=mha

[server1]
hostname=20.0.0.50
#主服务器
port=3306

[server2]
candidate_master=1   
#设置候选的master,主库崩溃会切换到server2
check_repl_delay=0
#即使设置了权重,但是从服务器的relay_logs落后master100M,也不会切换,设置为0忽略延迟复制,直接升为主,强制切换到设定为候选master的服务器
hostname=20.0.0.51  
#备用主服务器
port=3306

[server3]
hostname=20.0.0.52  
#从服务器2
port=3306

第一次配置需要在 Master 节点上手动开启虚拟IP
/sbin/ifconfig ens33:1 20.0.0.100/24

在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示。
masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Nov 14 13:27:38 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Nov 14 13:27:38 2023 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Nov 14 13:27:38 2023 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Nov 14 13:27:38 2023 - [info] Starting SSH connection tests..
Tue Nov 14 13:27:39 2023 - [debug] 
Tue Nov 14 13:27:38 2023 - [debug]  Connecting via SSH from [email protected](20.0.0.50:22) to [email protected](20.0.0.51:22)..
Tue Nov 14 13:27:39 2023 - [debug]   ok.
Tue Nov 14 13:27:39 2023 - [debug]  Connecting via SSH from [email protected](20.0.0.50:22) to [email protected](20.0.0.52:22)..
Tue Nov 14 13:27:39 2023 - [debug]   ok.
Tue Nov 14 13:27:40 2023 - [debug] 
Tue Nov 14 13:27:39 2023 - [debug]  Connecting via SSH from [email protected](20.0.0.51:22) to [email protected](20.0.0.50:22)..
Tue Nov 14 13:27:39 2023 - [debug]   ok.
Tue Nov 14 13:27:39 2023 - [debug]  Connecting via SSH from [email protected](20.0.0.51:22) to [email protected](20.0.0.52:22)..
Tue Nov 14 13:27:40 2023 - [debug]   ok.
Tue Nov 14 13:27:41 2023 - [debug] 
Tue Nov 14 13:27:39 2023 - [debug]  Connecting via SSH from [email protected](20.0.0.52:22) to [email protected](20.0.0.50:22)..
Tue Nov 14 13:27:40 2023 - [debug]   ok.
Tue Nov 14 13:27:40 2023 - [debug]  Connecting via SSH from [email protected](20.0.0.52:22) to [email protected](20.0.0.51:22)..
Tue Nov 14 13:27:40 2023 - [debug]   ok.
Tue Nov 14 13:27:41 2023 - [info] All SSH connection tests passed successfully.

在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常。如下所示。
masterha_check_repl -conf=/etc/masterha/app1.cnf

Tue Nov 14 13:28:00 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Nov 14 13:28:00 2023 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Nov 14 13:28:00 2023 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Nov 14 13:28:00 2023 - [info] MHA::MasterMonitor version 0.57.

...................

Tue Nov 14 13:28:05 2023 - [info] Checking replication health on 20.0.0.51..
Tue Nov 14 13:28:05 2023 - [info]  ok.
Tue Nov 14 13:28:05 2023 - [info] Checking replication health on 20.0.0.52..
Tue Nov 14 13:28:05 2023 - [info]  ok.
Tue Nov 14 13:28:05 2023 - [info] Checking master_ip_failover_script status:
Tue Nov 14 13:28:05 2023 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=20.0.0.50 --orig_master_ip=20.0.0.50 --orig_master_port=3306 


IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.100===

Checking the Status of the script.. OK 
Tue Nov 14 13:28:05 2023 - [info]  OK.
Tue Nov 14 13:28:05 2023 - [warning] shutdown_script is not defined.
Tue Nov 14 13:28:05 2023 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

在 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 &

查看 MHA 状态,可以看到当前的 master 是 master 节点。
masterha_check_status --conf=/etc/masterha/app1.cnf

查看 MHA 日志,也以看到当前的 master 是 192.168.233.21,如下所示。
cat /var/log/masterha/app1/manager.log | grep "current master"

故障模拟
先停止master节点,mysql服务
systemctl stop mysqld
在查看ifconfig
看100飘到slave1上

然后关闭slave1服务
修改master的配置文件
把原来的配置文件修改为
server-id = 1
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

修改slave1配置文件
server-id = 2
log_bin = master-bin
log-slave-updates = true

修改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=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.50 -s 20.0.0.52
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=20.0.0.51
port=3306

[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.50
port=3306


[server3]
hostname=20.0.0.52
port=3306

master和slave1服务器重启服务
systemctl restart mysqld.service

slave1服务器进入mysql
mysql -u root -p123456
查看位置节点
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+

进入master的mysql
mysql -u root -p123456
slave1和master做同步
change master to master_host='20.0.0.51',
  master_user='myslave',master_password='123456',
  master_log_file='master-bin.000002',
  master_log_pos=154; 
 
 reset slave;
 start slave;
show slave status\G;
结果出现两个yes说明主从配置完成
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

测试
在主上slave1创建一个库
CREATE DATABASE xiaobu;
从库上也同样有xiaobu这个库,说明实验成功了

你可能感兴趣的:(mysql)