mysql user camp: gtids
DESCRIPTION
The slde contains an introduction to the global transaction identifiers(GTIDs) in MySQL Replication. The new protocol at re-connect, skipping transactions with GTIDS, replication filters, purging logs, backup/restore ets are covered here.TRANSCRIPT
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
1
MySQL Replication:Global Transaction Identifiers(GTIDs)
Shivji Kumar JhaSoftware Developer, MySQL Replication Team
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
2
Safe Harbour Statement
The following is intended to outline our general product direction. It is intended
for information purposes only, and may not be incorporated into any contract.
It is not a commitment to deliver any material, code, or functionality, and should
not be relied upon in making purchasing decisions. The development, release,
and timing of any features or functionality described for Oracle’s products
remains at the sole discretion of Oracle.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
3
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
4
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
What isReplication?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
5
Replication: Copy Changes Master → Slave
MySQL Master Server– Changes data
– Sends changes to slave
MySQL Slave Server– Receives changes from master
– Applies received changes to database
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
M S
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
6
Replication: Copy Changes Master → SlaveIntroduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
M M/S S
S
S
S
M
Server can be master, slave or both
Master can have multiple slaves
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
7
Replication: Copy Changes Master → SlaveIntroduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
SM
M
● Slave can only have one master
SM
MSlave can have multiple masters!labs.mysql.com
labsYippee!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
8
Replication: Copy Changes Master → SlaveIntroduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
M/S
● Circular replication is also possible
M/S
M/S
M/S
M/S
M/S
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
9
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Why UseReplication?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
10
Why Replication? – Performance
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Read scale-out
M S
write clients read clients
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
11
Why Replication? – Performance
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Read scale-out
M S
write clients read clients
Morereads?More
slaves!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
12
Why Replication? – Performance
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Read scale-out
M S
write clients read clients
Morereads?More
slaves!
S
S
S
M
read clients
write clients
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
13
Why Replication? – Redundancy
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
If master crashes, promote slave to master
C
B
A
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
14
Why Replication? – Redundancy
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
If master crashes, promote slave to master
C
B
ACrash
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
15
Why Replication? – Redundancy
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
If master crashes, promote slave to master
C
B
A
B is thenew master
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
16
Why Replication? – Online backup/reporting
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Expensive queries on slave(s)
M S
Regular clients
Reports Big queries Business intelligence
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
17
CB
BAAC
Image fromwww.ginkgomaps.com
Why Replication? – Long-distance Data Distribution
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
18
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
How DoesReplication
Work?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
19
All Changes Written to Binary Log
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
20
All Changes Written to Binary Log
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
A
binary log
Clientcreate table t (a int);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
21
All Changes Written to Binary Log
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...
A
binary log
Client
Table t
create table t (a int);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
22
All Changes Written to Binary Log
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...
A
binary log
Client
Table t
create table t (a int);insert into t values (1);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
23
All Changes Written to Binary Log
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...insert...A
binary log
Client
Table t1
create table t (a int);insert into t values (1);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
24
Slave Initiates Replication
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
B
binary log
A
binary log
Client1. Slave sends request
to start replicationto master
2. Master sends streamof replication data
to slave
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
25
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
B
binary log
A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
26
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...
B
binary log
A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
27
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...
Table t
B
binary log
create...
A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
28
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...
Table t Table t
create...
B
binary log
create...
A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
29
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...insert...
Table t Table t
create...
B
binary log
create...
A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
30
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...insert...
Table t1
Table t
create...
B
binary log
create...insert...A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
31
Binary Log Sent to Slave, Re-executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
create...insert...
Table t1
Table t1
create...insert...B
binary log
create...insert...A
binary log
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
32
Makes sense?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
33
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
34
Fail-over Crash can happen to anyone:
– Hardware failure
– Human mistake
– Bug
– Natural disaster
Automatic fail-over with Global Transaction Identifiers:– Reduces admin overhead
– Prevents planned downtime
– Prevents unplanned downtime
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
36
Fail-over
Handle server crash with minimal disruption Example 1: tree
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
C
B
A
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
37
Fail-over
Handle server crash with minimal disruption Example 1: tree
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
C
B
ACrash
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
38
Fail-over
Handle server crash with minimal disruption Example 1: tree
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
C
B
A
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
39
Fail-over
Handle server crash with minimal disruption Example 1: tree
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
C
B
A
Make B the new master
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
43
Fail-over
Handle server crash with minimal disruption Example 1: tree Example 2: line
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
44
Fail-over
Handle server crash with minimal disruption Example 1: tree Example 2: line
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
CA B
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
45
Fail-over
Handle server crash with minimal disruption Example 1: tree Example 2: line
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
CA BCrash
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
46
Fail-over
Handle server crash with minimal disruption Example 1: tree Example 2: line
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
CA B
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
47
Fail-over
Handle server crash with minimal disruption Example 1: tree Example 2: line
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
CA B
Make A direct master of C
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
53
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle
C
A
B
D
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
54
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle
C
A
B
DCrash
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
55
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle
C
A
B
D
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
56
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle
C
A
B
D
Make a shortcutin the circle
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
61
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle Example 4: scheduled maintenance
C
A
B
D
Takeout D!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
62
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle Example 4: scheduled maintenance
C
A
B
D
MaintainD offline,without
disruptingservice
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
63
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle Example 4: scheduled maintenance
C
A
B
D
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
64
Fail-over
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Handle server crash with minimal disruption Example 1: tree Example 2: line Example 3: circle Example 4: scheduled maintenance Example 5: arbitrary topology
A
B
C
D
E
F
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
65
Wow!Lets
get started withfail-over.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
66
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
67
Global Transaction Identifiers
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Server assigns Global Transaction Identifier (GTID) to every transaction:
– server_uuid:numbera61678ba-4889-4279-9e58-45ba840af334:1
– server_uuid identifies the server; globally unique
– number is incremented by 1 for each transaction on this server
Writes GTID to binary log Slave ensures transaction gets the same GTID when re-executed
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
68
Global Transaction Identifiers
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
master> CREATE TABLE t1 (a INT);
master> SELECT @@global.gtid_executed;a61678ba488942799e5845ba840af334:1
master> INSERT INTO t1 VALUES (1);
master> INSERT INTO t1 VALUES (2);
master> SELECT @@global.gtid_executed;a61678ba488942799e5845ba840af334:13
Note: interval
New variable:gtid_executed
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
69
Global Transaction Identifiers
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
master> SELECT @@global.gtid_executed;a61678ba488942799e5845ba840af334:110000
slave> SELECT @@global.gtid_executed;a61678ba488942799e5845ba840af334:19999
Slave is missingone transactionSlave is missingone transaction
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
70
But how are GTIDs
used infail-over?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
71
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
72
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Master–slave protocol:– Slave sends @@gtid_executed to master
– Master sends all other transactions to slave
id1,trx1,id2,trx2
id1,trx1,id2,trx2,id3,trx3
A 2. id3, trx3, …
1. id1…id2
B
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
73
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree
(master)
Aid1,trx1,id2,trx2,id3,trx3
(slave)
Cid1,trx1
(slave)
id1,trx1,id2,trx2 B
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
74
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree
(master)
Aid1,trx1,id2,trx2,id3,trx3
(slave)
Cid1,trx1
(slave)
id1,trx1,id2,trx2 B
Crash!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
75
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree
(master)
Aid1,trx1,id2,trx2,id3,trx3
(slave)
Cid1,trx1
(slave)
id1,trx1,id2,trx2 B
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
76
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree
(master)
Aid1,trx1,id2,trx2,id3,trx3
(slave)
Cid1,trx1
(slave)
id1,trx1,id2,trx2 B id1
id2, trx2,...
What theslave has
What theslave doesnot have
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
80
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
BA C
client client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
81
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1
BA C
client client trx1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
82
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1
Bid2,trx2
A C
client clienttrx2 trx1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
83
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1,id2,trx2 B
id2,trx2
A C
client clienttrx2 trx1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
84
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1,id2,trx2,id3,trx3
Bid2,trx2
A C
client clienttrx2 trx1, trx3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
85
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1,id2,trx2,id3,trx3
Bid2,trx2
A C
client clienttrx2 trx1, trx3
Crash!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
86
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1,id2,trx2,id3,trx3
Bid2,trx2
A C
client clienttrx2 trx1, trx3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
87
Automatic Positioning
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Example 1: tree Example 2: circle
id1,trx1,id2,trx2,id3,trx3
Bid2,trx2
A C
client clienttrx2 trx1, trx3
id2
id1,trx1,id3,trx3,...What theslave has
What theslave doesnot have
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
91
Awesome!How do I set
it up?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
92
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
93
Hands-On
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Prerequisites:– Use transactional storage engine for all tables (InnoDB)
– Don't use CREATE TABLE … SELECT
– Don't execute CREATE TEMPORARY TABLE or
– DROP TEMPORARY TABLE inside a transaction
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
94
Hands-On
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Prepare the server for fail-over– Sync and stop all servers
– Add to every my.cnf:gtidmode=onenforcegtidconsistency=onlogbinlogslaveupdates
– Start all servers
– Execute:> CHANGE MASTER TO MASTER_AUTO_POSITION = 1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
95
Hands-On
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Perform fail-over
On the slave, simply point to the new master:> CHANGE MASTER TO MASTER_HOST = '<host>', MASTER_PORT = <port number>, MASTER_USER = '<user name>' MASTER_PASSWORD = 'secret';
No positions needed!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
96
Hands-On
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Perform fail-over
C
B
ACHANGE MASTER TOMASTER_HOST = 'B',MASTER_PORT = <B's port>,MASTER_USER = '<user name>'MASTER_PASSWORD = 'secret';
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
97
Interesting!I want to learn
more.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
98
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
99
Under the Hood: How Slave Preserves GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Bid1,trx1,id2,trx2 A
Slave= client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
100
Under the Hood: How Slave Preserves GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Bid1,trx1,id2,trx2 A
Slave= client
SET GTID_NEXT = 'id1';trx1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
101
Under the Hood: How Slave Preserves GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1
Bid1,trx1,id2,trx2 A
Slave= client
SET GTID_NEXT = 'id1';trx1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
102
Under the Hood: How Slave Preserves GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1
Bid1,trx1,id2,trx2 A
Slave= client
SET GTID_NEXT = 'id1';trx1SET GTID_NEXT = 'id2';trx2
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
103
Under the Hood: How Slave Preserves GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1,id2,trx2 B
id1,trx1,id2,trx2 A
Slave= client
SET GTID_NEXT = 'id1';trx1SET GTID_NEXT = 'id2';trx2
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
104
Under the Hood: GTID_NEXT
GTID_NEXT – session system variable
Default: “AUTOMATIC”
– → Server generates new GTID for next transaction
Slave thread sets to “UUID:NUMBER”– → Server uses specified GTID for next transaction
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
105
Under the Hood: GTID_NEXT
Clients can set GTID_NEXT too:
Mysqlbinlog outputs SET GTID_NEXT statements:
id1,trx1id2,trx2
set gtid_next = “id1”;trx1set gtid_next = “id2”;trx2
mysqlbinlog
binary log
id,insert
A
Client
set gtid_next=”id”;insert
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
106
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
107
Under the Hood: GTIDs Must Be Unique
Impossible to execute a GTID twice:
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1id1,trx2A
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
108
Under the Hood: GTIDs Must Be Unique
Impossible to execute a GTID twice:
Try to execute a GTID second time → transaction skipped:
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1id1,trx2A
id1,trx1
A
Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
109
Under the Hood: GTIDs Must Be Unique
Impossible to execute a GTID twice:
Try to execute a GTID second time → transaction skipped:
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1id1,trx2A
id1,trx1
A
Client
set gtid_next=”id1”;trx2
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
110
Under the Hood: GTIDs Must Be Unique
Impossible to execute a GTID twice:
Try to execute a GTID second time → transaction skipped:
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,trx1id1,trx2A
id1,trx1
A
Client
set gtid_next=”id1”;trx2
trx2 not executed!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
111
I loveGTIDs!
Is there anythingthat I should pay
attention to?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
112
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
113
Skipping a Transaction
Common use case: skip a bad transaction Transaction should be skipped forever Not enough to move replication position
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1
B
Badtransaction
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
114
Skipping a Transaction
Common use case: skip a bad transaction Transaction should be skipped forever Not enough to move replication position
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1,id3,trx3 B
Badtransaction
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
115
Skipping a Transaction
Common use case: skip a bad transaction Transaction should be skipped forever Not enough to move replication position
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1,id3,trx3 B
Badtransaction trx2 comes back
after reconnect
id2, trx2, …
id1,id3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
116
Skipping a Transaction
Common use case: skip a bad transaction Transaction should be skipped forever Not enough to move replication position
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1,id3,trx3 B
Badtransaction trx2 comes back
after reconnect
So we don'tallow seeking!
id2, trx2, …
id1,id3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
117
Skipping a Transaction
Recall: Try to execute a GTID second time → transaction skipped Interesting feature:
Client executes GTID before slave→ slave skips transaction
To force slave thread to skip a transaction:
slave> SET GTID_NEXT=“GTID of transaction to skip”;slave> COMMIT;
Transaction is skipped forever
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
118
Skipping a Transaction
How to skip atransaction forever
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1
B
Badtransaction
set gtid_next=”id2”;commit;Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
119
Skipping a Transaction
How to skip atransaction forever
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1,id2, – B
Badtransaction
set gtid_next=”id2”;commit;Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
120
Skipping a Transaction
How to skip atransaction forever
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1,id2, – ,id3,trx3
B
Badtransaction
set gtid_next=”id2”;commit;Client
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
121
Skipping a Transaction
How to skip atransaction forever
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid1,trx1,id2,trx2,id3,trx3
id1,trx1,id2, – ,id3,trx3
B
Badtransaction
set gtid_next=”id2”;commit;Client
id4, trx4, …
id1,id2,id3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
122
Filters
--replicate-ignore-table etc Transactions should be filtered-out forever Slave commits empty transaction
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
A
(crashed)(master)
Aid1,trx1,id2,trx2,id3,trx3
(slave)
id1,trx1,id2,-
id3,trx3B
Filters outtrx2
Logs id2 with anempty transaction
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
123
Skipping Stuff: Summary & Recipe
To seek forward from position A to B:– Commit empty transaction for each GTID between A and B
To seek backward:– Not possible, does not make sense
To skip N transactions:– Get the GTIDs, commit empty transaction for each GTID
Replication filters automatically commit empty transactions
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
124
So the ruleis easy:
skip = emptytransaction
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
125
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
126
Purging Binary Logs
Binary logs are rotated and can be purged
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid4,trx4,id5,trx5,id6,trx6
master-bin.02 master-bin.03
id7,trx7,id8,trx8
master-bin.01
id1,trx1,id2,trx2,id3,trx3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
127
Purging Binary Logs
Binary logs are rotated and can be purged
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid4,trx4,id5,trx5,id6,trx6
master-bin.02 master-bin.03
id7,trx7,id8,trx8
Client PURGE BINARY LOGS TO 'masterbin.02'
master-bin.01
id1,trx1,id2,trx2,id3,trx3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
128
Purging Binary Logs
Binary logs are rotated and can be purged
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid4,trx4,id5,trx5,id6,trx6
master-bin.02 master-bin.03
id7,trx7,id8,trx8 XPurged!
Client PURGE BINARY LOGS TO 'masterbin.02'
master-bin.01
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
129
Purging Binary Logs
On Master: cannot send purged GTIDs to slave
– @@gtid_purged = all purged GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
130
Purging Binary Logs
On Master: cannot send purged GTIDs to slave
– @@gtid_purged = all purged GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
B Purged!Aid4,trx4,id5,trx5,id6,trx6
master-bin.02
XPurged!id1,trx1,id2,trx2
slave-bin.01
gtid_purged=id1,id2,id3 gtid_executed=id1,id2
master-bin.01
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
131
Purging Binary Logs
On Master: cannot send purged GTIDs to slave
– @@gtid_purged = all purged GTIDs
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
B Purged!Aid4,trx4,id5,trx5,id6,trx6
master-bin.02
XPurged!id1,trx1,id2,trx2
slave-bin.01
Error!
id1,id2
Nothing sensible to do:B needs trx3
which is purgedgtid_purged=id1,id2,id3 gtid_executed=id1,id2
master-bin.01
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
132
Purging Binary Logs
On Slave: must send all GTIDs (purged or not) to master
– @@gtid_executed = all GTIDs (purged or not)
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
A
slave-bin.02
XPurged!A
master-bin.02
id1,trx1,id2,trx2,id3,trx3
id4,trx4,id5,trx5
id4,trx4
slave-bin.01master-bin.01
id5,trx5
id1…id4
Note: slave must send id1...id3 despite purgedor else master would send trx1…trx3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
133
Purging Binary Logs
On Master: cannot send purged GTIDs to slave
– @@gtid_purged = all purged GTIDs
On Slave: must send all GTIDs (purged or not) to master
– @@gtid_executed = all GTIDs (purged or not)
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Aid4,trx4,id5,trx5,id6,trx6
master-bin.02 master-bin.03
id7,trx7,id8,trx8 XPurged!
@@gtid_purged = id1 … id3 @@gtid_executed = id1 … id8
master-bin.01
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
134
Purged and Executed are Stored in Binary Log
Binary logs begin with:
[ list of all GTIDs in all previous binary logs ]
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
A
[id1…id3]id4,trx4,id5,trx5,id6,trx6
master-bin.02 master-bin.03
[id1…id6]id7,trx7,id8,trx8 XPurged!
master-bin.01
GTIDs inmaster-bin.01 +master-bin.02
GTIDs inmaster-bin.01
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
135
Purged and Executed are Stored in Binary Log
Binary logs begin with:
[ list of all GTIDs in all previous binary logs ]
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
A
[id1…id3]id4,trx4,id5,trx5,id6,trx6
master-bin.02 master-bin.03
[id1…id6]id7,trx7,id8,trx8 XPurged!
@@gtid_executed=
head of newest log +GTIDs in newest logid1…id3
id1…id6 + id7 + id8
master-bin.01
@@gtid_purged=
head of oldest log
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
136
Purging Binary Logs: Summary
Works automatically, behind the scenes
Purged GTIDs are stored in @@gtid_purged @@gtid_purged is a subset of @@gtid_executed
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
137
Automaticis the word!Does purging haveanything to do with
backups?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
138
Program Agenda
Introduction to MySQL Replication
Fail-over Base– Global Transaction Identifiers – Automatic Positioning – Hands-On
Under the Hood: Become a GTID Expert– How Slave Preserves GTIDs – GTIDs Must Be Unique
Integration With Other Features– Seeking & Skipping – Purging Binary Logs – Restoring from Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
139
Provision a Slave From a Backup
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,create,id2,insert A
Table t1
B
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
140
Provision a Slave From a Backup
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,create,id2,insert A
Table t1
B
Backup
Table t1
Backup
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
141
Provision a Slave From a Backup
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,create,id2,insert A
Table t1
B
Table t1 Backup
Table t1
Backup Restore
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
142
Provision a Slave From a Backup
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,create,id2,insert A
Table t1
B
Table t1 Backup
Table t1
Backup Restore
Binary lognot
restored!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
143
Provision a Slave From a Backup
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,create,id2,insert A
Table t1
B
Table t1 Backup
Table t1
Backup Restore
Binary lognot
restored!
Set gtid_executed andgtid_purged to id1,id2
(or else A would sendid1,id2 when B connects)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
144
Provision a Slave From a Backup
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
id1,create,id2,insert A
Table t1
B
Table t1 Backup
Table t1
Backup Restore
Binary lognot
restored!
Set gtid_executed andgtid_purged to id1,id2
(or else A would sendid1,id2 when B connects)
XPurged!
set @@gtid_purged = “id1,id2”
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
145
Provision a Slave From a Backup @@gtid_executed must agree with database state
– @@gtid_executed after restore = @@gtid_executed at backup
@@gtid_purged = GTIDs in @@gtid_executed that are not in log– Binary logs not (normally) restored
– @@gtid_purged after restore = @@gtid_executed after restore
@@gtid_purged is settable:
– SET @@GLOBAL.GTID_PURGED = "GTID_EXECUTED at backup"– Also sets @@GTID_EXECUTED to the same value
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
146
Restore a Backup
Backup tools must store @@gtid_executed Restore tools must execute:
– SET @@GLOBAL.GTID_PURGED = "GTID_EXECUTED at backup"
Introduction to MySQL Replication Fail-over Base Under the Hood Integration With Other Features
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
147
You hadme at “GTID”
What shall Ido now?
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
148
Next Steps
More on GTIDs– svenmysql.blogspot.co.uk
More on MySQL 5.6 Replication– http://dev.mysql.com/tech-resources/articles/
mysql-5.6-replication.html
Try Out MySQL 5.6– http://dev.mysql.com/downloads/mysql/