def change_master(slave_hostaddr,
master_hostaddr,
master_log_file,
master_log_pos,
no_start=False):
""" Setup MySQL replication on new replica
Args:
slave_hostaddr - hostaddr object for the new replica
hostaddr - A hostaddr object for the master db
master_log_file - Replication log file to begin streaming
master_log_pos - Position in master_log_file
no_start - Don't run START SLAVE after CHANGE MASTER
"""
conn = connect_mysql(slave_hostaddr)
cursor = conn.cursor()
set_global_variable(slave_hostaddr, 'read_only', True)
reset_slave(slave_hostaddr)
master_user, master_password = get_mysql_user_for_role('replication')
parameters = {
'master_user': master_user,
'master_password': master_password,
'master_host': master_hostaddr.hostname,
'master_port': master_hostaddr.port,
'master_log_file': master_log_file,
'master_log_pos': master_log_pos
}
sql = ''.join(("CHANGE MASTER TO "
"MASTER_USER=%(master_user)s, "
"MASTER_PASSWORD=%(master_password)s, "
"MASTER_HOST=%(master_host)s, "
"MASTER_PORT=%(master_port)s, "
"MASTER_LOG_FILE=%(master_log_file)s, "
"MASTER_LOG_POS=%(master_log_pos)s "))
warnings.filterwarnings('ignore', category=MySQLdb.Warning)
cursor.execute(sql, parameters)
warnings.resetwarnings()
log.info(cursor._executed)
if not no_start:
start_replication(slave_hostaddr)
# Replication reporting is wonky for the first second
time.sleep(1)
# Avoid race conditions for zk update monitor
assert_replication_sanity(slave_hostaddr,
set([CHECK_SQL_THREAD, CHECK_IO_THREAD]))
评论列表
文章目录