MHA 的 Binlog Server & VIP 漂移
Binlog Server
考虑一个问题,如果主库服务器宕机,为了保证数据不丢失,Binlog 如何保存?
另外搭建一台 Binlog Server 服务器,实时同步主库的 Binlog,保证主库 Binlog 不丢失,
此处为了节省资源,将 Binlog Server 与 从库服务器放在一起,
要注意的是,master_binlog_dir 的位置要重新指定一个目录,否则会与从库的 /usr/local/mysql/data 目录下的 Binlog 搞混
在 MHA 配置文件中配置 Binlog Server
[root@dbtest03 mha]# cat app1.conf
# 配置 Binlog Server 标签,同步主库的 Binlog
[binlog1]
hostname=172.16.1.123
# 不能跟当前服务器的数据库的 Binlog 存放目录一样,需要另外新建一个目录
master_binlog_dir=/binlog/
[server default]
manager_log=/etc/mha/app1/manager
manager_workdir=/etc/mha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/etc/mha/master_ip_failover
password=mha
ping_interval=2
repl_password=123
repl_user=rep
ssh_user=root
user=mha
[server1]
hostname=172.16.1.121
port=3306
[server2]
hostname=172.16.1.122
port=3306
[server3]
hostname=172.16.1.123
port=3306
创建 Binlog 存放目录
[root@dbtest03 ~]# mkdir /binlog
实时传输主库 Binlog 命令
# 进入该目录(一定进入该目录,再执行命令)
[root@dbtest03 ~]# cd /binlog/
# 备份 binlog(实时传输)
[root@dbtest03 binlog]# mysqlbinlog -R --host=172.16.1.121 --user=mha --password=mha --raw --stop-never mysql-bin.000001 &
# 或者用如下一条命令解决
[root@dbtest03 mha]# cd /binlog && mysqlbinlog -R --host=172.16.1.121 --user=mha --password=mha --raw --stop-never mysql-bin.000001 &
重启 MHA
[root@dbtest03 binlog]# masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[root@dbtest03 binlog]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/manager.log 2>&1 &
检验 MHA Manager 服务器 Binlog 同步
# 主库
[root@dbtest02 ~]# mysql -p12345
mysql> create database mha;
Query OK, 1 row affected (0.01 sec)
[root@dbtest02 ~]# ll /usr/local/mysql/data/mysql-bin.000008
-rw-rw---- 1 mysql mysql 67576 Jul 28 10:33 /usr/local/mysql/data/mysql-bin.000008
# MHA Manager 服务器查看 binlog
[root@dbtest03 binlog]# ll
total 96
-rw-rw---- 1 root root 852 Jul 28 10:30 mysql-bin.000001
-rw-rw---- 1 root root 214 Jul 28 10:30 mysql-bin.000002
-rw-rw---- 1 root root 214 Jul 28 10:30 mysql-bin.000003
-rw-rw---- 1 root root 214 Jul 28 10:30 mysql-bin.000004
-rw-rw---- 1 root root 465 Jul 28 10:30 mysql-bin.000005
-rw-rw---- 1 root root 214 Jul 28 10:30 mysql-bin.000006
-rw-rw---- 1 root root 214 Jul 28 10:30 mysql-bin.000007
-rw-rw---- 1 root root 67576 Jul 28 10:33 mysql-bin.000008
主库宕机恢复自动化脚本
主要功能:
主库宕机,可以自动恢复为从库,重启 MHA 服务,并使 Binlog Server 同步的 binlog,重新指定到新主库的 IP 地址,同步新主库的 binlog
[root@dbtest01 ~]# cat /tmp/start_mha.sh
#!/bin/bash
mysql_pid=`ps -ef | grep [m]ysqld | wc -l`
# 如果挂掉,重启;如果没有挂掉,杀掉重启
if [ $mysql_pid -eq 0 ];then
systemctl start mysqld
else
pkill mysqld
systemctl stop mysqld
systemctl start mysqld
fi
# 重新配置主从
change=`ssh 172.16.1.123 "grep 'CHANGE MASTER TO' /etc/mha/app1/manager | tail -1 | sed s#xxx#123#g" |awk -F: '{print $4}'`
mysql -uroot -p12345 -e "$change;start slave;"
# 将完整的配置文件(包括宕机主库)恢复
ssh 172.16.1.123 "\cp /etc/mha/app1.conf.bak /etc/mha/app1.conf"
# 获取新的主库的地址(从 MHA 日志中回复)
master_host=`ssh 172.16.1.123 "grep 'as a new master' /etc/mha/app1/manager | tail -1"| awk -F '[ ,(]' '{print $2}'`
# 恢复 Binlog Server,重新同步新主库的 Binlog
ssh 172.16.1.123 "cd /binlog && mysqlbinlog -R --host=${master_host} --user=mha --password=mha --raw --stop-never mysql-bin.000001" &
# 恢复 MHA 功能
ssh 172.16.1.123 "nohup masterha_manager --conf=/etc/mha/app1.conf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/manager.log 2>&1 &"
SSH 报错
碰到如下报错,无法 SSH 到 Binlog Server,但是 SSH 连接以及健康检查是完全没有问题的,

SSH 在连接自己服务器时,会有反向解析,所以连接速度很慢,MHA 会误认为无法 SSH 到 Binlog Server 服务器上,所以报错,这个报错会导致 MHA 的进程挂掉 。
解决方案:
优化一下 SSH sshd_config 里面 添加 USEDNS = no 以及 修改原 GSSAPIAuthentication yes 为 GSSAPIAuthentication no
VIP 漂移
VIP 漂移的两种方式
1.Keeplaived 的方式
2.MHA 自带的脚本进行 VIP 漂移
2.配置 MHA 读取 VIP 自动漂移 脚本
# 编辑配置文件
[root@dbtest03 ~]# vim /etc/mha/app1.cnf
# 在 [server default] 标签下添加
[server default]
manager_log=/etc/mha/app1/manager
manager_workdir=/etc/mha/app1
master_binlog_dir=/usr/local/mysql/data
# 使用 MHA 漂移脚本
master_ip_failover_script=/etc/mha/master_ip_failover
password=mha
ping_interval=2
repl_password=123
repl_user=rep
ssh_user=root
user=mha
[server1]
hostname=172.16.1.121
port=3306
[server2]
hostname=172.16.1.122
port=3306
[server3]
hostname=172.16.1.123
port=3306
编写 VIP 自动漂移脚本
# 在二进制安装包中,默认脚本存放在下面目录
[root@dbtest01 ~]# ll mha4mysql-manager-0.56/samples/scripts/
total 32
-rwxr-xr-x 1 4984 users 3648 Apr 1 2014 master_ip_failover
# 编辑脚本
[root@dbtest03 mha]# vim 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 = '172.16.1.55/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth1:$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 ($@) {
warn "Got Error: $@\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 ($@) {
warn $@;
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";
}
# 将这个脚本授权为可执行文件
[root@dbtest03 mha]# chmod 755 master_ip_failover
[root@dbtest03 mha]# dos2unix master_ip_failover
dos2unix: converting file master_ip_failover to Unix format ...
手动绑定主库 VIP
[root@dbtest01 ~]# ifconfig eth1:1 172.16.1.55/24
[root@dbtest01 ~]# ip a s eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:6e:52:99 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.121/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet 172.16.1.55/24 brd 172.16.1.255 scope global secondary eth1:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe6e:5299/64 scope link
valid_lft forever preferred_lft forever
# 解绑 VIP 命令(不用执行)
[root@dbtest01 ~]# ifconfig eth1:1 down
重启 MHA
# 启动 MHA
[root@dbtest03 ~]# nohup masterha_manager --conf=/etc/mha/app1.conf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/manager.log 2>&1 &
# 启动失败:
# 1.检查配置文件语法是否正确
# 2.授权是否正确
[root@dbtest03 mha]# chmod 755 master_ip_failover
# 3.将脚本中的换行符 转为 unix格式(原可能是 windows 格式)
[root@dbtest03 mha]# dos2unix master_ip_failover
dos2unix: converting file master_ip_failover to Unix format ...
测试 VIP 漂移
# 停止主库
[root@dbtest01 ~]# systemctl stop mysqld.service
# 查看切换成主库的ip地址
[root@dbtest02 ~]# ip a s eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:6e:52:99 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.121/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet 172.16.1.55/24 brd 172.16.1.255 scope global secondary eth1:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe6e:5299/64 scope link
valid_lft forever preferred_lft forever
MHA 的 Binlog Server & VIP 漂移的更多相关文章
- mha之vip漂移 配置binlog-server备份服务器 Atlas
MHAvip漂移 配置 通过MHA自带脚本方式,管理虚拟IP的漂移 获取管理脚本master_ip_failover cp master_ip_failover /usr/local/bin/ #脚本 ...
- MHA集群(gtid复制)和vip漂移
在上一片博客中,讲述了怎么去配置MHA架构!这片博客不再细说,只说明其中MySQL主从搭建,这里使用的是gtid加上半同步复制! 步骤与上一片博客一样,不同之处在于MySQL主从的搭建!详细的gtid ...
- mysql高可用架构 -> MHA配置VIP漂移-05
VIP漂移的两种方式 1)通过keepalived的方式,管理虚拟IP的漂移 2)通过MHA自带脚本方式,管理虚拟IP的漂移 MHA脚本方式 虚拟ip漂移的脚本下载地址 -> wget http ...
- 由VIP漂移引发的算法异常问题调查和解决
最近工作中的一个问题,耗时一个月之久终于调查完毕且顺利解决,顿时感慨万千.耗时之久和预期解决时间和环境搭建以及日志不合理等等有关,当然这个并非此文的重点.之所以在很久以后的今天又开始写文,主要是这个问 ...
- keepalived vip漂移基本原理及选举算法
keepalived可以将多个无状态的单点通过虚拟IP(以下称为VIP)漂移的方式搭建成一个高可用服务,常用组合比如 keepalived+nginx,lvs,haproxy和memcached等.它 ...
- (转)小谈keepalived vip漂移原理与VRRP协议
背景:之前搭建过keepalived双机热备的集群,但对其中的原理不甚理解,看完就忘了,所有有必要深入的学习下. 简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术 ...
- 一主多从+Binlog Server,主库故障无法访问,如何在从库中选举一个新主库
一.基本环境 VMware10.0+CentOS6.9+MySQL5.7.19 ROLE HOSTNAME BASEDIR DATADIR IP PORT M ZST1 /usr/local/mysq ...
- my08_mysqldump+binlog server备份
备份策略描述 ******************************************* mysqldump备份适用于小数据量的备份,比如100G以下的数据量,就可以使用逻辑备份 举例两个 ...
- 利用binlog server及Xtrabackup备份集来恢复误删表(drop)
Preface Today I'm gonna test how to rescue a dropped table from binlog server based on a ful ...
随机推荐
- 洛谷P3275 [SCOI2011]糖果(差分约束)
题目描述 幼儿园里有 $N$ 个小朋友,$lxhgww $老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的 ...
- [WPF] 在单元测试中使用 Prism 的 EventAggregator,订阅到 ThreadOption.UIThread 会报错
1. 问题 [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { ContainerLocator ...
- 开发进阶:Dotnet Core多路径异步终止
今天用一个简单例子说说异步的多路径终止.我尽可能写得容易理解吧,但今天的内容需要有一定的编程能力. 今天这个话题,来自于最近对gRPC的一些技术研究. 话题本身跟gRPC没有太大关系.应用中,我用 ...
- 单元测试:单元测试中的mock
公司要求提升单元测试的质量,提高代码的分支覆盖率和行覆盖率,安排我研究单元测试,指定方案分享并在开发部普及开.整理完资料后,同步一下到博客. 单元测试中的mock的目的 mock的主要目的是让单元测试 ...
- 边缘计算k8s集群SuperEdge初体验
前言 手上一直都有一堆的学生主机,各种各样渠道途径拿来的机器. 一直管理里面都比较蛋疼,甚至也不太记得住它们在哪是什么IP,管理起来很是头疼. 有阵子空闲的时候想折腾了一下边缘计算集群方案. 希望能把 ...
- [Usaco2005 Mar]Out of Hay 干草危机
题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,000)条双向道路,所有 ...
- 【源码解读】js原生消息提示插件
效果如下: 关闭message后前后message的衔接非常丝滑,这部分是我比较感兴趣的.带着这个问题先了解下DOM结构,顺便整理下作者的思路. 从DOM里我们可以看到所有的message都在一个容器 ...
- Redis-第五章节-8种数据类型
目录 一.Redis对key的操作 二.五种数据类型 String类型 List(集合) Set(集合) Hash(哈希) Zset(有序集合) 三.三种特殊数据类型 geospatial(地理位置) ...
- 白日梦的Elasticsearch实战笔记,ES账号免费借用、32个查询案例、15个聚合案例、7个查询优化技巧。
目录 一.导读 二.福利:账号借用 三._search api 搜索api 3.1.什么是query string search? 3.2.什么是query dsl? 3.3.干货!32个查询案例! ...
- Apache HTTP Server 映射URL到文件系统(翻译)
div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...