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. Linux证书登录,禁用密码

    如果验证成功的话就可以关闭密码登陆方式了, 编辑/etc/ssh/sshd_config, 将PasswordAuthentication改为no, ChallengeResponseAuthenti ...

  2. Spring Boot实战(3) Spring高级话题

    1. Spring Aware Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的.即你可以将你的容器替换成别的容器. 实际项目中,不可避免地会用到Spring容 ...

  3. WebGL 踩坑系列-3

    WebGL 踩坑系列-3 绘制球体 在 WebGL 中绘制物体时需要的顶点是以直角坐标表示的, 当然了,gl_Position 是一个四维的向量,一般将顶点赋值给 gl_Position 时,最后一维 ...

  4. HDU 4357——String change——————【规律题】

    String change Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. jq获取页面距离

    $(window).height() //获取的是当前可视窗口的高度,也就是用户能看到的窗口的高度,是不变的(在窗口大小不变的前提下) $(document).height() //获取的是窗口内文档 ...

  6. .net iis6中配置伪静态

    1.右键点击 要设置网站的网站 2.属性 ——>主目录 ——>配置——> 3.如右侧窗口,找到 .aspx 扩展名——>编辑——>复制 可执行文件的路径——>关闭 ...

  7. 切图让我进步!关于white-space属性的组合拳

    菜鸟一枚,没有大神的风骚,只有一点在练习中的心得,今天获得的知识是关于white-space属性.overflow属性还有text-overflow属性的组合使用,废话不多说浪费时间,进入今天的正题! ...

  8. (八)JavaScript之[JSON]与[void]

    14].JSONJSON 格式在语法上与创建 JavaScript 对象代码是相同的. 方法:JSON.parse(); //将JSON字符串转换为JavaScript对象JSON.stringify ...

  9. SQL Server2005修改计算机名后不能发布订阅

    在一台安装有SQL Server2005的计算机上,更改计算机名后,在发布订阅的时候提示如下错误报告: 由于需要需要配置一个发布订阅,可是一直报告:" sql server 复制需要有实际的 ...

  10. Laravel 5.5 官方推荐 Nginx 配置学习

    Laravel 5.5 版本官方放出了 Nginx 服务器的配置,中文文档:服务器配置 Nginx server { listen 80; server_name example.com; root ...