数据库Mysql-----搭建MHA高可用,故障切换!!!

一,MHA概述

  • 一套优秀的Mysql高可用环境下故障切换和主从复制的软件
  • Mysql故障过程中,MHA能够做到0-30秒内自动完成故障切换

组成

  • MHA Manager (管理节点)
  • MHA Node (数据节点)

特点

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

优势

虽然MHA试图从宕机的主服务器上保存bin-log日志,但并不总是可行的,例如:如果主服务器硬件故障或无法通过ssh访问,MHA没法保存bin-log日志,就会出现只进行故障转义但丢失了最新的数据的情况。使用半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave 已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此保证所有节点的数据一致性。

二,搭建MHA

准备环境

主机 操作系统 ip地址 作用
服务器 CentOS7.6(64 位) Mysql1/20.0.0.25 Master 节点,安装 node 组件
服务器 CentOS7.6(64 位) Mysql2/20.0.0.26 Slave 节点,安装 node 组件
服务器 CentOS7.6(64 位) Mysql3/20.0.0.27 Slave 节点,安装 node 组件
服务器 CentOS7.6(64 位) MHA-manager/20.0.0.28 管理节点,安装 node 组件,安装 manager 组件

2.1 mysql已安装,主从复制已搭建!

安装mysql 我的博客地址
搭建主从复制 我的博客地址

2.2 主从复制数据库授权


(注:主从复制已经搭建好了的话,只需要做下授权,设置从库只读就行了!!!!!)
(注:未搭建好主从复制,搭建的时候,按照正常操作流程,授权记得添加就行,)

//主备服务器需要主配置文件内也开启二进制日志;
slave1:  vi /etc/my.cnf    [mysqld]配置段内 log-bin = master-bin  重启服务;

(1) 在所有数据库(主从服务器三个服务器上都要)节点上授权两个用户,一个是从数据库同步使用,另外一个是 manager 使用。

mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123456';
mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';

下面三条授权按理论是不用添加的,但是在做案例实验时通过 MHA 检查 MySQL 主从 有报错,报两个从库通过主机名连接不上主库,所以三个数据库都添加下面的授权。
mysql>grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager'; 
mysql>grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager'; 
mysql>grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';
 

(2)注意:必须设置两个从库为只读模式,在两个从库上分别执行 

进入数据库;

mysql> set global read_only=1;

2.3 所有节点安装node (数据节点)和MHA依赖环境

(1)所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源(本地源不行!!)
(现网源,要可以上网)

yum install -y epel-release
yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

(2)所有节点安装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

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

2.4 所有节点配置SSH免交互

在 manager 上配置到所有节点的无密码免交互

hostnamectl  set-hostname MHA-manager
bash
ssh-keygen -t rsa     	###一路按回车键,免交互不需要回车就行
ssh-copy-id 20.0.0.25    
ssh-copy-id 20.0.0.26
ssh-copy-id 20.0.0.27
在 MySQL1 主服务器上配置到数据节点的无密码免交互
hostnamectl  set-hostname Mysql1
bash
ssh-keygen -t rsa 	###一路按回车键,免交互不需要回车就行
ssh-copy-id 20.0.0.25
ssh-copy-id 20.0.0.26
ssh-copy-id 20.0.0.27
在 MySQL2 从服务器上配置到数据节点的无密码免交互
hostnamectl  set-hostname Mysql2
bash
ssh-keygen -t rsa 	###一路按回车键,免交互不需要回车就行
ssh-copy-id 20.0.0.25
ssh-copy-id 20.0.0.26
ssh-copy-id 20.0.0.27
在 MySQL3 从服务器上配置到数据节点的无密码免交互
hostnamectl  set-hostname Mysql3
bash
ssh-keygen -t rsa 	###一路按回车键,免交互不需要回车就行
ssh-copy-id 20.0.0.25
ssh-copy-id 20.0.0.26
ssh-copy-id 20.0.0.27

2.5 所有数据库节点配置软连接,路径优化

ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

2.6 配置 MHA

1.在 manager 节点上复制相关脚本到/usr/local/bin 目录

cp /opt/mha4mysql-manager-0.57/samples/scripts/*     /usr/local/bin

2.使用下面内容完整替换master_ip_failover文件的内容,

//备份原有配置后,所有内容删除,使用下面脚本
vi /usr/local/bin/master_ip_failover

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;

my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);

my $vip = '20.0.0.200';        #需要注意vip地址根据自己情况定义,还有网卡部分根据节点(服务器的)网卡配置!!我定义的是20.0.0.200
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;
#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";
}

3.创建 MHA 软件目录并拷贝配置文件

mkdir /etc/masterha                 #在/etc下创建软件目录

cp /opt/mha4msql-manager-0.57/samples/conf/app1.cnf      /etc/masterha     #cp到软件目录下

mkdir -p /var/log/masterha/app1               #创建manger工作存放目录

编辑配置文件app1.cnf

vi   /etc/masterha/app1.cnf                     
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
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
user=mha						    ###MySQL 监控用户
password=manager					###MySQL 监控用户的密码
ping_interval=1 					###ping 包时间间隔
remote_workdir=/tmp
repl_user=myslave					###主从复制用户
repl_password=123456 				###主从复制用户的密码
secondary_check_script= /usr/local/bin/masterha_secondary_check -s 20.0.0.26 -s 20.0.0.27
shutdown_script=""
ssh_user=root 					    ###SSH 登录用户名

[server1]
hostname=20.0.0.25
port=3306

[server2]
hostname=20.0.0.26
port=3306
candidate_master=1                  #设置候选的master
check_repl_delay=0                  #MHA只会选择数据保存完整度高的从服务器作为主的服务器!!!这样更保证数据的完整性!!!!

[server3]
hostname=20.0.0.27
port=3306

4.测试 ssh 无密码认证,如果正常最后会输出 successfully

注:在做测试主从连接时,最好先对从服务器配置做下修改!

####### 做以下添加 ##########
############################
在从服务器上:vi /etc/my.cnf
在[client]参数下注释 #default-character-set=utf8
字符集可能也是会影响到,给注释掉

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

5.测试 mysq 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常

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

2.7 在主服务器Mysql1添加vip地址

(注释:)首次配置 MHA 的 VIP 地址需要手动进行配置,在 Mysql1 上执行如下命令
VIP 地址不会因为 manager 节点停止 MHA 服务而消失

ifconfig ens33:1 20.0.0.200
ifconfig ens33:1
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:da:8c:3c  txqueuelen 1000  (Ethernet)

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

2.9 查看 MHA 状态

当前的 master 是 Mysql1 节点。

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

app1 (pid:7763) is running(0:PING_OK), master:20.0.0.25

注:有时服务正在开启,会显示稍等片刻的显示,等会在查看,就会显示!!!!!

关闭MHA的命令

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

2.10 查看 MHA 日志文件

在这里插入代码片cat /var/log/masterha/app1/manager.log
.....省略部分.........
From:
20.0.0.25(20.0.0.25:3306) (current master)
 +--20.0.0.26(20.0.0.26:3306)
 +--20.0.0.27(20.0.0.27:3306)
.........省略部分...............```





三,模拟master主数据库服务器故障

自动切换

(1)在manger MHA服务器先动态的查看下日志文件

 tail -f /var/log/masterha/app1/manager.log  

(2)在主服务器停掉mysqld服务或结束进程

第一种:pkill -9 mysql
第二种:systemctl stop mysqld 	



(3)在MHA服务器上查看刚才日志文件是否故障转换

显示:主故障转移到20.0.0.26转移成功!
Master failover to 20.0.0.26(20.0.0.26:3306) completed successfully.


(4)在从服务器20.0.0.26上查看vip是否被接管!

ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.26  netmask 255.0.0.0  broadcast 20.255.255.255
        inet6 fe80::fd2b:a7cf:a888:af56  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:17:c9:2a  txqueuelen 1000  (Ethernet)
        RX packets 7876  bytes 1172961 (1.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7427  bytes 1579609 (1.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.200  netmask 255.0.0.0  broadcast 20.255.255.255
        ether 00:0c:29:17:c9:2a  txqueuelen 1000  (Ethernet)

查看 Mysql2 是否接管 VIP
正常自动切换一次后,MHA 进程会退出。
MHA 会自动修改 app1.cnf 文件内容,将宕机的 mysql 节点删除。

(5)如主库修复后,可继续使用如下步骤,使其重新加入群集,并将 Mysql1 作为从库

1.在已经故障转移的20.0.0.26上,查看下二进制日志文件!他现在是主服务器了!

mysql -u root -p 
mysql> show master status;
+-----------+----------+--------------+------------------+-------------------+
| File      | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------+----------+--------------+------------------+-------------------+
| on.000008 |      154 |              |                  |                   |
+-----------+----------+--------------+------------------+-------------------+

mysql>stop slave;           #停掉之前做的同步进程,不然下次作为从库同步会报错
mysql>reset slave;

2.在20.0.0.25之前的主服务器上!!!!它现在成为从服务器了!
systemctl start mysqld   #启动数据库,之前被咱们关闭了
 mysql -u root -p
mysql>CHANGE MASTER TO MASTER_HOST='20.0.0.26', MASTER_PORT=3306, MASTER_LOG_FILE='on.000001', MASTER_LOG_POS=154, MASTER_USER='myslave', MASTER_PASSWORD='123456';
mysql>start slave;                              #启动从库同步
mysql>set global read_only=1;          #设置为只读


查看20.0.0.25(故障主服务器的)
查看I/O进程,sql进程是否开启! 
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.26
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: on.000008
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql1-relay-bin.000002
                Relay_Log_Pos: 313
        Relay_Master_Log_File: on.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

查看20.0.0.27,是否主从同步!


mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.26
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: on.000008
          Read_Master_Log_Pos: 310
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 469
        Relay_Master_Log_File: on.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

手动切换

(1)修改/etc/masterha/app1.cnf 文件,将刚刚手动宕机 Mysql1 库作为主库继续提供 服务

注意:手动切换 VIP 不会漂移。重新检查数据库主从状态是否正常,修改后的 app1.cnf 文件内容如下

在MHA服务器上
 vi /etc/masterha/app1.cnf        #配置文件内添加server1  因为故障转移,之前的宕机ip已被删除

[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
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
user=mha						###MySQL 监控用户
password=manager					###MySQL 监控用户的密码
ping_interval=1 					###ping 包时间间隔
remote_workdir=/tmp
repl_user=myslave 					###主从复制用户
repl_password=123456 				###主从复制用户的密码
secondary_check_script= /usr/local/bin/masterha_secondary_check -s 20.0.0.22 -s 20.0.0.23
shutdown_script=""
ssh_user=root 					###SSH 登录用户名

[server1]
hostname=20.0.0.25
port=3306
candidate_master=1
check_repl_delay=0

[server2]
hostname=20.0.0.26
port=3306


[server3]
hostname=20.0.0.27
port=3306

(2)检查无密码认证和 MySQL 主从状态是否正常,请参考之前的步骤,这里不再列出。
注意:检验主从连接状态时,需注意可能会遇到问题:

问题:mysqlbinlog: [ERROR] unknown variable ‘default-character-set=utf8’
只需要将原来20.0.0.25下的/etc/my.cnf [client]下的utf-8注释掉!!

(3) 最后启动 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 &

(4)查看当前主库是 Mysql2(20.0.0.26)。

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

app1 (pid:19195) is running(0:PING_OK), master:20.0.0.26

(5)停止 MHA,因为手动切换不能启动 MHA。

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

(6)手动设置当前的主库 Mysql2 为 dead,最后的报错没有影响。

masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=dead --dead_master_host=20.0.0.26

(7)再次检查主库的状态。

masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).            #app1 已经停止

(8)设置新的 master 为 Mysql1,在输入命令后会有交互需要输入 yes。

另外需要注释 手动切换脚本/usr/local/bin/master_ip_online_change 
里面的 152 行,不然会报错。 解决方案如下

FIXME_xxx_drop_app_user($orig_master_handler); 		####此行需要注释掉

(9)重新执行设置新的 master 为 alive 命令,出现 completed successfully 字样,表示手动切换成功。

cp /usr/local/bin/master_ip_online_change   /usr/local/bin/

masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=20.0.0.25 --orig_master_is_new_slave

............省略部分.............................
Sat Sep 19 04:34:07 2020 - [info] 20.0.0.25 can be new master.
Sat Sep 19 04:34:07 2020 - [info] 
From:
20.0.0.26(20.0.0.26:3306) (current master)
 +--20.0.0.25(20.0.0.25:3306)
 +--20.0.0.27(20.0.0.27:3306)

To:
20.0.0.25(20.0.0.25:3306) (new master)
 +--20.0.0.27(20.0.0.27:3306)
 +--20.0.0.26(20.0.0.26:3306)
...............省略部分...............
Switching master to 20.0.0.25(20.0.0.25:3306) completed successfully.

(10)在mysql2,3上验证slave 是否改变 同步!


20.0.0.26

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.25
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000013
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000013
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes



20.0.0.27

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.25
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000013
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000013
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

完成结束!

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47320286/article/details/108811203