Mysql集群部署(MHA)

MySQL高可用方案中,MHA(Master High Availability)是一个相对成熟的解决方案,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。

该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

mysql主从部署

参见 Mysql集群部署(主从复制)

配置公钥互信

创建mha管理节点

docker run -itd --privileged --name mysql-mha-manager centos-mysql-80 /usr/sbin/init

目前容器和ip对应关系为:

mysql-master  172.17.0.2
mysql-slave-1 172.17.0.3
mysql-slave-1 172.17.0.4
mysql-mha-manager 172.17.0.5

设置所有容器的root密码, 配置互信时需要:

passwd root
# 以下公钥互信命令,各主从容器都需要执行
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.2
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.3
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.4
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.0.5

MHA部署

# 安装节点软件包,每个容器都要安装
yum install epel-release
yum install perl-DBD-MySQL
yum install perl-Config-Tiny
yum install perl-Log-Dispatch
yum install perl-Parallel-ForkManager
yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm

# 安装管理软件包(仅mysql-mha-manager容器)
wget https://fushengwushi.com/files/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

增加文件脚本/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
);
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 {
      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" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

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";
}
chmod +x master_ip_failover

进入容器 mysql-mha-manager 创建mha配置文件 /etc/mha-manager.cnf:

[server default]
manager_workdir=/var/log/mha/app1
manager_log=/var/log/mha/app1/manager.log
user=root
password=123456
ssh_user=root
repl_user=root
repl_password=123456
ping_interval=1

master_ip_failover_script=/usr/local/bin/master_ip_failover

[server1]
hostname=172.17.0.2
master_binlog_dir=/var/lib/mysql
port=3306

[server2]
hostname=172.17.0.3
port=3306

[server3]
hostname=172.17.0.4
port=3306

配置文件校验

# 检查SSH配置
masterha_check_ssh --conf=/etc/mha-manager.cnf
# 验证配置
masterha_check_repl --conf=/etc/mha-manager.cnf

其他命令

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

运行

nohup masterha_manager --conf=/etc/mha-manager.cnf  --remove_dead_master_conf --ignore_last_failover < /dev/null >  /var/log/mha/app1/manager.log  2>&1 &

验证

mysql-master关闭mysql

service mysql stop

mysql-mha-manager容器, /var/log/mha/app1/manager.log:

至此,mysql集群部署完成~

参考链接

https://blog.csdn.net/qq_15350581/article/details/114090794

https://blog.csdn.net/fhdsece/article/details/104920422

0 评论
最新
最旧 最多投票
内联反馈
查看所有评论
滚动至顶部