Spec-Zone .ru
спецификации, руководства, описания, API
|
It is possible to automate much of the process described in the previous section (see Section
17.6.9, "MySQL Cluster Backups With MySQL Cluster Replication"). The following Perl script reset-slave.pl
serves as an example of how you can do this.
#!/user/bin/perl -w# file: reset-slave.pl# Copyright ©2005 MySQL AB# 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:# Free Software Foundation, Inc.# 59 Temple Place, Suite 330# Boston, MA 02111-1307 USA## Version 1.1######################## Includes ###############################use DBI;######################## Globals ################################my $m_host='';my $m_port='';my $m_user='';my $m_pass='';my $s_host='';my $s_port='';my $s_user='';my $s_pass='';my $dbhM='';my $dbhS='';####################### Sub Prototypes ##########################sub CollectCommandPromptInfo;sub ConnectToDatabases;sub DisconnectFromDatabases;sub GetSlaveEpoch;sub GetMasterInfo;sub UpdateSlave;######################## Program Main ###########################CollectCommandPromptInfo;ConnectToDatabases;GetSlaveEpoch;GetMasterInfo;UpdateSlave;DisconnectFromDatabases;################## Collect Command Prompt Info ##################sub CollectCommandPromptInfo{ ### Check that user has supplied correct number of command line args die "Usage:\n reset-slave >master MySQL host< >master MySQL port< \n >master user< >master pass< >slave MySQL host< \n >slave MySQL port< >slave user< >slave pass< \n All 8 arguments must be passed. Use BLANK for NULL passwords\n" unless @ARGV == 8; $m_host = $ARGV[0]; $m_port = $ARGV[1]; $m_user = $ARGV[2]; $m_pass = $ARGV[3]; $s_host = $ARGV[4]; $s_port = $ARGV[5]; $s_user = $ARGV[6]; $s_pass = $ARGV[7]; if ($m_pass eq "BLANK") { $m_pass = '';} if ($s_pass eq "BLANK") { $s_pass = '';}}############### Make connections to both databases #############sub ConnectToDatabases{ ### Connect to both master and slave cluster databases ### Connect to master $dbhM = DBI->connect( "dbi:mysql:database=mysql;host=$m_host;port=$m_port", "$m_user", "$m_pass") or die "Can't connect to Master Cluster MySQL process! Error: $DBI::errstr\n"; ### Connect to slave $dbhS = DBI->connect( "dbi:mysql:database=mysql;host=$s_host", "$s_user", "$s_pass") or die "Can't connect to Slave Cluster MySQL process! Error: $DBI::errstr\n";}################ Disconnect from both databases ################sub DisconnectFromDatabases{ ### Disconnect from master $dbhM->disconnect or warn " Disconnection failed: $DBI::errstr\n"; ### Disconnect from slave $dbhS->disconnect or warn " Disconnection failed: $DBI::errstr\n";}###################### Find the last good GCI ##################sub GetSlaveEpoch{ $sth = $dbhS->prepare("SELECT MAX(epoch) FROM mysql.ndb_apply_status;") or die "Error while preparing to select epoch from slave: ", $dbhS->errstr; $sth->execute or die "Selecting epoch from slave error: ", $sth->errstr; $sth->bind_col (1, \$epoch); $sth->fetch; print "\tSlave Epoch = $epoch\n"; $sth->finish;}####### Find the position of the last GCI in the binary log ########sub GetMasterInfo{ $sth = $dbhM->prepare("SELECT SUBSTRING_INDEX(File, '/', -1), Position FROM mysql.ndb_binlog_index WHERE epoch > $epoch ORDER BY epoch ASC LIMIT 1;") or die "Prepare to select from master error: ", $dbhM->errstr; $sth->execute or die "Selecting from master error: ", $sth->errstr; $sth->bind_col (1, \$binlog); $sth->bind_col (2, \$binpos); $sth->fetch; print "\tMaster binary log = $binlog\n"; print "\tMaster binary log position = $binpos\n"; $sth->finish;}########## Set the slave to process from that location #########sub UpdateSlave{ $sth = $dbhS->prepare("CHANGE MASTER TO MASTER_LOG_FILE='$binlog', MASTER_LOG_POS=$binpos;") or die "Prepare to CHANGE MASTER error: ", $dbhS->errstr; $sth->execute or die "CHANGE MASTER on slave error: ", $sth->errstr; $sth->finish; print "\tSlave has been updated. You may now start the slave.\n";}# end reset-slave.pl