MySQL5.6基于MHA方式高可用搭建
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 {
"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方式高可用搭建的更多相关文章
- 技术实战:基于 MHA 方式实现 MySQL 的高可用(转)
转自:http://os.51cto.com/art/201307/401702_all.htm MHA故障转移可以很好的帮我们解决从库数据的一致性问题,同时最大化挽回故障发生后的数据.本文分享了基于 ...
- 基于zookeeper的高可用Hadoop HA集群安装
(1)hadoop2.7.1源码编译 http://aperise.iteye.com/blog/2246856 (2)hadoop2.7.1安装准备 http://aperise.iteye.com ...
- MySQL/MariaDB数据库的MHA实现高可用实战
MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...
- MaxCompute,基于Serverless的高可用大数据服务
摘要:2019年1月18日,由阿里巴巴MaxCompute开发者社区和阿里云栖社区联合主办的“阿里云栖开发者沙龙大数据技术专场”走近北京联合大学,本次技术沙龙上,阿里巴巴高级技术专家吴永明为大家分享了 ...
- 实现基于Keepalived主从高可用集群网站架构
背景 上一期我们实现了基于lvs负载均衡集群的电商网站架构,随着业务的发展,网站的访问量越来越大,网站访问量已经从原来的1000QPS,变为3000QPS,目前业务已经通过集群LVS架构可做到随时拓展 ...
- 使用Docker Compose部署基于Sentinel的高可用Redis集群
使用Docker Compose部署基于Sentinel的高可用Redis集群 https://yq.aliyun.com/articles/57953 Docker系列之(五):使用Docker C ...
- hadoop 集群HA高可用搭建以及问题解决方案
hadoop 集群HA高可用搭建 目录大纲 1. hadoop HA原理 2. hadoop HA特点 3. Zookeeper 配置 4. 安装Hadoop集群 5. Hadoop HA配置 搭建环 ...
- 基于LevelDB的高可用ActiveMQ集群
基于LevelDB的高可用ActiveMQ集群 http://donald-draper.iteye.com/blog/2347913
- hadoop+zookeeper集群高可用搭建
hadoop+zookeeper集群高可用搭建 Senerity 发布于 2 ...
随机推荐
- .NET面试题1
1. const和readonly有什么区别? const关键字用来声明编译时常量,readonly用来声明运行时常量.都可以标识一个常量,主要有以下区别: 1.初始化位置不同.const必须在声明的 ...
- c#实体转化
经常会遇到把一个实体转化成另一个实体这样的情况,实体的属性一个一个手写去转化不反对,但不是啥好的方法:可以使用反射写一个通用的实体转化类,针对任何实体转化,不用再去自己手写. public stati ...
- cf633F. The Chocolate Spree(树形dp)
题意 题目链接 \(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大 Sol 这辈子也不会写树形dp的 也就是有几种情况,可以讨论一下.. 下文的"最大值"指的是& ...
- 什么是MongoDb
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型.Mo ...
- #include stdio.h(7)
#include <stdio.h> int main() { //***********一.循环语句*************** //什么叫做循环: //重复的做某件事情,重复的执行一 ...
- 移动web基础
接触retina屏 基础知识(移动Web的基础知识)排版布局(高效的移动Web布局)开发效率终端交互优化 pixel像素基础viewport视图viewport_meta标签viewport_codi ...
- 工作流常使用API
记录实际开发中常使用到的API CreateProcess 在工作流开始之前,创建一个新的工作流 Wf_engine.CreateProcess (itemtype in varchar2, - ...
- SQL SERVER 错误代码 0x534
解决办法就是修改一下登陆名: ALTER LOGIN [G-PC\zqwang] WITH NAME=[新的机器名\zqwang]; 然后查询一下 Service Broker 队列, 里面已经有 ...
- C盘空间太大,分区助手减小分区大小教程
首先看一个需要缩小C盘或需要减少分区空间的一个例子:“我的电脑里C盘剩余空间为530GB,除了C盘外还有一个D盘,但D盘的空间不到30GB,另外还有两个隐藏分区,一个200MB,一个15GB.我想把C ...
- avast从隔离区恢复后,仍无法打开被误杀文件的解决方案
从隔离区中手动恢复后,隔离区中被恢复的文件将不再展示. 此时,如果手动恢复的文件仍无法打开(图标此时也异常),请: 将avast禁用: 将avast启用. 然后尝试重新打开被误隔离并手动恢复的文件.