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 ...
随机推荐
- C语言中左值和右值的区别(C语言学习笔记)
重要的内容要重复强调: C语言的术语Ivalue指用于识别或定位一个存储位置的标识符.( 注意:左值同时还必须是可改变的) 其实rvalue的发明完全是为了搭配lvalue , rvalue你可以理解 ...
- 2V转3V的电源芯片电路图,2.4V转3V电路
两节镍氢电池1.2V+1.2V是2.4V的标称电压,2.4V可以转3V输出电路应用. 在2.4V转3V和2V转3V的应用中,输出电流可最大600MA. 2V的低压输入,可以采用PW5100低压输入专用 ...
- MYSQL基础知识的复习2
1.修改表中的数据 update 表名 set 要修改的字段 where 条件;-- 如果修改多个字段那么字段和字段之间用逗号隔开 2.查询(很重要) 1.查询表中部分字段: select 字段名,字 ...
- 打包遇到错误Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
引自:https://blog.csdn.net/xiexiangyan/article/details/107936774 遇到的问题 有一个maven项目,我clone一下最新的代码.准备打包(m ...
- Python小度
这只是一个对话器!还不能听歌(反正我也没在UNIT平台配置听歌的功能)! 反正最近也不知怎么的,就想做一个AI对话器语音识别和语音输出都不要,input()和print()就行本来准备用小爱的,但要实 ...
- winform 添加背景图 闪屏问题解决
winform中只要添加了背景图片资源,窗体加载显示的时候就会出现不停的闪屏操作,网上找了很多方法,效果都不明显: 然后自己观察和思路:看窗体的加载过程,当有背景图的时候,首先出来的是背景图,之后背景 ...
- Soul API 网关源码解析 02
如何读开源项目:对着文档跑demo,对着demo看代码,懂一点就开始试,有问题了问社区. 今日目标: 1.运行examples下面的 http服务 2.学习文档,结合divde插件,发起http请求s ...
- NAT模式、路由模式、桥接模式的区别
NAT模式 NAT模式概述 NAT是"Network Address Translation"的缩写,中文意思是"网络地址转换",它允许一个整体机构以一个公用I ...
- Bitter.Core系列四:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 查询
一: 单表模型驱动查询 如下示例代码演示: // 根据ID 查询: var studentquery = db.FindQuery<TStudentInfo>().QueryById(12 ...
- YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. data = yaml.load(file_data)
YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsa ...