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 漂移的更多相关文章

  1. mha之vip漂移 配置binlog-server备份服务器 Atlas

    MHAvip漂移 配置 通过MHA自带脚本方式,管理虚拟IP的漂移 获取管理脚本master_ip_failover cp master_ip_failover /usr/local/bin/ #脚本 ...

  2. MHA集群(gtid复制)和vip漂移

    在上一片博客中,讲述了怎么去配置MHA架构!这片博客不再细说,只说明其中MySQL主从搭建,这里使用的是gtid加上半同步复制! 步骤与上一片博客一样,不同之处在于MySQL主从的搭建!详细的gtid ...

  3. mysql高可用架构 -> MHA配置VIP漂移-05

    VIP漂移的两种方式 1)通过keepalived的方式,管理虚拟IP的漂移 2)通过MHA自带脚本方式,管理虚拟IP的漂移 MHA脚本方式 虚拟ip漂移的脚本下载地址 -> wget http ...

  4. 由VIP漂移引发的算法异常问题调查和解决

    最近工作中的一个问题,耗时一个月之久终于调查完毕且顺利解决,顿时感慨万千.耗时之久和预期解决时间和环境搭建以及日志不合理等等有关,当然这个并非此文的重点.之所以在很久以后的今天又开始写文,主要是这个问 ...

  5. keepalived vip漂移基本原理及选举算法

    keepalived可以将多个无状态的单点通过虚拟IP(以下称为VIP)漂移的方式搭建成一个高可用服务,常用组合比如 keepalived+nginx,lvs,haproxy和memcached等.它 ...

  6. (转)小谈keepalived vip漂移原理与VRRP协议

    背景:之前搭建过keepalived双机热备的集群,但对其中的原理不甚理解,看完就忘了,所有有必要深入的学习下. 简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术 ...

  7. 一主多从+Binlog Server,主库故障无法访问,如何在从库中选举一个新主库

    一.基本环境 VMware10.0+CentOS6.9+MySQL5.7.19 ROLE HOSTNAME BASEDIR DATADIR IP PORT M ZST1 /usr/local/mysq ...

  8. my08_mysqldump+binlog server备份

    备份策略描述 ******************************************* mysqldump备份适用于小数据量的备份,比如100G以下的数据量,就可以使用逻辑备份 举例两个 ...

  9. 利用binlog server及Xtrabackup备份集来恢复误删表(drop)

      Preface       Today I'm gonna test how to rescue a dropped table from binlog server based on a ful ...

随机推荐

  1. P2979 [USACO10JAN]奶酪塔Cheese Towers(完全背包,递推)

    题目描述 Farmer John wants to save some blocks of his cows' delicious Wisconsin cheese varieties in his ...

  2. Docker安装配置及华为云镜像加速

    Docker华为云镜像加速 软件介绍 支持的操作系统 docker安装 docker镜像加速(华为云实现) 1.登录华为云网站,注册华为云账户 2.登录华为云账户,点击网页右上角的控制台 3.点击左上 ...

  3. linux下的命令自动补齐增强

    linux 7 下 安装 bash-completion 可以实现命令的参数的自动补齐

  4. git 基本命令和操作

    设置全局用户名+密码 $ git config --global user.name 'runoob' $ git config --global user.email test@runoob.com ...

  5. Py数据类型—列表,字典,元组

    列表:数据类型list. 写法li=[1,12,9,"sdsad",["ad","dd"] ].用中括号括起来,用逗号分割每个元素列表中元素 ...

  6. 阿里云镜像仓库镜像迁移至私有Harbor

    下载镜像同步工具 wget https://goodrain-delivery.oss-cn-hangzhou.aliyuncs.com/boe/image-syncer && chm ...

  7. Trie 前缀树或字典树 确定有限状态自动机

    https://zh.wikipedia.org/wiki/Trie 应用 trie树常用于搜索提示.如当输入一个网址,可以自动搜索出可能的选择.当没有完全匹配的搜索结果,可以返回前缀最相似的可能.[ ...

  8. linux下mysql基于mycat做主从复制和读写分离之基础篇

    Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , my ...

  9. 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入。

    package com.minxinloan.utils; import java.math.BigDecimal; public class Arith { // 源文件Arith.java: /* ...

  10. 【Android初级】如何实现一个比相册更高大上的左右滑动特效(附源码)

    在Android里面,想要实现一个类似相册的左右滑动效果,我们除了可以用Gallery.HorizontalScrollView.ViewPager等控件,还可以用一个叫做 ViewFlipper 的 ...