master 10.205.22.185 #MHA node

slave1 10.205.22.186  #MHA node+MHA manager

slave2 10.205.22.187  #MHA node

三台服务器的MySQL已经搭建好主从架构,并互相配置好ssh免密码登录。

1.下载MHA,并安装MHA包的依赖软件

https://downloads.mariadb.com/MHA(或者 https://github.com/yotoobo/linux/tree/master/mha
yum install perl-Config-Tiny perl-Log-Dispatch perl-Time-HiRes perl-Parallel-ForkManager

2.安装MHA软件包

rpm -ivh mha4mysql-node-0.56-.el6.noarch #所有节点安装
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch #186管理节点安装

3.创建用户mha管理的账号,在所有mysql服务器上都需要执行:

GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'10.205.22.%' IDENTIFIED BY '';

如果是在slave服务器上安装的manager,则需要创建以本机hostname名连接的账号,不然masterha_check_repl测试通不过。
GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'master(主机名)' IDENTIFIED BY ''

4.在MHA manager服务器添加配置文件/etc/masterha/app1.cnf(自定义),并创建相关目录和修改权限

[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
secondary_check_script= masterha_secondary_check -s 10.205.22.185 -s 10.205.22.186 #实现多路由监测Master的可用性
master_ip_failover_script=/usr/local/scripts/master_ip_failover user=mha_rep
password= ssh_user=root
repl_user=repl
repl_password=password
ping_interval= [server1]
hostname=10.205.22.185
candidate_master=
master_binlog_dir=/home/data/mysql/binlog [server2]
hostname=10.205.22.186
candidate_master=
master_binlog_dir=/home/data/mysql/binlog [server3]
hostname=10.205.22.187
no_master=
master_binlog_dir=/home/data/mysql/binlog
mkdir -p /home/data/mysql/binlog #创建相关目录
chown -R mysql.mysql /home/data/mysql/binglog #修改权限

5.编写自动切换VIP的脚本,/usr/local/scripts/master_ip_failover ,并加上可执行权限+x。

!/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 = '10.205.22.111/24'; # Virtual IP
my $gateway = '10.205.22.1'; #Gateway IP
my $interface = 'eth0';
my $key = "";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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" ) {
# $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 = ;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = ;
};
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=, etc) here.
my $exit_code = ;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = ;
};
if ($@) {
warn $@;
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 \"`;
exit ;
}
else {
&usage();
exit ;
}
}
# A simple system call that enable the VIP on the new master
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";
}

6.检查配置文件

利用mha工具检测ssh
masterha_check_ssh --conf=/etc/masterha_application.cnf 使用mha工具check检查repl环境
masterha_check_repl --conf=/etc/masterha_application.cnf

7.开启MHA manager,并查看状态

开启masterha_manager
nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log >& &
(masterha_stop --conf=/etc/masterha/app1.cnf #关闭时使用的命令) 查看
masterha_check_status --conf=/etc/masterha/app1.cnf

8.模拟MySQL故障,查看VIP漂移和MySQL自动切换情况(切换后MHA服务会自动停止)

/etc/init.d/mysqld stop #master上操作
在manager上查看切换日志:tail -f /var/log/masterha/app1/manager.log 
查看备用节点情况:show master status \G

9.MySQL故障服务器重新加入MHA环境

.把故障服务器设为新的slave
.重新启动MHA manager
.查看MHA状态

10.在线手动切换主从,没有启用MHA自动切换功能

1.原master出现故障
masterha_stop --conf=/etc/masterha/app1.cnf #停止
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=10.205.22.185 --dead_master_port= --new_master_host=10.205.22.186 --new_master_port= --ignore_last_failover 2.把原master变为slave切换
masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=10.205.22.185 --new_master_port=3306 --orig_master_is_new_slave

为了保证数据完全一致性,在最快的时间内完成切换,MHA的在线切换必须满足以下条件才会切换成功,否则会切换失败。

1.所有slave的IO线程都在运行

2.所有slave的SQL线程都在运行

3.所有的show slave status的输出中Seconds_Behind_Master参数小于或者等于running_updates_limit秒,如果在切换过程中不指定running_updates_limit,那么默认情况下running_updates_limit为1秒。

4.在master端,通过show processlist输出,没有一个更新花费的时间大于running_updates_limit秒。

MySQL5.6基于MHA方式高可用搭建的更多相关文章

  1. 技术实战:基于 MHA 方式实现 MySQL 的高可用(转)

    转自:http://os.51cto.com/art/201307/401702_all.htm MHA故障转移可以很好的帮我们解决从库数据的一致性问题,同时最大化挽回故障发生后的数据.本文分享了基于 ...

  2. 基于zookeeper的高可用Hadoop HA集群安装

    (1)hadoop2.7.1源码编译 http://aperise.iteye.com/blog/2246856 (2)hadoop2.7.1安装准备 http://aperise.iteye.com ...

  3. MySQL/MariaDB数据库的MHA实现高可用实战

      MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...

  4. MaxCompute,基于Serverless的高可用大数据服务

    摘要:2019年1月18日,由阿里巴巴MaxCompute开发者社区和阿里云栖社区联合主办的“阿里云栖开发者沙龙大数据技术专场”走近北京联合大学,本次技术沙龙上,阿里巴巴高级技术专家吴永明为大家分享了 ...

  5. 实现基于Keepalived主从高可用集群网站架构

    背景 上一期我们实现了基于lvs负载均衡集群的电商网站架构,随着业务的发展,网站的访问量越来越大,网站访问量已经从原来的1000QPS,变为3000QPS,目前业务已经通过集群LVS架构可做到随时拓展 ...

  6. 使用Docker Compose部署基于Sentinel的高可用Redis集群

    使用Docker Compose部署基于Sentinel的高可用Redis集群 https://yq.aliyun.com/articles/57953 Docker系列之(五):使用Docker C ...

  7. hadoop 集群HA高可用搭建以及问题解决方案

    hadoop 集群HA高可用搭建 目录大纲 1. hadoop HA原理 2. hadoop HA特点 3. Zookeeper 配置 4. 安装Hadoop集群 5. Hadoop HA配置 搭建环 ...

  8. 基于LevelDB的高可用ActiveMQ集群

    基于LevelDB的高可用ActiveMQ集群 http://donald-draper.iteye.com/blog/2347913

  9. hadoop+zookeeper集群高可用搭建

                                                                  hadoop+zookeeper集群高可用搭建 Senerity 发布于 2 ...

随机推荐

  1. lftp 快速使用

    登录 lftp username:password@ip:port 设置字符集 set ftp:charset 'gbk' set ftp:charset 'utf-8' 下载文件 mget *.tx ...

  2. 在C#.net中如何操作XML

    在C#.net中如何操作XML 需要添加的命名空间: using System.Xml; 定义几个公共对象: XmlDocument xmldoc ; XmlNode xmlnode ; XmlEle ...

  3. 【第一篇笔记】C# 全局容错,全局异常

    网上找到两个方式,一个简单的只是做个记录,另一个能像QQ一样提交到后台. 方法一: static class Program { /// <summary> /// 应用程序的主入口点. ...

  4. mint-ui popup自动关闭

    <template> <div class="hello"> <input type="text" v-model="n ...

  5. iOS - The identity used to sign the executable is no longer valid

    ①.首先在xcode中的Build Settings中看有没有设置: ②.账号是不是多个人在用,个人开发者的账号只能绑定一台电脑,当另外一台电脑绑定了话,你的电脑就失效了.你确认下是不是这个原因造成的 ...

  6. Android基础Activity篇——Intent

    1.显式的Intent intent是用来各各活动之间切换的,还可以用来传递参数. 项目还是使用之前创建的ActivityTest项目,这里新建一个活动SecondActivity.java,并且勾选 ...

  7. Android内存监测工具使用

    用 Heap监测应用进程使用内存情况的步骤如下:1. 启动eclipse后,切换到DDMS透视图,并确认Devices视图.Heap视图都是打开的:2. 将手机通过USB链接至电脑,链接时需要确认手机 ...

  8. 初识ImageSwither

    imageswitcher继承自viewswitcher,使用ImageSwither只需要两步: 1.为ImageSwither提供一个ViewFactory,该ViewFactory生成的View ...

  9. 【QT】【OpenCv】初始配置以及测试功能

    #include "mainwindow.h" #include "ui_mainwindow.h" #include<opencv2/core/core ...

  10. Struts2_使用token拦截器控制重复提交(很少用)

    控制重复提交的方式:1.表单提交后页面重定向:2.Struts2.x token拦截器 大致流程: 例子: index.jsp <%@ page language="java" ...