Advertisement
crtelinde

reset-slave

Mar 3rd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.87 KB | None | 0 0
  1. #!/user/bin/perl -w
  2.  
  3. #  file: reset-slave.pl
  4.  
  5. #  Copyright ©2005 MySQL AB
  6.  
  7. #  This program is free software; you can redistribute it and/or modify
  8. #  it under the terms of the GNU General Public License as published by
  9. #  the Free Software Foundation; either version 2 of the License, or
  10. #  (at your option) any later version.
  11.  
  12. #  This program is distributed in the hope that it will be useful,
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #  GNU General Public License for more details.
  16.  
  17. #  You should have received a copy of the GNU General Public License
  18. #  along with this program; if not, write to:
  19. #  Free Software Foundation, Inc.
  20. #  59 Temple Place, Suite 330
  21. #  Boston, MA 02111-1307 USA
  22. #
  23. #  Version 1.1
  24.  
  25.  
  26. ######################## Includes ###############################
  27.  
  28. use DBI;
  29.  
  30. ######################## Globals ################################
  31.  
  32. my  $m_host='';
  33. my  $m_port='';
  34. my  $m_user='';
  35. my  $m_pass='';
  36. my  $s_host='';
  37. my  $s_port='';
  38. my  $s_user='';
  39. my  $s_pass='';
  40. my  $dbhM='';
  41. my  $dbhS='';
  42.  
  43. ####################### Sub Prototypes ##########################
  44.  
  45. sub CollectCommandPromptInfo;
  46. sub ConnectToDatabases;
  47. sub DisconnectFromDatabases;
  48. sub GetSlaveEpoch;
  49. sub GetMasterInfo;
  50. sub UpdateSlave;
  51.  
  52. ######################## Program Main ###########################
  53.  
  54. CollectCommandPromptInfo;
  55. ConnectToDatabases;
  56. GetSlaveEpoch;
  57. GetMasterInfo;
  58. UpdateSlave;
  59. DisconnectFromDatabases;
  60.  
  61. ################## Collect Command Prompt Info ##################
  62.  
  63. sub CollectCommandPromptInfo
  64. {
  65.   ### Check that user has supplied correct number of command line args
  66.   die "Usage:\n
  67.       reset-slave >master MySQL host< >master MySQL port< \n
  68.                   >master user< >master pass< >slave MySQL host< \n
  69.                   >slave MySQL port< >slave user< >slave pass< \n
  70.       All 8 arguments must be passed. Use BLANK for NULL passwords\n"
  71.        unless @ARGV == 8;
  72.  
  73.   $m_host  =  $ARGV[0];
  74.   $m_port  =  $ARGV[1];
  75.   $m_user  =  $ARGV[2];
  76.   $m_pass  =  $ARGV[3];
  77.   $s_host  =  $ARGV[4];
  78.   $s_port  =  $ARGV[5];
  79.   $s_user  =  $ARGV[6];
  80.   $s_pass  =  $ARGV[7];
  81.  
  82.   if ($m_pass eq "BLANK") { $m_pass = '';}
  83.   if ($s_pass eq "BLANK") { $s_pass = '';}
  84. }
  85.  
  86. ###############  Make connections to both databases #############
  87.  
  88. sub ConnectToDatabases
  89. {
  90.   ### Connect to both master and slave cluster databases
  91.  
  92.   ### Connect to master
  93.   $dbhM
  94.     = DBI->connect(
  95.     "dbi:mysql:database=mysql;host=$m_host;port=$m_port",
  96.     "$m_user", "$m_pass")
  97.       or die "Can't connect to Master Cluster MySQL process!
  98.              Error: $DBI::errstr\n";
  99.  
  100.   ### Connect to slave
  101.   $dbhS
  102.     = DBI->connect(
  103.           "dbi:mysql:database=mysql;host=$s_host",
  104.           "$s_user", "$s_pass")
  105.     or die "Can't connect to Slave Cluster MySQL process!
  106.            Error: $DBI::errstr\n";
  107. }
  108.  
  109. ################  Disconnect from both databases ################
  110.  
  111. sub DisconnectFromDatabases
  112. {
  113.   ### Disconnect from master
  114.  
  115.   $dbhM->disconnect
  116.   or warn " Disconnection failed: $DBI::errstr\n";
  117.  
  118.   ### Disconnect from slave
  119.  
  120.   $dbhS->disconnect
  121.   or warn " Disconnection failed: $DBI::errstr\n";
  122. }
  123.  
  124. ######################  Find the last good GCI ##################
  125.  
  126. sub GetSlaveEpoch
  127. {
  128.   $sth = $dbhS->prepare("SELECT MAX(epoch)
  129.                         FROM mysql.ndb_apply_status;")
  130.       or die "Error while preparing to select epoch from slave: ",
  131.              $dbhS->errstr;
  132.  
  133.   $sth->execute
  134.       or die "Selecting epoch from slave error: ", $sth->errstr;
  135.  
  136.   $sth->bind_col (1, \$epoch);
  137.   $sth->fetch;
  138.   print "\tSlave Epoch =  $epoch\n";
  139.   $sth->finish;
  140. }
  141.  
  142. #######  Find the position of the last GCI in the binary log ########
  143.  
  144. sub GetMasterInfo
  145. {
  146.   $sth = $dbhM->prepare("SELECT
  147.                           SUBSTRING_INDEX(File, '/', -1), Position
  148.                         FROM mysql.ndb_binlog_index
  149.                         WHERE epoch > $epoch
  150.                         ORDER BY epoch ASC LIMIT 1;")
  151.       or die "Prepare to select from master error: ", $dbhM->errstr;
  152.  
  153.   $sth->execute
  154.       or die "Selecting from master error: ", $sth->errstr;
  155.  
  156.   $sth->bind_col (1, \$binlog);
  157.   $sth->bind_col (2, \$binpos);
  158.   $sth->fetch;
  159.   print "\tMaster binary log =  $binlog\n";
  160.   print "\tMaster binary log position =  $binpos\n";
  161.   $sth->finish;
  162. }
  163.  
  164. ##########  Set the slave to process from that location #########
  165.  
  166. sub UpdateSlave
  167. {
  168.   $sth = $dbhS->prepare("CHANGE MASTER TO
  169.                         MASTER_LOG_FILE='$binlog',
  170.                         MASTER_LOG_POS=$binpos;")
  171.       or die "Prepare to CHANGE MASTER error: ", $dbhS->errstr;
  172.  
  173.   $sth->execute
  174.        or die "CHANGE MASTER on slave error: ", $sth->errstr;
  175.   $sth->finish;
  176.   print "\tSlave has been updated. You may now start the slave.\n";
  177. }
  178.  
  179. # end reset-slave.pl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement