MHA MySQL 高可用集群搭建

软件版本
MySQL: 5.7
MHA: 0.56
CentOS: 7.4

MySQL服务器
Master: 192.168.1.224 mysql01
Slave1: 192.168.1.225 mysql02
Slave2: 192.168.1.226 mysql03

MHA
Manager: 192.168.1.224 mysql01
Node1: 192.168.1.225 mysql02
Node2: 192.168.1.226 mysql03

VIP
192.168.1.221

配置主机解析

cat /etc/hosts
192.168.1.224   mysql01
192.168.1.225   mysql02
192.168.1.226   mysql03

在三台服务器搭建MySQL主从复制集群
1、配置MySQL yum安装源

cat /etc/yum.repos.d/MySQL.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

2、安装MySQL

yum install mysql-community-{server,client,common,libs}-*

3、修改配置文件

#Master
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
log_bin
binlog_format=row
#Slave1
server-id=2
log_bin
gtid_mode=on
enforce_gtid_consistency=on
log_slave_updates=1
binlog_format=row
#Slave2
server-id=3
log_bin
gtid_mode=on
enforce_gtid_consistency=on
log_slave_updates=1
binlog_format=row

4、启动MySQL,并获取初始密码

systemctl start mysqld
grep password /var/log/mysqld.log 

5、在三台服务器上修改root密码和创建复制账户

mysql -u root -p
set sql_log_bin=0;
alter user [email protected]‘localhost‘ identified by ‘MySQL5.7‘;
create user [email protected]‘192.168.1.%‘ identified by ‘MySQL5.7‘
grant all on *.* to [email protected]‘192.168.1.%‘;
flush privileges;
set sql_log_bin=1;

6、指定Slave的Master,启动slave,查看状态

change master to master_host=‘192.168.1.224‘,master_user=‘root‘,master_password=‘MySQL5.7‘,master_auto_position=1;
start slave;
show slave status\G

在三台服务器实现免密码登录

ssh-keygen -t rsa
ssh-copy-id mysql01
ssh-copy-id mysql02
ssh-copy-id mysql03

安装MHA
1、配置epel源,安装依赖包

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y perl-CPAN* perl-DBD-MySQL* perl-Params-Validate perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

2、在三台服务器上安装node

tar -zxvf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56
perl Makefile.PL
make && make install

3、在Mater安装manager

tar -zxvf mha4mysql-manager-0.57.tar.gz
perl Makefile.PL
make
make install

4、创建目录和配置文件

mkdir -p /usr/local/mha
mkdir -p /etc/mhacd /etc/mha/
cat /etc/mha/mha.conf
----------------------------------------------------------------------------------------------------
[server default]
user=root
password=MySQL5.7
manager_workdir=/usr/local/mha
manager_log=/usr/local/mha/manager.log
remote_workdir=/usr/local/mha
ssh_user=root
repl_user=root
repl_password=MySQL5.7
ping_interval=1
master_ip_failover_script=/usr/local/scripts/master_ip_failover
master_ip_online_change_script=/usr/local/scripts/master_ip_online_change

[server1]
hostname=192.168.1.224
ssh_port=22
master_binlog_dir=/var/lib/mysql
candidate_master=1
port=3306

[server2]
hostname=192.168.1.225
ssh_port=22
master_binlog_dir=/var/lib/mysql
candidate_master=1
port=3306

[server3]
hostname=192.168.1.226
ssh_port=22
master_binlog_dir=/var/lib/mysql
no_master=1
port=3306
----------------------------------------------------------------------------------------------------

5、创建目录和脚本,注意根据具体情况修个IP和网卡名

mkdir -p /usr/local/scripts
cat /usr/local/scripts/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.1.221/24‘;
my $key = ‘0‘;
my $ssh_start_vip = "/sbin/ifconfig ens160:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens160:$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,
);  

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 ([email protected]) {
            warn "Got Error: [email protected]\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 ([email protected]) {
            warn [email protected];
            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 \"`;
}
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";
}
----------------------------------------------------------------------------------------------------
cat /usr/local/scripts/master_ip_online_change
----------------------------------------------------------------------------------------------------
#!/usr/bin/env perl
use strict;
use warnings FATAL =>‘all‘;  

use Getopt::Long;  

my $vip = ‘192.168.1.221/24‘;  # Virtual IP
my $key = "0";
my $ssh_start_vip = "/sbin/ifconfig ens160:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens160:$key down";
my $exit_code = 0;  

my (
  $command,              $orig_master_is_new_slave, $orig_master_host,
  $orig_master_ip,       $orig_master_port,         $orig_master_user,
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,
  $new_master_ip,        $new_master_port,          $new_master_user,
  $new_master_password,  $new_master_ssh_user,
);
GetOptions(
  ‘command=s‘                => \$command,
  ‘orig_master_is_new_slave‘ => \$orig_master_is_new_slave,
  ‘orig_master_host=s‘       => \$orig_master_host,
  ‘orig_master_ip=s‘         => \$orig_master_ip,
  ‘orig_master_port=i‘       => \$orig_master_port,
  ‘orig_master_user=s‘       => \$orig_master_user,
  ‘orig_master_password=s‘   => \$orig_master_password,
  ‘orig_master_ssh_user=s‘   => \$orig_master_ssh_user,
  ‘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,
  ‘new_master_ssh_user=s‘    => \$new_master_ssh_user,
);  

exit &main();
sub main {  

#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";  

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 {
            print "\n\n\n***************************************************************\n";
            print "Disabling the VIP - $vip on old master: $orig_master_host\n";
            print "***************************************************************\n\n\n\n";
&stop_vip();
            $exit_code = 0;
        };
        if ([email protected]) {
            warn "Got Error: [email protected]\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 "\n\n\n***************************************************************\n";
            print "Enabling the VIP - $vip on new master: $new_master_host \n";
            print "***************************************************************\n\n\n\n";
&start_vip();
            $exit_code = 0;
        };
        if ([email protected]) {
            warn [email protected];
            exit $exit_code;
        }
        exit $exit_code;
}
elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
        exit 0;
}
else {
&usage();
        exit 1;
}
}  

# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $orig_master_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=po
rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";  

}
----------------------------------------------------------------------------------------------------
chmod +x /usr/local/scripts/*

6、检查配置

/usr/local/bin/masterha_check_ssh --conf=/etc/mha/mha.conf

7、在Master上添加vip、启动mha、检查状态

ip addr add 192.168.1.221 dev ens160
nohup masterha_manager --conf=/etc/mha/mha.conf > /tmp/mha_manager.log  < /dev/null 2>&1 &
masterha_check_status --conf=/etc/mha/mha.conf

注意:切换后需要将mha.failover.complete文件删除,否则后面切换会失败

参考文档:企业中MySQL主流高可用架构实战三部曲之MHA

原文地址:http://blog.51cto.com/linux10000/2150449

时间: 2024-10-09 14:24:52

MHA MySQL 高可用集群搭建的相关文章

MySQL主从复制、读写分离、高可用集群搭建

MySQL主从复制.读写分离.高可用集群搭建  一.服务介绍   1.1 Keepalived     Keepalived,见名知意,即保持存活,其目的是解决单点故障,当一台服务器宕机或者故障时自动切换到其他的服务器中.Keepalived是基于VRRP协议实现的.VRRP协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成虚拟设备,可以对外提供虚拟路由器IP(一个或多个),即漂移IP(VIP). 1.2 ProxySQL ProxySQL是一个高性能,高可用性的MySQL

mysql高可用集群——MHA架构

目录 1.下载 2.搭建mha 2.1 系统配置 2.2 架构 2.3 添加ssh公钥信任 2.4 安装mha节点 2.5 manager配置文件 2.6 检查 2.7 启动manager进程 2.8 碰到的问题 3.测试切换 3.1 正常切换测试 3.2 回切测试 3.3 雪崩测试 3.4 主从不一致切换测试 下载 mha链接地址:http://pan.baidu.com/s/1pJkDGX9#dir/path=%2Fmysql%2FHA%2Fmha 或者:https://code.googl

MHA 高可用集群搭建(二)

MHA 高可用集群搭建安装scp远程控制http://www.cnblogs.com/kevingrace/p/5662839.html yum install openssh-clients mysql5.7运行环境:centos6.51 主机部署 manager:192.168.133.141test1: 192.168.133.138test2:192.168.133.139 (为master1的备用)test3: 192.168.133.140 test1为主,test2和test3为备

MySQL高可用集群之MHA

MySQL高可用集群之MHA 一.MHA简介 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用. MHA里有两

配置MySQL高可用集群MHA

配置MySQL高可用集群+++++++++++++++++++主机角色 :客户端 client50数据库服务器 mysql51 到 mysql55管理主机 mgm56VIP地址 192.168.4.100拓扑结构: client50 | mysql51主 | | | | | |mysql52 mysql53 mysql54 mysql55 mgm56从 从 从 从 管理集群备用主 备用主+++++++++++++++++++++++++++++++++++++++++++++++++++++++

heartbeat v2配置高可用web集群和基于nfs搭建MySQL高可用集群

安装环境:Centos 6.4, httpd2.4,mysql5.5,heartbeat v2 提供两台机器node1和node2,在/etc/hosts文件中添加名称解析,并且主机名称要与节点名称要相同,即uname -n的名称要和hosts定义的名称必须一样. #   IP                         HOSTNAME             ALIAS 10.204.80.79     node1.mylinux.com     node1 10.204.80.80  

企业主流MySQL高可用集群架构三部曲之PXC

前段时间,老张给大家介绍了企业中主流MySQL高可用集群架构三部曲中的前两部,有不了解的同学可以去访问我之前的博客内容. 第一部曲直通车>> 企业中MySQL主流高可用架构实战三部曲之MHA 第二部曲直通车>>企业中MySQL高可用集群架构三部曲之MM+keepalived 独家新课程上线>>MySQL体系结构深入剖析及实战DBA视频课程 今儿给大家介绍最后一部曲,是percona公司的percona xtraDB cluster.简称PXC.它是基于GaLera协议的

LVS+Keepalived+Nginx+Tomcat高可用集群搭建(转)

LVS+Keepalived+Nginx+Tomcat高可用集群搭建 最近公司重整架构,十几台服务器而且还要尽可能节约成本.我就谷歌了一下在几种集群之前进行比较最终采用了Keepalived+Nginx做负债均衡高可用.虽然之前也研究过集群,看过很多集群方面的原理和架构,但毕竟没有真正操作过以下案例是在虚拟机中完成实验其实对于高可用搭建来说只用给出Keepalived和Nginx的配置即可后面的一些安装搭建完全是本人项目需要可以不用理睬仅供参考. 本文只是实验搭建记录方便以后在服务器中实施搭建.

mysql高可用集群方案

这里有一篇关于Mysql高可用方案的干货文章:[干货分享] 一文了解数据库高可用容灾方案的设计与实现 网友们公司中的使用方案讨论:想问各位大大 MySQL 是怎么做高可用的? 一.Mysql高可用解决方案 方案一:共享存储 一般共享存储采用比较多的是 SAN/NAS 方案. 方案二:操作系统实时数据块复制 这个方案的典型场景是 DRBD,DRBD架构(MySQL+DRBD+Heartbeat) 方案三:主从复制架构 主从复制(一主多从) MMM架构(双主多从) MHA架构(多主多从) 方案四:数