GTIDs in MySQL 5.6: New Replication Protocol, New Ways to Break Replication
Join the DZone community and get the full member experience.Join For Free
this post was originally written by stephane combaudon
one of the mysql 5.6 features many people are interested in is global transactions ids ( gtids) . this is for a good reason: reconnecting a slave to a new master has always been a challenge while it is so trivial when gtids are enabled. however, using gtids is not only about replacing good old binlog file/position with unique identifiers, it is also using a new replication protocol. and if you are not aware of it, it can bite.
replication protocols: old vs new
the old protocol is pretty straightforward: the slave connects to a given binary log file at a specific offset, and the master sends all the transactions from there.
the new protocol is slightly different: the slave first sends the range of gtids it has executed, and then the master sends every missing transaction. it also guarantees that a transaction with a given gtid can only be executed once on a specific slave.
in practice, does it change anything? well, it may change a lot of things. imagine the following situation: you want to start replicating from trx 4, but trx 2 is missing on the slave for some reason.
with the old replication protocol, trx 2 will never be executed while with the new replication protocol, it will be executed automatically.
here are 2 common situations where you can see the new replication protocol in action.
it is well known that the good old
set global sql_slave_skip_counter = n
is no longer supported when you want to skip a transaction and gtids are enabled. instead, to skip the transaction with
, you have to
inject an empty transaction
mysql> set gtid_next = 'xxx:n'; mysql> begin; commit; mysql> set gtid_next = 'automatic';
why can’t we use
? because of the new replication protocol!
imagine that we have 3 servers like the picture below:
let’s assume that
is allowed and has been used on s2 to skip trx 2. what happens if you make s2 a slave of s1?
both servers will exchange the range of executed gtids, and s1 will realize that it has to send trx 2 to s2. two options then:
- if trx 2 is still in the binary logs of s1, it will be sent to s2, and the transaction is no longer skipped.
- if trx 2 no longer exists in the binary logs of s1, you will get a replication error.
this is clearly not safe, that’s why
is not allowed with gtids. the only safe option to skip a transaction is to execute a fake transaction instead of the real one.
if you execute a transaction locally on a slave (called errant transaction in the mysql documentation), what will happen if you promote this slave to be the new master?
with the old replication protocol, basically nothing (to be accurate, data will be inconsistent between the new master and its slaves, but that can probably be fixed later).
with the new protocol, the errant transaction will be identified as missing everywhere and will be automatically executed on failover, which has the potential to break replication.
let’s say you have a master (m), and 2 slaves (s1 and s2). here are 2 simple scenarios where reconnecting slaves to the new master will fail (with different replication errors):
# scenario 1
# s1 mysql> create database mydb; # m mysql> create database if not exists mydb; # thanks to 'if not exits', replication doesn't break on s1. now move s2 to s1: # s2 mysql> stop slave; change master to master_host='s1'; start slave; # this creates a conflict with existing data! mysql> show slave status\g [...] last_sql_errno: 1007 last_sql_error: error 'can't create database 'mydb'; database exists' on query. default database: 'mydb'. query: 'create database mydb' [...]
# scenario 2
# s1 mysql> create database mydb; # now, we'll remove this transaction from the binary logs # s1 mysql> flush logs; mysql> purge binary logs to 'mysql-bin.000008'; # m mysql> create database if not exists mydb; # s2 mysql> stop slave; change master to master_host='s1'; start slave; # the missing transaction is no longer available in the master's binary logs! mysql> show slave status\g [...] last_io_errno: 1236 last_io_error: got fatal error 1236 from master when reading data from binary log: 'the slave is connecting using change master to master_auto_position = 1, but the master has purged binary logs containing gtids that the slave requires.' [...]
as you can understand, errant transactions should be avoided with gtid-based replication. if you need to run a local transaction, your best option is to disable binary logging for that specific statement:
mysql> set sql_log_bin = 0; mysql> # run local transaction
gtids are a great step forward in the way we are able to reconnect replicas to other servers. but they also come with new operational challenges. if you plan to use gtids, make sure you correctly understand the new replication protocol, otherwise you may end up breaking replication in new and unexpected ways.
i’ll do more exploration about errant transactions in a future post.
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.