Chinaunix
标题:
贡献一个mysql replication出错自动检测的脚本
[打印本页]
作者:
平凡的香草
时间:
2005-01-05 11:13
标题:
贡献一个mysql replication出错自动检测的脚本
需要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
标题:
贡献一个mysql replication出错自动检测的脚本
呵呵,谢谢楼主,其实如果用shell来做的话,是粉简单的,而且以前也已经有现成的了.
作者:
odin_free
时间:
2005-01-06 10:18
标题:
贡献一个mysql replication出错自动检测的脚本
不错~~~保留
作者:
qinshiling
时间:
2005-03-29 17:11
标题:
贡献一个mysql replication出错自动检测的脚本
看你的脚本作用不错,能高诉我怎么用吗?
作者:
qinshiling
时间:
2005-03-29 17:14
标题:
贡献一个mysql replication出错自动检测的脚本
看你的脚本作用不错,能高诉我怎么用吗?
作者:
king1981
时间:
2006-10-28 18:50
yejr 你中文的MYSQL 手册能打个包给我吗?
先谢谢拉.
ba20000@163.com
作者:
qlks
时间:
2006-10-29 10:36
不错
不过生产环境中还是shell脚本来的方便
作者:
grfjs
时间:
2007-01-09 16:54
原帖由
yejr
于 2005-1-5 17:14 发表
呵呵,谢谢楼主,其实如果用shell来做的话,是粉简单的,而且以前也已经有现成的了.
请版主帮帮忙,能提供shell脚本的连接吗? 我翻了好久以前的帖子都没翻到啊
作者:
yejr
时间:
2007-01-09 20:24
http://imysql.cn/?q=node/77
中的 mysql日常维护 里头有类似的
作者:
yueliangdao0608
时间:
2007-01-10 09:04
你可以把perl的翻译成shell
作者:
Jeff_z
时间:
2007-01-11 00:17
不错,顶一下
作者:
cnhero
时间:
2009-07-28 21:23
谢谢啊
作者:
Coolriver
时间:
2009-07-29 14:16
不错顶一下.
Perl和Shell比有一个好处,可以保证会话在同一个会话中.所以推荐用Perl吧.
作者:
freeterman
时间:
2011-11-29 17:36
mysql 如果有批处理可以这样搞 有好了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2