溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mysql高可用之MHA(補(bǔ)充2)--郵件報(bào)警

發(fā)布時(shí)間:2020-08-07 19:34:31 來源:ITPUB博客 閱讀:170 作者:18141908802 欄目:MySQL數(shù)據(jù)庫

當(dāng)mha進(jìn)行failover 完成或由于錯(cuò)誤停止時(shí),我們可以使用send_report以郵件報(bào)警的方式來獲得failover報(bào)告,以便我們及時(shí)了解現(xiàn)在的數(shù)據(jù)庫狀態(tài)。

首先需要修改腳本:


[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監(jiān)控,停止master來觸發(fā)failover,在最后我們可以看到生成了failover報(bào)告并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



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI