首页 > 学技术 > 技术网文 > MySQL > 正文

[保留] 贡献一个mysql replication出错自动检测的脚本


来源 chinaunix.net 酷勤网整理

需要DBIX及Mail等perl模块
注意,虽然能够自动修复,但是可能造成slave的数据条目少于master的条目!使用的时候自己注意些好了
本脚本是修改版,原作者是Jeremy Zawodny

#!/usr/local/bin/perl -w

#
# fix mysql replication if it encounters a problem
# Useage:perl fix_repl.pl ip1 ip2 ip3

$|=1;      # unbuffer stdout
use strict;
use DBIx::DWIW;
use Mail::Mailer;
my $ip;
foreach $ip (@ARGV) {
&fixdb($ip);
}

exit;

sub fixdb()
{
my $host = shift || 'localhost';
my $conn = DBIx::DWIW->;Connect(
DB   =>; "test",
User =>; "sqlmon",
Pass =>; '!@#$%^&*()',
    Host =>; $host,
Port  =>; 3306) or die "Couldn't connect to database!";

print "checking $host ... \n";
my $info = $conn->;Hash("SHOW SLAVE STATUS") or die $@;
my @version = $conn->;Array("SHOW VARIABLES LIKE 'Version'");
my $fix_cmd;
my $start_cmd;
# slave not start
if ($info->;{Slave_IO_Running} eq 'No' and $info->;{Slave_SQL_Running} eq 'No')
{
$fix_cmd = "SET SQL_SLAVE_SKIP_COUNTER = 1";
$start_cmd = "SLAVE START";
}
# 4.0.0 - 4.0.2
elsif ($version[1] =~ /^4\.0\.[012]/ and $info->;{Slave_SQL_Running} eq 'No')
{
$fix_cmd = "SET SQL_SLAVE_SKIP_COUNTER = 1";
$start_cmd = "SLAVE START SQL_THREAD";
}
# 4.0.3 - 4.0.xx, 4.1.xx.  Don't know what 5.0 will be like.
elsif ($version[1] =~ /^4\.[01]\./ and $info->;{Slave_SQL_Running} eq 'No')
{
$fix_cmd = "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1";
$start_cmd = "SLAVE START SQL_THREAD";
}
# things are okay or unknown version?
else
{
print "GOOD\n";
}
if ($info->;{Slave_IO_Running} eq 'No'  or  $info->;{Slave_SQL_Running} eq 'No' ){
&mailtoadmin($info,$host);
print "FIXING ... ";
$conn->;Execute($fix_cmd);
$conn->;Execute($start_cmd);
print "DONE\n";
}

}

sub mailtoadmin(){
        my $body=shift;
my $host = shift;
        my @MailTo=("xxxx\@yyyy.com");
        my $subject = "Mysql Self fix replication on $host ";
my $mailer = Mail::Mailer->;new() or die $@;
$mailer->;open({ From    =>; $host,
To      =>; \@MailTo,
Subject =>; $subject,
})or die "Can't open: $!\n";
#    while ( my ($key, $value) = each(%$body) ) {
#        print $mailer "$key =>; $value\n";
#    }
print $mailer <<MAILCONTENT;

*************************** show slave status***************************

Master_Host =>;  $body->;{'Master_Host'}
Master_User =>;  $body->;{'Master_User'}
Master_Port =>;  $body->;{'Master_Port'}
Connect_retry =>;  $body->;{'Connect_retry'}
Master_Log_File =>;  $body->;{'Master_Log_File'}
Read_Master_Log_Pos =>;$body->;{'Read_Master_Log_Pos'}
Relay_Log_File =>;  $body->;{'Relay_Log_File'}
Relay_Log_Pos =>;  $body->;{'Relay_Log_Pos'}
Relay_Master_Log_File =>;$body->;{'Relay_Master_Log_File'}
Slave_IO_Running =>;$body->;{'Slave_IO_Running'}
Slave_SQL_Running =>;$body->;{'Slave_SQL_Running'}
Replicate_do_db =>;   $body->;{'Replicate_do_db'}
Replicate_ignore_db =>;$body->;{'Replicate_ignore_db'}
Last_errno =>;    $body->;{'Last_errno'}
Last_error =>;    $body->;{'Last_error'}
Skip_counter =>;    $body->;{'Skip_counter'}
Exec_master_log_pos =>; $body->;{'Exec_master_log_pos'}
Relay_log_space =>;    $body->;{'Relay_log_space'}
MAILCONTENT
$mailer->;close();
}





 yejr 回复于:2005-01-05 17:14:38

呵呵,谢谢楼主,其实如果用shell来做的话,是粉简单的,而且以前也已经有现成的了.


 odin_free 回复于:2005-01-06 10:18:00

不错~~~保留


 qinshiling 回复于:2005-03-29 17:11:30

看你的脚本作用不错,能高诉我怎么用吗?


 qinshiling 回复于:2005-03-29 17:14:08

看你的脚本作用不错,能高诉我怎么用吗?


 king1981 回复于:2006-10-28 18:50:53

yejr 你中文的MYSQL 手册能打个包给我吗?
先谢谢拉.
[email]ba20000@163.com[/email]


 qlks 回复于:2006-10-29 10:36:15

不错
不过生产环境中还是shell脚本来的方便


 grfjs 回复于:2007-01-09 16:54:18

引用:原帖由 yejr 于 2005-1-5 17:14 发表
呵呵,谢谢楼主,其实如果用shell来做的话,是粉简单的,而且以前也已经有现成的了. 



请版主帮帮忙,能提供shell脚本的连接吗? 我翻了好久以前的帖子都没翻到啊 :em16:


 yejr 回复于:2007-01-09 20:24:00

http://imysql.cn/?q=node/77
中的 mysql日常维护 里头有类似的


 yueliangdao0608 回复于:2007-01-10 09:04:10

你可以把perl的翻译成shell


 Jeff_z 回复于:2007-01-11 00:17:24

不错,顶一下




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=477712

收藏到: