mysql-day08     部署MYSQL高可用集群

集群架构

                                                             

MHA 工作过程

• MHA Manager 会定时探测集群中的 master 节点,

当 master 出现故障时,它可以自动将最新数据的 sl

ave 提升为新的 master ,然后将所有其他的 slave

重新指向新的 master 。整个故障转移过程对应用程

序完全透明。

– ( 1 )从宕机崩溃的 master 保存二进制日志事件( binlog events)

– ( 2 )识别含有最新更新的 slave

– ( 3 )应用差异的中继日志( relay log )到其他的 slave

– ( 4 )应用从 master 保存的二进制日志事件( binlog events )

– ( 5 )提升一个 slave 为新的 master ;

– ( 6 )使其他的 slave 连接新的 master 进行复制;

准备环境

一、集群定义:使用多台服务提供相同的服务

二、高可用集群定义:主备模式,被客户端访问的称作主,当主宕机时,备用

服务器自动接收客户端访问。

拓扑结构

client

|

|  -->vip 192.168.4.100 《51,52,53》

_____________________________________________________

|||                 |||                  |||                   ||            ||             ||              ||

master51  master(备)52  master(备)53  slave54   slave55   mgm56   Manager

一、配置所有数据节点主机之间可以互相以ssh密钥对方式认证登陆

二、配置manager56主机无密码ssh登录所有数据节点主机

三、配置主从同步,要求如下:

51主库 开半同步复制

52从库(备用主库)开半同步复制

53从库(备用主库)开半同步复制

54从库不做备用主库所以不用开半同步复制

55从库不做备用主库所以不用开半同步复制

拓扑图

• 采用 5 个数据库服务器节点

步骤

注意:安装mariadb,就有mysql命令

1.在所有主机上安装perl软件包(51~56)

]#cd mha-soft-student

]#yum -y install perl-*.rpm

2.在所有主机上安装mha_node软件包(51~56)

yum -y install perl-DBD-mysql

rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

2.只在管理"主机56"上安装mha_manager软件包

]#yum -y install perl-ExtUtils-* perl-CPAN*

]#tar -zxvf mha4mysql-manager-0.56.tar.gz

]#cd mha4mysql-manager-0.56

]#perl Makefile.PL

]#make

]#make install

3.2.4检查配置环境

在主机51 52 53检查是否有同步数据的用户repluser

mysql>show grants for repluser@"%";

u 在主机51~55做如下授权

mysql>grant all on *.* to root@"%" identified by "123456";

u 在主机51~55做如下设置

mysql>set global relay_log_purge=off;

3.2.5拷贝命令(56)

]#cp mha4mysql-manager-0.56/bin/* /usr/local/bin/

3.2.6创建工作目录和主配置文件(56)

]#mkdir /etc/mha_manager/

]#cp mha4mysql-manager-0.56/samples/conf/app1.cnf

/etc/mha_manager/

3.2.7创建故障切换脚本(56)

]#ls /usr/local/bin/master_ip_failover

]#cp mha4mysql-manager-0.56/samples/scripts/master_ip_failover    /usr/local/bin/

3.2.8编辑主配置文件app1.cnf (56)

]#vim /etc/mha_manager/app1.cnf

[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager/manager.log
master_ip_failover_script=/usr/local/bin/master_ip_failover
ssh_user=root
ssh_port=22
repl_user=repluser
repl_password=123456
user=root
password=123456
[server1]
hostname=192.168.4.51
port=3306
[server2]
hostname=192.168.4.52
port=3306
candidate_master=1
[server3]
hostname=192.168.4.53
port=3306
candidate_master=1
[server4]
hostname=192.168.4.54
no_master=1
[server5]
hostname=192.168.4.55
no_master=1

2.2.9验证配置

验证ssh免密码登录数据节点主机

]#masterha_check_ssh --conf=/etc/mha_manager/app1.cnf

Sun May 6 16:38:19 2018-[info]All SSH connection tests passed

successfully.

验证数据节点的主从同步配置(要不调用故障切换脚本)

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

MySQL Replication Health is OK.

四、测试高可用集群配置

4.1在主库上手动部署vip地址192.168.4.100

[root@db51~]#ifconfig eth0:1 192.168.4.100/24

[root@db51~]#ifconfig eth0:1

eth0:1:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500

inet 192.168.4.100 netmask 255.255.255.0 broadcast

192.168.4.255

ether 74:52:09:07:51:01 txqueuelen 1000(Ethernet)

4.2修改故障切换脚本指定vip地址的部署信息

]#vim/usr/local/bin/master_ip_failover

my$vip='192.168.4.100/24';#Virtual IP

my$key="1";

my$ssh_start_vip="/sbin/ifconfig eth0:$key$vip";

my$ssh_stop_vip="/sbin/ifconfig eth0:$key down";

......

##Update master ip on the catalog database,etc

#FIXME_xxx;//备注

:wq


#!/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 = '192.168.4.60/24';  # Virtual
IP
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
      &stop_vip();
      $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";
     
$new_master_handler->enable_log_bin_local();
     
$new_master_handler->disconnect();

## Update master ip on the catalog
database, etc
      &start_vip();
      $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 start_vip() {
    `ssh $ssh_user\@$new_master_host
\" $ssh_start_vip \"`;
}
sub stop_vip() {
    return 0 unless ($ssh_user);
    `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.3启动管理服务,并查看服务状态

]#masterha_manager--conf=/etc/mha/app1.cnf

--remove_dead_master_conf--ignore_last_failover

[root@host56~]#masterha_check_status--conf=/etc/mha_manager/app1.cnf

app1(pid:16944)is running(0:PING_OK),master:192.168.4.51

[root@host56~]#

+++++++++++++++++++++++++++++++++++++++++++

4.4测试故障转移

在主库51上执行]#shutdown-h now

4.5在管理主机上查看服务状态(如果服务停止了,手动启动一下服务,再查看状态)

[root@host56~]#masterha_check_status --conf=/etc/mha_manager/app1.cnf

app1(pid:17507)is running(0:PING_OK),master:192.168.4.52

4.6在52本机查看是否获取vip地址

[root@db52~]#ip addr show|grep 192.168.4 inet 192.168.4.52/24 brd 192.168.4.255 scope global eth0

inet 192.168.4.100/24 brd 192.168.4.255 scope global secondary

4.7客户端连接vip地址,访问数据服务

]#mysql-h192.168.4.100-uwebadmin-p123456

4.8重置主库

Smysql>stop slave;

Mysql>change master to

master_host="192.168.4.51",master_user="repluser", master_password="123456",

master_log_file="master51.000014",

master_log_pos=154;

Mysql>start slave;

Mysql>show slave status\G;//查看主库信息

mysql>show variables like'log_bin%';//查看log设置

 

##############################################

 

还原成独立数据库

root@host55~]#systemctl stop mysqld

[root@host55~]#cd /var/lib/mysql

[root@host55 mysql]#rm -rf master.info relay-log.info

[root@host55 mysql]#ls

[root@host55 mysql]#rm -rf host55-relay-bin.*

[root@host55 mysql]#systemctl start mysqld

[root@host55 mysql]#mysql -uroot -p123456 -e "show slave status\G"

部署MYSQL高可用集群的更多相关文章

  1. MySQL高可用集群方案

    一.Mysql高可用解决方案 方案一:共享存储 一般共享存储采用比较多的是 SAN/NAS 方案. 方案二:操作系统实时数据块复制 这个方案的典型场景是 DRBD,DRBD架构(MySQL+DRBD+ ...

  2. MySQL高可用集群MHA方案

    MySQL高可用集群MHA方案 爱奇艺在用的数据库高可用方案 MHA 是目前比较成熟及流行的 MySQL 高可用解决方案,很多互联网公司正是直接使用或者基于 MHA 的架构进行改造实现 MySQL 的 ...

  3. (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)

    参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...

  4. Mysql高可用集群环境介绍

    MySQL高可用集群环境搭建 01.MySQL高可用环境方案 02.MySQL主从复制原理 03.MySQL主从复制作用 04.在Linux环境上安装MySQL 05.在MySQL集群环境上配置主从复 ...

  5. Mysql 高可用集群PXC

    PXC是percona公司的percona  xtraDB  cluster,简称PXC.它是基于Galera协议的高可用集群方案.可以实现多个节点间的数据同步复制以及读写,并且可保障数据库的服务高可 ...

  6. 部署zookeepe高可用集群

                                                                部署zookeepe高可用集群 部署规划 Nno1         192.16 ...

  7. keepalived+MySQL高可用集群

    基于keepalived搭建MySQL的高可用集群   MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,MMM,Heartbeat+DRBD,PXC,Galera Clus ...

  8. corosync+pacemaker and drbd实现mysql高可用集群

    DRBD:Distributed Replicated Block Device 分布式复制块设备,原理图如下 DRBD 有主双架构和双主架构的,当处于主从架构时,这个设备一定只有一个节点是可以读写的 ...

  9. 一键部署Kubernetes高可用集群

    三台master,四台node,系统版本为CentOS7 IP ROLE 172.60.0.226 master01 172.60.0.86 master02 172.60.0.106 master0 ...

随机推荐

  1. element UI使用

    1.Button按钮 <el-button type="text">文字按钮</el-button>设置type="text",可以是无 ...

  2. Scrapy同时启动多个爬虫

    1. 在项目文件夹中新建一个commands文件夹 2. 在command的文件夹中新建一个文件 crawlall.py 3.在crawlall.py 中写一个command类,该类继承 scrapy ...

  3. 基于 HTML5 WebGL 的发动机 3D 可视化系统

    前言     工业机械产品大多体积庞大.运输成本高,在参加行业展会或向海外客户销售时,如果没有实物展示,仅凭静态.简单的图片说明书介绍,无法让客户全面了解产品,不仅工作人员制作麻烦,客户看得也费力.如 ...

  4. Spring注解 - AOP 面向切面编程

    基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...

  5. 适用于 macOS 下 2K 显示器开启 HiDPI 的简便解决方案

    前阵子入手了一款2k的显示器用于mbp的拓展屏幕,但由于苹果系统的严格限制,只有在4K及以上显示器上才能开启自带的HiDPI进行缩放以达到retina显示屏的效果.2k显示器下的字体会偏小发虚,使用1 ...

  6. python-参考书

    真的是找了很多的参考书,但是看懂,上手用的就一本比较好的<编程小白的第1本python入门书>非常的好. 属于那种一看就懂,能说明白的.别的,要么就是翻译的外文的,有点难以理解,要么就是中 ...

  7. 选择tomcat时候提示Project facet Java version 1.8 is not supported.解决办法

    是因为jdk版本不一致导致的,如何解决? 方法一: 选中项目Properties,选择Project Facets,右击选择Java,Change Version 方法二: 在项目的目录下有一个.se ...

  8. Linux - top命令监控列表的详细解析

    统计信息区(系统资源信息区) 前五行,即列表上方的五行 第一行:输出系统任务队列信息 18:46:38:系统当前时间 up 2days 1:54:系统开机后到现在的总运行时间 1 user:当前登录用 ...

  9. CentOS 7.3下安装MySql

    1.下载mysql源安装包 wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm   2.安装mysql源 ...

  10. [源码分析] 从实例和源码入手看 Flink 之广播 Broadcast

    [源码分析] 从实例和源码入手看 Flink 之广播 Broadcast 0x00 摘要 本文将通过源码分析和实例讲解,带领大家熟悉Flink的广播变量机制. 0x01 业务需求 1. 场景需求 对黑 ...