MySQL的MHA

MySQL的MHA

MHA是什么?

高可用模式下的故障切换,基于主从复制。

单点故障和主从复制不能切换的问题。

至少需要3台。奇数台

故障切换的过程0~30秒。

根据vip地址所在的主机,确定主备。

主和备不是靠优先级决定的,主从复制的时候就确定了主,备是在MHA的过程中确定的。

MHA的组件

MHA NODE 数据节点,每台MySQL和管理服务器都需要安装,监控服务器的状态以及收集数据。

MHA的manager管理节点

管理MySQL的高可用集群

可以单独部署在一台独立的服务器,也可以是多个。

实现主备之间切换。主发生故障,切换到备

MHA特点

1、manager来实 现主备切换

2、数据同步还是依靠二进制日志,最大程度上保证数据的完整

3、半同步的方式,都是为了实现数据的完整性。

支持一主多从的结构,至少要三台

1、主宕机,保存二进制日志

2、备从主的二进制日志当中更新更新到自己的slave日志当中。

3、备成主,同步到master的二进制文件

4、其他备服务器从新的主同步数据

5、原来的备成为主,其他的备服务器都和主继续同步数据。

6、主备切换之后,mysql模式下,一般是继续以现有主作为集群的主,重新把服务器加入到集群。

cd /usr/local/bin里的文件
masterha_check_ssh
所有的数据库节点和管理节点通过ssh来进行通信,检查集群的ssh配置
masterha_check_repl
检查mysql的复制情况数据同步
masterha_manager
manager 文件的启动脚本
masterha_check_status
检查MHA集群状态的文件
masterha_master_switch
控制故障转移
masterha_stop
关闭manager服务

scripts里的文件
master_jip _failover 自动故障切换时,vp的管理脚本
master_ip_online_change 在线切换时vip的管理脚本
power_manager 故障发生后,关闭主机的脚本
send_report 故障切换之后,发送报警的脚本
[server default]
manager log=/var/log/masterha/appl/manager.log
#主日志文件,报错就看他
manager workdir=/var/log/masterha/app1
#manager的工作目录
master binlog dir=/usr/local/mysal/data#mysql主服务器的binlog二进制文件的保存目录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主库的时间间隔,默认是3秒,我们设置成1秒,ping3次。三次不通就会自动切换
​
[serverl]
hostname=192.168.233.21
#主服务器
port=3306
[server2]
candidate_master=1
#设置为备用master
check_repl_delay=0
#默认是选择一个slave,这个slave和master的数据是最一致的。如果slave
#的同步数据落后主100M,MHA永远也不会选择该服务器做为备主。不考虑主从之间延迟的问题,强制指定slave为备
hostname=192.168.233.22
#备用主服务器
port=3306
[server3]
hostname=192.168.233.23
#从服务器2
port=3306
nohup masterha manager -.conf=/etc/masterha/app1.cnf .remove_dead master conf -ignore last failover < /dev/null >Nar/log/masterharappl/managerlog 2>&1 &
nghyp 记录程序启动时的日志文件,保存到指定的位置.

四台机器

master 11 node组件

slave1 12 node组件

slave2 13 node组件

管理节点 41 manager

1、先改主机名

[root@mysql1 opt]# hostnamectl set-hostname master
[root@mysql1 opt]# su
[root@mysql2 opt]# hostnamectl set-hostname slave1
[root@mysql2 opt]# su
[root@mysql3 opt]# hostnamectl set-hostname slave2
[root@mysql3 opt]# su
​

2、修改MySQL配置文件

[root@master opt]# vim /etc/my.cnf
server-id = 1
binlog_format=MIXED
log_bin = master-bin
log-slave-updates = true
relay_log_recovery=1
​
[root@slave1 opt]# vim /etc/my.cnf
server-id = 2
log-bin=master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
relay_log_recovery=1
​
[root@slave2 opt]# vim /etc/my.cnf
server-id = 3
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
relay_log_recovery=1

3、进入数据库,创建用户

5.配置 mysql 一主两从
(1)所有数据库节点进行 mysql 授权
mysql -uroot -p123456
​
#从数据库同步使用
CREATE USER 'myslave'@'192.168.65.%' IDENTIFIED WITH mysql_native_password BY '123456';
​
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.65.%';         
    
#manager 使用
CREATE USER 'mha'@'192.168.65.%' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.65.%' 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;
​
(2)在 Master 节点查看二进制文件和同步点
show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |    1747  |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
​
(3)在 Slave1、Slave2 节点执行同步操作
change master to master_host='192.168.65.11',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1747; 
​
start slave;
​
(4)在 Slave1、Slave2 节点查看数据同步结果
show slave status\G;
    
//确保 IO 和 SQL 线程都是 Yes,代表同步正常。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
​
(5)两个从库必须设置为只读模式:
set global read_only=1;

4、把从服务器设置成只读模式

mysql2> set global read_only=1;
​
mysql3> set global read_only=1;

5、安装依赖环境和软件

(1)所有服务器上都安装 MHA 依赖的环境,首先安装 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
​
(2)安装 MHA 软件包,先在所有服务器上必须先安装 node 组件
对于每个操作系统版本不一样,这里 CentOS7.6选择 0.57 版本。
在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,
因为 manager 依赖 node 组件。
cd /opt
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
​
(3)在 MHA manager 节点上安装 manager 组件
cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install

6、在所有服务器上配置无密码认证

(1)在 manager 节点上配置到所有数据库节点的无密码认证
ssh-keygen -t rsa               #一路按回车键
ssh-copy-id 192.168.65.11
ssh-copy-id 192.168.65.12
ssh-copy-id 192.168.65.13
​
(2)在 master 上配置到数据库节点 slave1 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.65.12
ssh-copy-id 192.168.65.13
​
(3)在 slave1 上配置到数据库节点 master 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.65.11
ssh-copy-id 192.168.65.13
​
(4)在 slave2 上配置到数据库节点 master 和 slave1 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.65.11
ssh-copy-id 192.168.65.12

7、在 manager 节点上配置 MHA

(1)在 manager 节点上复制相关脚本到/usr/local/bin 目录
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
(2)复制上述的自动切换时 VIP 管理的脚本到 /usr/local/bin 目录,
这里使用master_ip_failover脚本来管理 VIP 和故障切换
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
(3)修改内容如下:(删除原有内容,直接复制并修改vip相关参数)
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.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";
}
​
(4)创建 MHA 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器
mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
​
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 192.168.65.12 -s 192.168.65.13 
#从对主监听
shutdown_script=""
ssh_user=root
user=mha
​
[server1]
hostname=192.168.65.11 
#主服务器
port=3306
​
[server2]
candidate_master=1   
check_repl_delay=0
hostname=192.168.65.12  
#备用主服务器
port=3306
​
[server3]
hostname=192.168.65.13  
#从服务器2
port=3306

8、第一次配置需要在 Master 节点上手动开启虚拟IP

/sbin/ifconfig ens33:1 192.168.233.100/24

9、在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示。

masterha_check_ssh -conf=/etc/masterha/app1.cnf
​
Tue Nov 26 23:09:47 2020 - [debug]   ok.
Tue Nov 26 23:09:47 2020 - [info] All SSH connection tests passed successfully.

10、在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常。如下所示。

masterha_check_repl -conf=/etc/masterha/app1.cnf
​
MySQL Replication Health is OK.

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

12、查看 MHA 状态,可以看到当前的 master 是 master 节点。

masterha_check_status --conf=/etc/masterha/app1.cnf

13、查看 MHA 日志,也以看到当前的 master 是 192.168.65.11,如下所示。

cat /var/log/masterha/app1/manager.log | grep "current master"

14、查看master 的 VIP 地址 192.168.65.100 是否存在,这个 VIP 地址不会因为 manager 节点停止 MHA 服务而消失。

ifconfig

1

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值