1. 程式人生 > >不停服務做mysql從庫

不停服務做mysql從庫

mysql從庫掛掉,記下master節點然後匯出資料庫,匯入到從庫伺服器,最後修改slave的master資訊,有時候直接就好了,如果沒有好,比如匯出的時候主節點添加了很多資料,導致pos變掉了,當然會變,畢竟沒有停止資料庫。如果沒有好用下面的指令碼,執行下,跳過重複的資料就ok了

change master to master_host='111.111.111.111',master_user='slave_site',master_password='password', master_log_file='mysql-bin.000520',master_log_pos=146851479;

mysql_slave_skip.pl

#!/usr/bin/env perl
use strict;
use warnings;

# get slave status
sub get_status {
    my ($ip, $usr, $pass) = @_;
    my $info = `mysql -u$usr -p$pass -h$ip -e 'show slave status\\G;'`;
    if (($info =~ /Slave_IO_Running: Yes/) && ($info =~ /Slave_SQL_Running: No/)) {
        return 1;
    }
    return
0; } # mysql slave skip sub slaveskip { my ($ip, $usr, $pass) = @_; print "slave error **\n"; system("mysql -u$usr -p$pass -h$ip -e 'slave stop;'"); system("mysql -u$usr -p$pass -h$ip -e 'set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;'"); system("mysql -u$usr -p$pass -h$ip -e 'slave start;'"
); } sub stopslave { my ($ip, $usr, $pass) = @_; print "stop slave\n"; system("mysql -u$usr -p$pass -h$ip -e 'slave stop;'"); } my @hosts = qw/ localhost:root:A3NWgX4WQ4m22KvMU2a3Vc2vQ /; foreach (@hosts) { my ($ip, $usr, $pass) = split ':'; print "// ----- $ip\n"; my $oktimes=0; my $count = 1; while ($count < 100000) { my $status = get_status($ip, $usr, $pass); if($status==0){ $oktimes++; if($oktimes<50){ select(undef, undef, undef, 0.1); $count++; next; } else{ print "i: $count status: $status\n"; #stopslave($ip, $usr, $pass); last; } } else{ print "i: $count status: $status\n"; $oktimes=0; slaveskip($ip, $usr, $pass); select(undef, undef, undef, 0.1); $count++; } } print "\n"; } exit;