Switch master

From Wikitech
(Difference between revisions)
Jump to: navigation, search
 
(17 intermediate revisions by 7 users not shown)
Line 1: Line 1:
To switch masters:
+
== The Fairly Easy Way (asher uses this, domas did something similar) ==
  
* Check that the new master has binlog enabled. All slaves should be configured this way, for convenience.  
+
# cd to your svn checkout of [https://svn.wikimedia.org/viewvc/mediawiki/trunk/tools/switch-master/ trunk/tools/switch-master]
 +
# Have a <code>~/.my.cnf</code> setup with the root mysql password
 +
# Create a config file according to <code>config.sample</code> for the cluster you're switching. Make sure the secondary master in in the slaves section! (it can be added in for the form of sX-secondary)
 +
# Edit [https://gerrit.wikimedia.org/r/gitweb?p=operations/mediawiki-config.git;a=blob;f=wmf-config/db.php <code>db.php</code>], moving the new master to position 0.
 +
## Commit to the repository in this form (the post-switch version)
 +
## Resume editing to comment out the old / current master (to drain it of all connections before the switch instead of sending a bunch of new slave queries to it - this results in a quicker swap) and to add this cluster to <code>readOnlyBySection</code>.
 +
## <code>sync-file wmf-config/db.php "switching master for sX to dbXX"</code>
 +
# run <code>./switch</code> and step through it
 +
# <code>git checkout wmf-config/db.php</code> (reverts local changes from step 4.2)
 +
# <code>sync-file wmf-config/db.php "completed master switch for sX"</code>
 +
# make sure to <code>!log</code> the new master binlog and position for the cluster in #wikimedia-operations
 +
# edit DNS to update the sX-master cname
 +
# edit puppet to update the $masters array in mysql.pp
  
cd [http://svn.wikimedia.org/viewvc/mediawiki/trunk/tools/switch-master/ ~tstarling/src/tools/switch-master]
+
== The easy way (needs some updating) ==
 +
: '''''DO NOT USE THIS IF THE MASTER IS DOWN ALREADY''''' (unless you really know what you are doing)
  
* Edit config.others or config.enwiki depending on which master you are switching. Then copy it to config. Config is the file that is actually used.
+
* NOTE: This tool doesn't currently switch the secondary master in the replication tree - '''make sure that is switched as well!!'''  You should also update the $masters section in mysql.pp, and update the $cluster-master or $cluster-secondary dns cnames - they must be accurate for heartbeat monitoring.
* Log in to the mysql on the old master as root. Check for any long running queries. Kill them. If there is a single long-running query such as a backup, which will restart after you kill it, then get its thread ID ready in a kill command, ready to press enter later.
+
* Set read-only mode in db.php, sync it.
+
* Start the script
+
  
./switch
+
<pre>
 +
cd /home/wikipedia/src/mediawiki/tools/switch-master
 +
php switch.php <old master> <new master>
 +
</pre>
  
* Press enter, this will attempt to switch the master to read-only. If there are long-running queries, kill them now. The script will block at FLUSH TABLES until you do.
+
==The hard way==
 +
 
 +
The script above (switch.php) does the following tasks, which you can do manually if you feel that way inclined:
 +
 
 +
* Check that the new master has binlog enabled. All slaves should be configured this way, for convenience. All servers, including masters, should have read_only mode on by default.
 +
* Run RESET MASTER on the new master
 +
* Log in to the mysql on the old master as root. Check for any long running queries. Kill them. If there is a single long-running query such as a backup, which will restart after you kill it, then get its thread ID ready in a kill command, ready to press enter later.
 +
* Set read-only mode in db.php, sync it.
 +
* Set read-only mode on the old master using SET GLOBAL read_only=1;
 +
* Run FLUSH TABLES on the old master. This will block until all queries complete, so you may need to kill some now.
 
* Check for slave lag. Slave lag must be zero on the new master before you continue.
 
* Check for slave lag. Slave lag must be zero on the new master before you continue.
* Press enter a few more times until it's done, these steps usually work. There may be an error from the old master saying that SLAVE STOP requires a running slave, you can ignore it.
+
* Run STOP SLAVE on the new master
 +
* Run SHOW MASTER STATUS on the new master to get the new replication position
 +
* Run a CHANGE MASTER TO query on each of the slaves including the old master
 +
* Set read/write mode on the new master using SET GLOBAL read_only=0;
 
* Edit db.php again, turning off read-only mode and simultaneously switching the loads configuration.
 
* Edit db.php again, turning off read-only mode and simultaneously switching the loads configuration.
* Edit my.cnf on the old master and the new master, setting read_only mode appropriately
+
* Run RESET SLAVE on the new master, to prevent it from replicating the old master after restart.
 +
 
 +
What could be easier than managing a replicated cluster in MySQL?
 +
 
 +
 
 +
[[Category:How-To]]
 +
[[Category:MySQL]]

Latest revision as of 16:36, 27 May 2012

[edit] The Fairly Easy Way (asher uses this, domas did something similar)

  1. cd to your svn checkout of trunk/tools/switch-master
  2. Have a ~/.my.cnf setup with the root mysql password
  3. Create a config file according to config.sample for the cluster you're switching. Make sure the secondary master in in the slaves section! (it can be added in for the form of sX-secondary)
  4. Edit db.php, moving the new master to position 0.
    1. Commit to the repository in this form (the post-switch version)
    2. Resume editing to comment out the old / current master (to drain it of all connections before the switch instead of sending a bunch of new slave queries to it - this results in a quicker swap) and to add this cluster to readOnlyBySection.
    3. sync-file wmf-config/db.php "switching master for sX to dbXX"
  5. run ./switch and step through it
  6. git checkout wmf-config/db.php (reverts local changes from step 4.2)
  7. sync-file wmf-config/db.php "completed master switch for sX"
  8. make sure to !log the new master binlog and position for the cluster in #wikimedia-operations
  9. edit DNS to update the sX-master cname
  10. edit puppet to update the $masters array in mysql.pp

[edit] The easy way (needs some updating)

DO NOT USE THIS IF THE MASTER IS DOWN ALREADY (unless you really know what you are doing)
  • NOTE: This tool doesn't currently switch the secondary master in the replication tree - make sure that is switched as well!! You should also update the $masters section in mysql.pp, and update the $cluster-master or $cluster-secondary dns cnames - they must be accurate for heartbeat monitoring.
cd /home/wikipedia/src/mediawiki/tools/switch-master
php switch.php <old master> <new master>

[edit] The hard way

The script above (switch.php) does the following tasks, which you can do manually if you feel that way inclined:

  • Check that the new master has binlog enabled. All slaves should be configured this way, for convenience. All servers, including masters, should have read_only mode on by default.
  • Run RESET MASTER on the new master
  • Log in to the mysql on the old master as root. Check for any long running queries. Kill them. If there is a single long-running query such as a backup, which will restart after you kill it, then get its thread ID ready in a kill command, ready to press enter later.
  • Set read-only mode in db.php, sync it.
  • Set read-only mode on the old master using SET GLOBAL read_only=1;
  • Run FLUSH TABLES on the old master. This will block until all queries complete, so you may need to kill some now.
  • Check for slave lag. Slave lag must be zero on the new master before you continue.
  • Run STOP SLAVE on the new master
  • Run SHOW MASTER STATUS on the new master to get the new replication position
  • Run a CHANGE MASTER TO query on each of the slaves including the old master
  • Set read/write mode on the new master using SET GLOBAL read_only=0;
  • Edit db.php again, turning off read-only mode and simultaneously switching the loads configuration.
  • Run RESET SLAVE on the new master, to prevent it from replicating the old master after restart.

What could be easier than managing a replicated cluster in MySQL?

Personal tools
Namespaces

Variants
Actions
Navigation
Ops documentation
Wiki
Toolbox