MySQL Replication Tutorial
MySQL Replication Tutorial
"#$%
&'()*+,-*./012-.3*,)
Lars Thalmann
Technical lead
Replication, Backup, and Engine Technology
Mats Kindahl
Lead Developer
Replication Technology
!"#$%0&'()*+,-*./
67"8 9.:8
MySQL
Binary log Server
• Log of everything executed Slave
• Divided into transactional components
• Used for replication and point-in-time recovery
1'3;*/.).<"
#-'(0DE0!,F'0,0G,+F2(0.=0-7'0;,5-'3
Master
Master
Slave
4./=*<23'0-7'05),C'E0(,3-0D
CHANGE MASTER TO
Master
master_host = “dbserv1”,
master_user = “rep-user”,
master_password =
“this-is-the-password”;
Slave
4./=*<23'0-7'05),C'E0(,3-0I
CHANGE MASTER TO
Master
master_host = “dbmaster.me.com”,
master_log_file = “binlog-00001”,
master_log_pos = 0;
Slave
#-,3-0-7'05),C'J
Master
Master
Slave
!,5-'30:*-70#),C'
binary
log
Master
TCP connection
Slave
&'()*+,-*./0*50*/?'('/?'/-0.=0#-.3,<'0K/<*/'5
! You can replicate between any pair of engines
InnoDB to InnoDB
MyISAM to MyISAM
InnoDB to MyISAM
MEMORY to MyISAM
etc...
! The binary log is not the InnoDB transaction log (or the
Falcon log, or ...)
!,5-'30:*-70!,/"0#),C'5
Master
Master/
Master Slave
Slave
log_slave_updates = 1
47,*/00@ #'3C'30I0<.'50?.:/AAA0
Master
X
Master/
Slave
Slave
AAA0#'3C'30L0*505-*))02(M0G2-0.2-0.=05"/+
Master
X
Master/
Slave
Slave
K,+705'3C'307,50,02/*>2'0N5'3C'3O*?P
server_id=1 server_id=3
Master/
Master Slave
Slave
server_id=2
Master/
Slave
server_id=1 Master/
Slave
server_id=3
17'03*/<0-.(.).<"0*50/.-0,03'+.;;'/?'?0
+./=*<23,-*./
Master/
Slave
Master/
Slave
X
Master/
Slave
Q,*30.=0!,5-'35
Master/ Master/
Slave Slave
Master
Master/ Master/
Slave Slave
Slave
Slave
Slave
17'0N&'),"0#),C'P
The master has to
handle only one
Master TCP connection.
Relay
Slave
log_slave_updates
Master
CHANGE MASTER TO
MASTER_HOST=’adventure.com’,
MASTER_USER=’dragon’,
MASTER_PASSWORD=’xyzzy’;
#1R&10#%RXK0,/?0#1BQ0#%RXK
! Used on slave
! Used to start or stop the slave threads
! Defaults to affecting both I/O and SQL thread
! ... but individual threads can be started or stopped
START SLAVE SQL_THREAD
START SLAVE IO_THREAD
&K#K10#%RXK
! Used on slave
! Removes all info on replication position
Deletes master.info, relay-log.info and all relay logs
! Relay logs are unconditionally removed!
... even if they have not been fully applied
#K10H%BTR%0#$%O#%RXKO#^UQO4BSV1K&
! Used on slave
! Global server variable
! Requires SUPER privileges
! Slave SQL thread shall not be running
! Slave will skip events when starting
! Useful when recovering from slave stops
! Might leave master and slave with different data in tables
... so be careful when you use it
S5'04,5'5
S5'04,5'5M0Q,3-0D0@ T,5*+0&'()*+,-*./
Intensive Reads High Availability
Master
Master/ Master/
Slave Slave
Slave
Slave
Slave
N#('+*,)*5-P05),C'50@ G,+F2(50,/?03'(.3-*/<
Master
Slave
Slave
Slave reports
Slave
Slave
backups
N#('+*,)*5-P05),C'50@ ('3_,(()*+,-*./
friends: 10 GB
Master
messages: 30
GB
Slave Slave
Slave
Slave
Master
Slave
Slave
Slave
Slave “message board” queries
(friends table in black
“friends list” queries
hole)
(message table in black
hole)
17*/<50-.0-7*/F0,G.2-0*/0G,5*+03'()*+,-*./
Master/ Master/
Slave Slave
Slave
Slave
Slave
R/"0G'--'38
Master/ Master/
Slave Slave
Proxy
Master
Slave
Slave
Slave
Q3.G)';E05),C'0=,*).C'30-.0,0/':0;,5-'3
! Look at SHOW SLAVE STATUS. This gives the file and
position on the failed master.
! “File 34 position 6000” on the failed master may correspond
to “File 33 position 22000” on the new master. Find the
corresponding file and position.
! CHANGE MASTER TO
master_host = ...
master_log_file = ...
master_log_pos = ...
! START SLAVE
9,/?)*/<0-7'0=,*).C'30(3.G)';
1. Automate it (scripting)
2. Avoid it
S5'04,5'5M0Q,3-0I0@ 9R0,/?0#+,)'0B2-
Master Master
Slave
Shared Disk Array
S5'04,5'5M0Q,3-0I0@ 9R0,/?0#+,)'0B2-
2: MySQL Cluster as master, MySQL slaves
Cluster Cluster
Slave Slave
Slave
S5'04,5'5M0Q,3-0I0@ 9R0,/?0#+,)'0B2-
Virtual IP address
Master Master
3: Master and
Slave Slave proxy master are
Slave both HA pairs
S5'04,5'5M0Q,3-0I0@ 9R0,/?0#+,)'0B2-
4: Replicate from
NDB Cluster Cluster
Cluster through
HA proxy pair
Virtual IP address
Slave
Slave
Slave
Slave
“friends list” slaves
“message board” slaves
R(()*+,-*./_)'C')0(,3-*-*./*/<0,/?0-7'0a'?'3,-'?0
K/<*/'
Friends
Master
Message
Master
“friends list”
Slave
Slave slaves
CREATE TABLE messages (
id int unsigned ...
) ENGINE=FEDERATED
CONNECTION=”mysql://feduser:fedpass@message-master/
friendschema/messages”;
S5'04,5'5M0Q,3-0L0@ !2)-*()'0Z,-,04'/-'35
secure
)*+$,&-"$ tunnel !"#$%&'(
rep
Active
Master
Master
wr wr
wr wr
Slave Slave
rd rd
app app
Slave Slave
wr wr
Slave Slave
rd app app rd
Slave Slave
! Statement-based replication
Replicate statement doing changes
Requires up-to-date slave
Requires determinism
! Row-based replication
Replicate actual row changes
Does not require up-to-date slave
Can handle any statement
4.;(,3*5./0.=03'()*+,-*./0;'-7.?5
! Row-based replication
Can handle ”difficult” statements
Required by cluster
! Statement-based replication
Sometimes smaller binary log
Binary log can be used for auditing
&.:_G,5'?03'()*+,-*./0=',-23'5
! Log is idempotent
... provided all tables in log have primary key
Master Slave
STMT ROW
Parse/optimize/execute Statements
flushed at
MySQL Server commit MySQL Server
SBR !,5-'3 #),C'
&'()*+,-*./ I/O
thread SQL
thread
Rows
SE1 SE2 SE1 SE2
Relay
Binlog Binlog
Storage Binlog
engine
interface Storage Engines Storage Engines
!"#$%0&'()*+,-*./0R3+7*-'+-23'
!"#$%0\ADE0&.:_G,5'?03'()*+,-*./0[&T&]
Application Application Application Application
Parse/optimize
Various optimizations:
•Only primary key in before image. Works if table has PK
•Only changed column values in after image. Works if table has PK
Log is idempotent if PK exists and there are only RBR events in log.
Slave can execute both SBR and RBR events.
4)25-'3
&'()*+,-*./
!"#$%04)25-'30&'()*+,-*./
%.+,)0,/?0H).G,)0&'?2/?,/+"
Application Application Application Application Application Application
Replication
MySQL MySQL MySQL MySQL MySQL MySQL
Server Server Server Server Server Server
ZT ZT ZT ZT
ZT ZT ZT ZT
H).G,)
%.+,)0#"/+73./.250 R5"/+73./.25
&'()*+,-*./0@ &'()*+,-*./
-:._(7,5'0+.;;*-
1..)5
,/?
1'+7/*>2'5
!,F*/<0,05/,(57.-0=3.;0,0;,5-'30?,-,G,5'
I/O
Flush
2:05 to 3:05 logs 3:05 to 4:05
SQL
!,/,<*/<0X*3-2,)0UQ0,??3'55'5
lars@mysql.com, mats@mysql.com
www.mysql.com
4.;;./0KC'/-09',?'30@ Dh0G"-'5
Field Length Description
Timestamp 4 bytes Seconds since 1970
Type 1 byte Event type
Master Id 4 bytes Server Id of server that created this event
# at 235
#060420 20:16:02 server id 1 end_log_pos 351
# Position Timestamp Type Master ID
# 000000eb e2 cf 47 44 02 01 00 00 00
# Size Master Pos Flags
# 74 00 00 00 5f 01 00 00 10 00
#-,-';'/-_G,5'?0UV#K&10IfIE0$2'3"0'C'/-0?,-,
# 000000fe 02 00 00 00 00 00 00 00
# 04 00 00 1a 00 00 00 40 |................|
# 0000010e 00 00 ... |.............std|
# 0000011e 04 08 ... |.......test.INSE|
# 0000012e 52 54 ... |RT.INTO.t1.VALUE|
# 0000013e 53 20 ... |S...A...B......X|
# 0000014e 27 2c ... |...Y......X...X.|
# 0000015e 29 |.|
# Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1145556962;
INSERT INTO t1 VALUES ('A','B'), ('X','Y'), ('X','X');
&.:_G,5'?0UV#K&10DfIE01,G)'0;,(0'C'/-
# at 235
#060420 20:07:01 server id 1 end_log_pos 275
# Position Timestamp Type Master ID
# 000000eb c5 cd 47 44 13 01 00 00 00
# Size Master Pos Flags
# 28 00 00 00 13 01 00 00 00 00
# 000000fe 0f 00 00 00 00 00 00 00
04 74 65 73 74 00 02 74 |.........test..t|
# 0000010e 31 00 02 fe fe |1....|
# Table_map: `test`.`t1` mapped to number 15
BINLOG 'xc1HRBMBAAAAKAAAABMBA...3QAAnQxAAL+/g==';
&.:_G,5'?0UV#K&10IfIE063*-'0'C'/-
BINLOG 'xc1HRBQBAAAALAAAAD...EBQvkBWAFZ+QFYAVg=';
!"#$%04)25-'30&'()*+,-*./
67'3'0-.0<'-0-7'0).<0'C'/-580
Replication
MySQL MySQL MySQL
Server Server Server
Application
ZT ZT
Application ZT ZT
MySQL MySQL
Server Server
TC (DB y) TC (DB x)
DB 1 DB 3
Row-level
locking
on primary
replica DB 2 DB 4
TC (DB x) TC (DB x)
Replication
server
DB 1 DB 3
Row-level
locking
on primary DB 2 DB 4
replica
Node group 1 Node group 2
!"#$%0&'()*+,-*./0R3+7*-'+-23'
!"#$%0\AD
Injector
interface SE1 SE2 SE1 SE2
Relay
Binlog Binlog
NDB Binlog
Injector
Storage Engines
Storage Engines
Row-based log from
cluster data nodes
!"#$%04)25-'30&'()*+,-*./
T'7,C'50)*F'0.3?*/,3"0!"#$%0&'()*+,-*./
Application Application Application Application Application Application
Replication
MySQL MySQL MySQL MySQL MySQL MySQL
Server Server Server Server Server Server
ZT ZT ZT ZT
ZT ZT ZT ZT
H).G,)
%.+,)0#"/+73./.250 R5"/+73./.25
&'()*+,-*./0@ &'()*+,-*./
-:._(7,5'0+.;;*-