云计算运维

Windows Server 2003 - Windows Server 2019 系统工具,Linux系统脚本,Mysql、Nginx、PHP、Redis、K8S、Seafile、Weblogic 、Jenkins、DNS、DHCP、FTP、IIS、Zookeeper、Rabbitmq、Oracle、Tomcat、Mavrn等服务搭建维护,请关注我.

搭建MHA高可用


先部署好一主二从的MySQL集群

再找一台主机作为管理节点

做MHA有两个安装包需要安装

github下载链接:Release mha4mysql-manager-0.58 · yoshinorim/mha4mysql-manager · GitHub

管理节点(只能centos7):mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

所有节点:mha4mysql-node-0.58-0.el7.centos.noarch.rpm

每台节点安装好各自需要的包

在管理节点配置:

#创建配置文件目录
mkdir /etc/mastermha/
#制作配置文件
vim /etc/mastermha/app1.cnf 
[server default]
user=mhauser    #用来远程连接各个MySQL节点的用户,需要有管理的权限
password=magedu
manager_workdir=/data/mastermha/app1/  #目录会自动生成,无需手动创建
manager_log=/data/mastermha/app1/manager.log  #日志文件存放路径
remote_workdir=/data/mastermha/app1/
ssh_user=root  #用于远程基于ssk——key的ssh连接,访问二进制日志
repl_user=copy   #用作主从复制的用户
repl_password=123456
ping_interval=1   #健康性检查时间间隔
master_ip_failover_script=/usr/local/bin/master_ip_failover #切换VIP的脚本
report_script=/usr/local/bin/sendmail.sh  #告警脚本
check_repl_delay=0  #默认值为1,如果slave的relay log落后master超过100M,则不会选择这个slave作为新的master,设置为0,则触发主从切换时会忽略这个差异
master_binlog_dir=/data/mysql/  #指定二进制日志存放的目录0.58版本以后必须指定
[server1]
hostname=10.0.0.139
candidate_master=1  #设置为优先候选的master
[server2]
hostname=10.0.0.100
candidate_master=1
[server3]
hostname=10.0.0.101

配置邮件告警

#准备告警脚本(需要先配置好邮件服务)
vim /usr/local/bin/sendmail.sh
#!/bin/bash
echo "MHA is failover!" | mail -s "MHA Warning" placjh@163.com
#给与执行权限
chmod +x /usr/local/bin/sendmail.sh

手动创建vip

#手动添加一个ip
ifconfig ens33:1 10.0.0.150/24
#查看添加的ip
ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:bb:eb:b3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.139/24 brd 10.0.0.255 scope global noprefixroute dynamic ens33
       valid_lft 1047sec preferred_lft 1047sec
    inet 10.0.0.150/24 brd 10.0.0.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::a3f7:3984:f29a:eb39/64 scope link noprefixroute 
       valid_lft forever preferred_lft foreve

配置切换vip的脚本,官方提供了一个perl脚本,但是要想实现vip飘动还需要做一些修改,根据网上搜索的答案做的改动

cat /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 = '10.0.0.150/24';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
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,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);
exit &main();
sub main {
  if ( $command eq "stop" || $command eq "stopssh" ) {
    # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
    # If you manage master ip address at global catalog database,
    # invalidate orig_master_ip here.
    my $exit_code = 1;
    eval {
      # updating global catalog, etc
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    # all arguments are passed.
    # If you manage master ip address at global catalog database,
    # activate new_master_ip here.
    # You can also grant write access (create user, set read_only=0, etc) here.
    my $exit_code = 10;
    eval {
	print "Enabling the VIP - $vip on the new master - $new_master_host \n";
        &start_vip();
        &stop_vip();
      my $new_master_handler = new MHA::DBHelper();
      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();
      ## Creating an app user on the new master
      print "Creating app user on the new master..\n";
      FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();
      ## Update master ip on the catalog database, etc
      #FIXME_xxx;
      $exit_code = 0;
    };
    if ($@) {
      warn $@;
      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {
	print "Checking the Status of the script.. OK \n";
        `ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
    # do nothing
    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的环境

#检查ssh环境是否满足要求
masterha_check_ssh --conf=/etc/mastermha/app1.cnf
Fri Nov  4 22:31:29 2022 - <div class="alert alert-warning"></div> Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Nov  4 22:31:29 2022 - <div class="alert alert-info"></div> Reading application default configuration from /etc/mastermha/app1.cnf..
Fri Nov  4 22:31:29 2022 - <div class="alert alert-info"></div> Reading server configuration from /etc/mastermha/app1.cnf..
Fri Nov  4 22:31:29 2022 - <div class="alert alert-info"></div> Starting SSH connection tests..
Fri Nov  4 22:31:31 2022 - [debug] 
Fri Nov  4 22:31:29 2022 - [debug]  Connecting via SSH from root@10.0.0.100(10.0.0.100:22) to root@10.0.0.139(10.0.0.139:22)..
Fri Nov  4 22:31:30 2022 - [debug]   ok.
Fri Nov  4 22:31:30 2022 - [debug]  Connecting via SSH from root@10.0.0.100(10.0.0.100:22) to root@10.0.0.101(10.0.0.101:22)..
Fri Nov  4 22:31:31 2022 - [debug]   ok.
Fri Nov  4 22:31:31 2022 - [debug] 
Fri Nov  4 22:31:29 2022 - [debug]  Connecting via SSH from root@10.0.0.139(10.0.0.139:22) to root@10.0.0.100(10.0.0.100:22)..
Fri Nov  4 22:31:30 2022 - [debug]   ok.
Fri Nov  4 22:31:30 2022 - [debug]  Connecting via SSH from root@10.0.0.139(10.0.0.139:22) to root@10.0.0.101(10.0.0.101:22)..
Fri Nov  4 22:31:30 2022 - [debug]   ok.
Fri Nov  4 22:31:32 2022 - [debug] 
Fri Nov  4 22:31:30 2022 - [debug]  Connecting via SSH from root@10.0.0.101(10.0.0.101:22) to root@10.0.0.139(10.0.0.139:22)..
Fri Nov  4 22:31:31 2022 - [debug]   ok.
Fri Nov  4 22:31:31 2022 - [debug]  Connecting via SSH from root@10.0.0.101(10.0.0.101:22) to root@10.0.0.100(10.0.0.100:22)..
Fri Nov  4 22:31:31 2022 - [debug]   ok.
Fri Nov  4 22:31:32 2022 - <div class="alert alert-info"></div> All SSH connection tests passed successfully.
#检查主从复制
masterha_check_repl --conf=/etc/mastermha/app1.cnf
Fri Nov  4 23:50:39 2022 - <div class="alert alert-warning"></div> Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Nov  4 23:50:39 2022 - <div class="alert alert-info"></div> Reading application default configuration from /etc/mastermha/app1.cnf..
......
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div> Checking replication health on 10.0.0.100..
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div>  ok.
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div> Checking replication health on 10.0.0.101..
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div>  ok.
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div> Checking master_ip_failover_script status:
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div>   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=10.0.0.139 --orig_master_ip=10.0.0.139 --orig_master_port=3306 
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div>  OK.
Fri Nov  4 23:50:47 2022 - <div class="alert alert-warning"></div> shutdown_script is not defined.
Fri Nov  4 23:50:47 2022 - <div class="alert alert-info"></div> Got exit code 0 (Not master dead).
MySQL Replication Health is OK.

前台启动mha

#前台启动
masterha_manager --conf=/etc/mastermha/app1.cnf --remove_dead_master_conf --ignore_last_failover
Fri Nov  4 23:56:04 2022 - <div class="alert alert-warning"></div> Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Nov  4 23:56:04 2022 - <div class="alert alert-info"></div> Reading application default configuration from /etc/mastermha/app1.cnf..
Fri Nov  4 23:56:04 2022 - <div class="alert alert-info"></div> Reading server configuration from /etc/mastermha/app1.cnf..
#检查mha状态
masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:14014) is running(0:PING_OK), master:10.0.0.139

后台运行mha

#使用nohup后台执行,关闭当前shell也无妨
nohup `masterha_manager --conf=/etc/mastermha/app1.cnf --remove_dead_master_conf --ignore_last_failover &>/dev/null` &

查看运行状态

masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:20495) is running(0:PING_OK), master:10.0.0.139

stop主节点测试能否实现故障转移,主节点的切换,vip的飘动

mha是属于一次性使用的程序,在完成故障转移后即退出running状态,配置文件也将被修改,需要再次手动将其启动

  • 分享:
评论
还没有评论
    发表评论 说点什么