实现 MHA VIP 功能

配置 master_ip_failover 脚本(db3)

把 master_ip_failover 上传到 /iba/software 上

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 = '192.168.31.88/24';

my $key = '1';

my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";

my $ssh_stop_vip = "/sbin/ifconfig eth0:$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";

}

</details>

cd /iba/software/

cp /iba/software/master_ip_failover /usr/local/bin/

修改脚本参数,改为自己的VIP,网卡设备

vi /usr/local/bin/master_ip_failover

my $vip = '192.168.31.88/24';

my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";

my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

修改 mha 配置

vi /etc/mha/app1.cnf

在 [server default] 下添加

master_ip_failover_script=/usr/local/bin/master_ip_failover

yum install dos2unix -y

dos2unix /usr/local/bin/master_ip_failover

chmod +x /usr/local/bin/master_ip_failover

### 在主库上手工生成第一个vip地址

ifconfig eth0:1 192.168.31.88/24

手动停止网卡的方法:ifconfig eth0:1 down

### 重启 MHA manager 使配置生效(db3)

masterha_stop --conf=/etc/mha/app1.cnf

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &

tail -f /var/log/mha/app1/manager.log

### 测试

停止主库

systemctl stop mysqld

查看 ip, 发现 vip 已经飘走

ip add

在 db3 上执行

masterha_check_repl --conf=/etc/mha/app1.cnf

结果显示 主库现在为 db1 ,经查看,vip 成功切换到 db1 上。


## 实现邮件报警
### 编写发邮件脚本(db3)

在 [server default] 下添加

vi /etc/mha/app1.cnf

report_script=/usr/local/bin/send_report


### 文件/usr/local/bin/send_report 内容

!/usr/bin/python

-- coding: UTF-8 --

import smtplib

from email.mime.text import MIMEText

from email.header import Header

第三方 SMTP 服务

mail_host="smtp.126.com" #设置服务器

mail_user="xxxxx@126.com" #用户名

mail_pass="xxxxx" #口令

sender = 'xxxxx@126.com'

receivers = 'yyyyyy@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message = MIMEText('MHA master 已经切换, 请查看', 'plain', 'utf-8')

message['From'] = sender

message['To'] = receivers

subject = 'MySQL MHA 邮件'

message['Subject'] = Header(subject, 'utf-8')

try:

smtpObj = smtplib.SMTP()

smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号

smtpObj.login(mail_user,mail_pass)

smtpObj.sendmail(sender, receivers, message.as_string())

print "邮件发送成功"

except smtplib.SMTPException, e:

print "Error:", evi /usr/local/bin/send_report

### 添加执行权限

chmod + x /usr/local/bin/send_report

### 重启 mha

masterha_stop --conf=/etc/mha/app1.cnf

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &

### MySQL 主库 down 时,切换成功发送邮件
![](https://img2018.cnblogs.com/blog/1334255/201912/1334255-20191204205056518-872668.png)
### 邮件发送成功

MySQL 部署 MHA 高可用架构 (二)的更多相关文章

  1. MySQL 部署 MHA 高可用架构 (一)

    MHA 官方网址 Manager : https://github.com/yoshinorim/mha4mysql-manager Node : https://github.com/yoshino ...

  2. MySQL系列:高可用架构之MHA

    前言 从11年毕业到现在,工作也好些年头,入坑mysql也有近四年的时间,也捣鼓过像mongodb.redis.cassandra.neo4j等Nosql数据库.其实一直想写博客分享下工作上的零零碎碎 ...

  3. MySQL数据库的优化(下)MySQL数据库的高可用架构方案

    MySQL数据库的优化(下)MySQL数据库的高可用架构方案 2011-03-09 08:53 抚琴煮酒 51CTO 字号:T | T 在上一篇MySQL数据库的优化中,我们跟随笔者学习了单机MySQ ...

  4. mysql +keeplive+drbd高可用架构(MHA基于监听端口VIP的高可用)

    1MySQL+DRBD+keepalived高可用架构 DRBD(DistributedReplicatedBlockDevice)是一个基于块设备级别在远程服务器直接同步和镜像数据的开源软件,类似于 ...

  5. MHA高可用架构与Atlas读写分离

    1.1 MHA简介 1.1.1 MHA软件介绍 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton ...

  6. 浅谈MySQL集群高可用架构

    前言 高可用架构对于互联网服务基本是标配,无论是应用服务还是数据库服务都需要做到高可用.对于一个系统而言,可能包含很多模块,比如前端应用,缓存,数据库,搜索,消息队列等,每个模块都需要做到高可用,才能 ...

  7. mysql集群高可用架构

    前言 高可用架构对于互联网服务基本是标配,无论是应用服务还是数据库服务都需要做到高可用.对于一个系统而言,可能包含很多模块,比如前端应用,缓存,数据库,搜索,消息队列等,每个模块都需要做到高可用,才能 ...

  8. mysql +keeplive+drbd高可用架构

    1MySQL+DRBD+keepalived高可用架构 DRBD(DistributedReplicatedBlockDevice)是一个基于块设备级别在远程服务器直接同步和镜像数据的开源软件,类似于 ...

  9. MHA 高可用架构部署

    一, MHA 介绍 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公 ...

随机推荐

  1. vscode自动修复eslint规范的插件及配置

    在开发大型项目中,经常都是需要多人合作的.相信大家一定都非常头疼于修改别人的代码的吧,而合理的使用eslint规范可以让我们在代码review时变得轻松,也可以让我们在修改小伙伴们的代码的时候会更加清 ...

  2. 剑指offer 27:二叉搜索树与双向链表

    题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 解题思路 采用中序遍历遍历二叉树,利用二叉排序树的特性,顺次连接节点,形成 ...

  3. Oracle数据库创建只读用户

    创建一个只读用户user2,只能查询管理员用户user1下的表,不能增删改. 操作环境:PL/SQL Developer 1.用管理员用户user1登录,创建用户user2.并授权 --创建用户use ...

  4. 修改uid gid 的起始范围

    UID_MAX 60000 # # Min/max values for automatic gid selection in groupadd # GID_MIN 500 GID_MAX 60000 ...

  5. python xlwt写入excel操作

    引用https://www.cnblogs.com/python-robot/p/9958352.html 安装 $ pip install xlwt 例子: import xlwt # 创建一个wo ...

  6. go语言设计模式之memento

    memento.go package memento import ( "fmt" ) type State struct { Description string } type ...

  7. 整理几个经常在H5移动端开发遇到的东西。

    本篇主要是我个人的学习分享. 1.弹出数字键盘 <!-- 有“#” “*” 符号输入 --> <input type="tel"> <!-- 纯数字 ...

  8. 201871010112-梁丽珍《面向对象程序设计(java)》第一周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接>    https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址> ...

  9. Acwing 14. 不修改数组找出重复的数字

    题目地址  https://www.acwing.com/problem/content/description/15/ 来源:剑指Offer 给定一个长度为 n+1n+1 的数组nums,数组中所有 ...

  10. acwing 49. 二叉搜索树与双向链表

    地址:https://www.acwing.com/problem/content/87/ 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表. 要求不能创建任何新的结点,只能调整树中结点指针 ...