Skip to content

Commit f7d7dfd

Browse files
committed
Update MySQL replication tasks
Change the purpose of the task `setup_slave_from_master` so that it does actually do the setup. Rename the existing task to `replicate_slave_from_master` because that's what it's actually doing.
1 parent d539e90 commit f7d7dfd

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

mysql.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fabric.api import *
2+
from fabric.operations import prompt
23

34

45
def run_mysql_command(cmd):
@@ -40,14 +41,37 @@ def fix_replication_from_slow_query_log_after_upgrade():
4041
@task
4142
def setup_slave_from_master(master):
4243
"""
43-
Sets up a slave from a master by taking a dump from the master,
44-
copying it to the slave and then restoring the dump.
44+
Sets up a slave from a master by:
45+
- configuring MySQL replication config
46+
- using the replicate_slave_from_master task to do an initial dump to the slave
4547
4648
Usage: fab environment -H mysql-slave-1.backend mysql.setup_slave_from_master:'mysql-master-1.backend'
4749
"""
4850
if len(env.hosts) > 1:
4951
exit('This job is currently only setup to run against one slave at a time')
5052

53+
mysql_master = prompt("Master host (eg 'master.mysql' or 'whitehall-master.mysql'):")
54+
replication_username = 'replica_user'
55+
replication_password = prompt("Password for MySQL user {0}:".format(replication_username))
56+
57+
run_mysql_command("STOP SLAVE;")
58+
run_mysql_command("CHANGE MASTER TO MASTER_HOST='{0}', MASTER_USER='{1}', MASTER_PASSWORD='{2}';".format(
59+
mysql_master, replication_username, replication_password))
60+
61+
replicate_slave_from_master(master)
62+
63+
64+
@task
65+
def replicate_slave_from_master(master):
66+
"""
67+
Updates a slave from a master by taking a dump from the master,
68+
copying it to the slave and then restoring the dump.
69+
70+
Usage: fab environment -H mysql-slave-1.backend mysql.replicate_slave_from_master:'mysql-master-1.backend'
71+
"""
72+
if len(env.hosts) > 1:
73+
exit('This job is currently only setup to run against one slave at a time')
74+
5175
with settings(host_string=master):
5276
# The use of `--master-data` here implies `--lock-all-tables` per the
5377
# MySQL reference manual: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_master-data

0 commit comments

Comments
 (0)