MHA高可用


一、MHA

1.MHA概述

传统MySQL主从架构存在单点故障的问题

—套优秀的MySQL高可用环境下故障切换和主从复制的软件MHA的出现就是解决MySQL单点的问题。
MySQL故障过程中,MHA能做到0-30秒内自动完成故障切换
MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用

MHA的组成:
MHA Manager(管理节点)
MHA Node(数据节点)

MHA特点:
自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险
目前MHA支持一主多从架构,最少三台服务,即一主两从

MHA原理:
1.从宕机崩溃的master保存二进制日志事件(binlog events)
2.识别含有最新更新的slave
3.应用差异的中继日志(relay log)到其他的slave
4.应用从master保存的二进制日志事件(binlog events)
5.提升一个slave为新的master
6.使其他的slave连接新的master进行复制

二、主从同步

实验环境
主服务器:192.168.47.100
从服务器:
192.168.47.102
192.168.47.103
manager 节点:192.168.47.104

主服务器

更改主机名

[root@localhost ~]# hostnamectl set-hostname mysql1
[root@localhost ~]# su        
[root@mysql1 ~]# vim /etc/hosts
192.168.47.100 Mysql1
192.168.47.102 Mysql2
192.168.47.103 Mysql3

修改mysql配置文件

[root@mysql1 ~]# vim /etc/my.cnf
log_bin = master-bin
log-slave-updates = true
[root@mysql1 ~]# systemctl restart mysqld.service

设置软连接,每台服务器都要设置

[root@mysql1 ~]# ln -s /usr/local/mysql/bin/{mysql,mysqlbinlog} /usr/sbin/

数据库配置

[root@mysql1 ~]# mysql -uroot -p123123
(root@localhost) [(none)]> grant replication slave on *.* to 'myslave'@'192.168.47.%' identified by '123123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'192.168.47.%' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql2' identiffied by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql3' identiffied by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1745 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从服务器一

更改主机名

[root@localhost ~]# hostnamectl set-hostname mysql2
[root@localhost ~]# su        
[root@mysql1 ~]# vim /etc/hosts
192.168.47.100 Mysql1
192.168.47.102 Mysql2
192.168.47.103 Mysql3

修改mysql配置文件

[root@mysql1 ~]# vim /etc/my.cnf
server-id = 2 
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@mysql1 ~]# systemctl restart mysqld.service

设置软连接

[root@mysql1 ~]# ln -s /usr/local/mysql/bin/{mysql,mysqlbinlog} /usr/sbin/

数据库配置

[root@mysql1 ~]# mysql -uroot -p123123
(root@localhost) [(none)]> grant replication slave on *.* to 'myslave'@'192.168.47.%' identified by '123123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'192.168.47.%' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql2' identiffied by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql3' identiffied by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> change master to master_host='192.168.47.100',master_user='myslave',master_password='123123',master_log_file='master-bin.000001',master_log_pos=1745;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

(root@localhost) [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> show slave status\G
##看到这两个是yes说明配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

设置只读模式

(root@localhost) [(none)]> set global read_only=1;

从服务器二

更改主机名

[root@localhost ~]# hostnamectl set-hostname mysql3
[root@localhost ~]# su        
[root@mysql1 ~]# vim /etc/hosts
192.168.47.100 Mysql1
192.168.47.102 Mysql2
192.168.47.103 Mysql3

修改mysql配置文件

[root@mysql1 ~]# vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@mysql1 ~]# systemctl restart mysqld.service

设置软连接

[root@mysql1 ~]# ln -s /usr/local/mysql/bin/{mysql,mysqlbinlog} /usr/sbin/

数据库配置

[root@mysql1 ~]# mysql -uroot -p123123
(root@localhost) [(none)]> grant replication slave on *.* to 'myslave'@'192.168.47.%' identified by '123123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'192.168.47.%' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql2' identiffied by 'man
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> grant all privileges on *.* to 'mha'@'Mysql3' identiffied by 'manager';
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@localhost) [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> change master to master_host='192.168.47.100',master_user='myslave',master_password='123123',master_log_file='master-bin.000001',master_log_pos=1745;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

(root@localhost) [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> show slave status\G
##看到这两个是yes说明配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

设置只读模式

(root@localhost) [(none)]> set global read_only=1;

测试

主服务器
在这里插入图片描述
从服务器
在这里插入图片描述
在这里插入图片描述

三、MHA安装与配置

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

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 组件
对于每个操作系统版本不一样,这里 CentOS7.6选择 0.57 版本。
在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。

[root@localhost ~]# cd /opt/
[root@localhost opt]# tar zxf mha4mysql-node-0.57.tar.gz
[root@localhost opt]# cd mha4mysql-node-0.57/
[root@localhost mha4mysql-node-0.57]# ls
AUTHORS  COPYING  inc  Makefile.PL  META.yml  rpm
bin      debian   lib  MANIFEST     README    t
[root@localhost mha4mysql-node-0.57]# perl Makefile.PL
[root@localhost mha4mysql-node-0.57]# make && make install

MHA-manager 节点

[root@localhost opt]# tar zxf mha4mysql-manager-0.57.tar.gz 
[root@localhost opt]# cd mha4mysql-manager-0.57/
[root@localhost mha4mysql-manager-0.57]# perl Makefile.PL 
[root@localhost mha4mysql-manager-0.57]# make && make install

在所有服务器上设置ssh无密码认证

manager 节点

[root@localhost bin]# ssh-keygen -t rsa
[root@localhost bin]# ssh-copy-id 192.168.47.100
[root@localhost bin]# ssh-copy-id 192.168.47.102
[root@localhost bin]# ssh-copy-id 192.168.47.103

mysql1

[root@localhost bin]# ssh-keygen -t rsa
[root@localhost bin]# ssh-copy-id 192.168.47.102
[root@localhost bin]# ssh-copy-id 192.168.47.103

mysql2

[root@localhost bin]# ssh-keygen -t rsa
[root@localhost bin]# ssh-copy-id 192.168.47.100
[root@localhost bin]# ssh-copy-id 192.168.47.103

mysql3

[root@localhost bin]# ssh-keygen -t rsa
[root@localhost bin]# ssh-copy-id 192.168.47.100
[root@localhost bin]# ssh-copy-id 192.168.47.102

manager 节点
[root@localhost bin]# cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
[root@localhost bin]# cd /usr/local/bin
[root@localhost bin]# cp scripts/* ./
将以下代码替换文件中内容,IP地址和网卡根据自己电脑更改
[root@localhost bin]# vim 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.47.188';									
my $brdc = '192.168.47.255';								
my $ifdev = 'ens37';										
my $key = '1';												
my $ssh_start_vip = "/sbin/ifconfig ens37:$key $vip";		
my $ssh_stop_vip = "/sbin/ifconfig ens37:$key down";		
my $exit_code = 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";
}

复制进去后行首会有一个#,输入:2,87 s/^#//

更改文件位置并修改内容

[root@localhost bin]# cd /opt/mha4mysql-manager-0.57/samples/conf/
[root@localhost conf]# ls
app1.cnf  masterha_default.cnf
[root@localhost conf]# mkdir /etc/masterha
[root@localhost conf]# cp app1.cnf /etc/masterha/
[root@localhost conf]# 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.47.102 -s 192.168.47.103
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.47.100
port=3306

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

[server3]
hostname=192.168.47.103
port=3306

在主服务器上开启虚拟ip

[root@mysql1 mha4mysql-node-0.57]# ifconfig ens33:1 192.168.47.188/24

manager 节点服务器上验证

[root@localhost conf]# masterha_check_ssh -conf=/etc/masterha/app1.cnf

最后一行显示成功

Mon Dec  6 18:40:47 2021 - [info] All SSH connection tests passed successfully
[root@localhost conf]# masterha_check_repl -conf=/etc/masterha/app1.cnf

最后一行显示以下内容则为成功

MySQL Replication Health is OK.

开启服务

[root@localhost app1]# 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 &

查看状态

cat /var/log/masterha/app1/manager.log | grep "current master"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值