N:1
Replication
meets MHA
2014/07/11 MySQL Casual Talks vol6
do_aki
@do_aki
@do_aki
http://do-aki.net/
About N:1 Replication
• Multi Master to Single Slave
Replication
• a.k.a Multi-source Replication
– MariaDB 10 (https://mariadb.com/kb/en/mariadb/mariadb-
documentation/replication-cluster-multi-master/replication/multi-
source-replication/)
– MySQL 5.7 (labs)
(http://www.mysqlperformanceblog.com/2013/10/02/mysql-5-7-multi-
source-replication/)
• N:1 Replication work on MySQL5.0!
Master
A
Slave
A+B
Master
B
N:1 Replication
Master
A
Slave
A+B
Master
B
CHANGE
MATER TO
‘Master A’
repeat for
ever per
2seconds
Master
A
Slave
A+B
Master
B
CHANGE
MATER TO
‘Master B’
repeat for
ever per
2seconds
Master
A
Slave
A1
Slave
A2
Slave
B1
Master
B
Slave
B2
table
foo
bar
…
table
hoge
piyo
…
Application
before
(weak)
relation
Master
A
Slave
A1
Slave
A2
Slave
B1
Master
B
Slave
B2
table
foo
bar
…
table
hoge
piyo
…
Application
problem
weak
relation
Master
A
Slave
A1
Slave
A2
Slave
B1
Master
B
Slave
B2
Slave
A+B
SELECT
with JOIN
Application
objective
Flowchart of N:1 Replication
wait for
Seconds_Behind_Master = 0
START SLAVE
STOP SLAVE IO_THREAD
MASTER_POS_WAIT(Master_Log_F
ile,Read_Master_Log_Pos)
Timeout? START SLAVE IO_THREAD
STOP SLAVE
CHANGE MASTER TO ‘next
master’
Yes
No
I talked about above at 3 years ago
Current N:1 Replication
• Work in my production for 3 years
• I almost never use time to
maintenance
• can’t keep pace in some case
– pt-online-schema-change
– master server changed
– need restart (relatively trouble) 
https://github.com/do-aki/n1repl
temporary rotation stop
• n1repl_command.pl stop
– stop rotate replication connection
– single replication is alive
• n1repl_command.pl start
– start rotate replication connection
support changed master server
n1repl_command.pl switch 
--orig_master_host = masterA 
--orig_master_port = 3306 
--orig_master_log_file = bin.00x
--orig_master_log_pos = 893
--new_master_host = masterB 
--new_master_pos = 3306 
--new_master_log_file = bin.00x 
--new_master_log_pos = 1129
A’
n1repl switch (before)
A
A+B
B
A’
n1repl swith (after)
A
A+B
B
support
MHA for
MySQL?
MHA for MySQL
• Master High Availability Manager and
tools for MySQL
(https://code.google.com/p/mysql-master-ha/)
• Automating master failover
– slave promotes to master when master
goes down
– include “Graceful master switch”
function
(masterha_master_switch --master_state=alive)
http://www.slideshare.net/matsunobu/automated-master-failover/44
MHA (master switch) 1
current master
(orig_master)
other
slaves
candidate of
master
(new_master)
write
MHA (master switch) 2
write
record binlog filename and position
(orig_master_log_file, orig_master_log_pos)
MHA (master switch) 3
MASTER_POS_WAIT(
orig_master_log_file,
orig_master_log_pos)
record binlog
(new_master_log_file,
new_master_log_pos)
write
MHA (master switch) 4
other slaves:
MASTER_POS_WAIT(
orig_master_log_file,
orig_master_log_pos)
CHANGE MASTER TO ‘new_master’
(new_master_log_file,
new_master_log_pos)
write
write
MHA (master switch) 5
write
complete!
similar mechanism
• wait replication delay is zero
• change connection
• specify same (orig->new) position
• it’s looked N:1 Replication code
support changed master server
n1repl_command.pl switch 
--orig_master_host = masterA 
--orig_master_port = 3306 
--orig_master_log_file = bin.00x
--orig_master_log_pos = 893
--new_master_host = masterB 
--new_master_pos = 3306 
--new_master_log_file = bin.00x 
--new_master_log_pos = 1129
But but but...
• Don’t work when master is dead 
• MHA has hook functions call
specified script *without* binlog
file and position!
Conclusion
• N:1 Replication not support MHA…
• please expect “JIKAISAKU”
Any question?

N:1 Replication meets MHA