- 论坛徽章:
- 0
|
需要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();
- }
复制代码 |
|