mysql高可用之MHA(补充2)--邮件报警

当mha进行failover 完成或由于错误停止时,我们可以使用send_report以邮件报警的方式来获得failover报告,以便我们及时了解现在的数据库状态。

首先需要修改脚本:

[root@rd-mysql-test4 mha]# cat /usr/local/bin/send_report 
#!/usr/bin/perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';
use Mail::Sender;
use Getopt::Long;

#new_master_host and new_slave_hosts are set only when recovering master succeeded
my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );

my $smtp='smtp.163.com';
my $mail_from='from@163.com';
my $mail_user='from@163.com';
my $mail_pass='password';
#my $mail_to=['to1@qq.com','to2@qq.com'];
my $mail_to='to@qq.com';

GetOptions(
  'orig_master_host=s' => \$dead_master_host,
  'new_master_host=s'  => \$new_master_host,
  'new_slave_hosts=s'  => \$new_slave_hosts,
  'subject=s'          => \$subject,
  'body=s'             => \$body,
);

# Do whatever you want here
mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);

sub mailToContacts {
	my ($smtp, $mail_from, $mail_user, $mail_pass, $mail_to, $subject, $msg ) = @_;
	open my $DEBUG, ">/var/log/masterha/app1/mail.log"
		or die "Can't open the debug	file:$!\n";
	my $sender = new Mail::Sender {
		ctype		=> 'text/plain;charset=utf-8',
		encoding	=> 'utf-8',
		smtp		=> $smtp,
		from		=> $mail_from,
		auth		=> 'LOGIN',
		TLS_allowed	=> '0',
		authid		=> $mail_user,
		authpwd		=> $mail_pass,
		to		=> $mail_to,
		subject		=> $subject,
		debug		=> $DEBUG
	};
	$sender->MailMsg(
		{
			msg => $msg,
			debug => $DEBUG
		}
	) or print $Mail::Sender::Error;
	return 1;
}

exit 0;
然后修改配置文件,只需添加report_script即可

[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/data/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=123456
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=rep
report_script=/usr/local/bin/send_report
ssh_port=22
ssh_user=root
user=mha

[server1]
hostname=10.10.10.56
port=3306

[server2]
hostname=10.10.10.57
port=3306

[server3]
hostname=10.10.10.58
port=3306
最后开启mha监控,停止master来触发failover,在最后我们可以看到生成了failover报告并send_report。

----- Failover Report -----

app1: MySQL Master failover 10.10.10.57(10.10.10.57:3306) to 10.10.10.56(10.10.10.56:3306) succeeded

Master 10.10.10.57(10.10.10.57:3306) is down!

Check MHA Manager logs at rd-mysql-test4:/var/log/masterha/app1/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 10.10.10.57(10.10.10.57:3306)
The latest slave 10.10.10.56(10.10.10.56:3306) has all relay logs for recovery.
Selected 10.10.10.56(10.10.10.56:3306) as a new master.
10.10.10.56(10.10.10.56:3306): OK: Applying all logs succeeded.
10.10.10.56(10.10.10.56:3306): OK: Activated master IP address.
10.10.10.58(10.10.10.58:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.10.10.58(10.10.10.58:3306): OK: Applying all logs succeeded. Slave started, replicating from 10.10.10.56(10.10.10.56:3306)
10.10.10.56(10.10.10.56:3306): Resetting slave info succeeded.
Master failover to 10.10.10.56(10.10.10.56:3306) completed successfully.
Thu Aug 13 11:27:36 2015 - [info] Sending mail..
Unknown option: conf
我们收到的邮件如下:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MHA(Master High Availability)是一种用于MySQL数据库的可用架构解决方案。它由日本DeNA公司开发,旨在实现MySQL主从复制的自动故障转移和故障恢复。 MHA架构通常由以下几个组件组成: 1. MHA Manager:负责监控MySQL主节点的状态,并在主节点故障时自动切换到备节点。MHA Manager使用SSH连接到主节点,并通过观察二进制日志来检测主节点是否正常工作。 2. Master Agent:安装在所有MySQL主节点和备节点上的代理程序。它负责与MHA Manager通信,并在主节点故障时执行故障转移操作。Master Agent会自动将备节点提升为新的主节点。 3. Slave Agent:安装在备节点上的代理程序。它负责监控备节点的状态,并将备节点的状态信息发送给Master Agent。 MHA的工作流程如下: 1. MHA Manager定期检查主节点的状态。如果主节点无法正常工作(如网络故障、MySQL进程崩溃等),MHA Manager会发起故障转移操作。 2. 在故障转移过程中,MHA Manager会将一个备节点提升为新的主节点,并更新其他备节点的配置,使它们成为新主节点的从节点。 3. MHA Manager会使用SSH连接到新主节点,并在新主节点上启动MySQL进程,实现自动的故障恢复。 总结来说,MHA是一种基于MySQL主从复制的可用架构解决方案,能够自动监控和管理MySQL主节点和备节点,实现故障转移和故障恢复的自动化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值