Replication in MySQL 5.6: GTIDs benefits and limitations – Part 2
Join the DZone community and get the full member experience.Join For Free
the main benefit of using gtids is to have much easier failover than with file-based replication. we will see how to change the replication topology when using gtid-based replication. that will show where gtids shine and where improvements are expected.
this is the second post of a series of articles focused on mysql 5.6 gtids. you can find part one here .
our goal will be to go from setup #1 to setup #2 on the picture below, following various scenarios:
for these tests, all servers are running on 127.0.0.1 with ports ranging from 10000 for s0 to 10004 for s4.
scenario #1: all slaves have processed all the writes
this is the easiest case, we will make s2 a master and redirect replication on the other servers to s2. this scenario can happen when you want to perform a planned failover.
with gtids, all the operations are straightforward:
#for s2 (the new master), we remove its configuration as a slave s1> stop slave; s1> reset slave all; # for s0 s0> change master to master_host='127.0.0.1',master_user='rsandbox',master_password='rsandbox',master_port=10001,master_auto_position=1; s0> start slave; # for s1, s3 and s4 mysql> stop slave; mysql> change master to master_port=10002; mysql> start slave;
those of you who have already done these operations with file-based replication know that it is usually very tedious and that proper recording of binlog file/binlog position needs to be done with care if you don’t want to break replication or corrupt your data.
scenario #2: one of the slaves is behind
now let’s imagine that s0 has crashed, and that s1 has not received all writes (and therefore s3 and s4 are also lagging behind).
s2> select count(*) from t; +----------+ | count(*) | +----------+ | 2 | +----------+ # s1 is behind s1> select count(*) from t; +----------+ | count(*) | +----------+ | 0 | +----------+
can we still use
master_auto_position = 1
? let’s hope
so, as it is one of the ideas of gtids: having for each event across the
cluster a monotonically incremental identifier for each event.
notice that this is the same problem for s0 (which will be late when it comes back) and s1, s3 and s4.
let’s give it a try!
# for s0,s1, s3, s4 mysql> stop slave; mysql> change master to master_port=10002; mysql> start slave; # and then check the number of records from the t table s1> select count(*) from t; +----------+ | count(*) | +----------+ | 2 | +----------+
great! so again, using gtids avoids the tedious work of looking for the binlog position of a specific event. the only part were we should pay attention is the server we choose for promotion: if it is not up-to-date, data may be lost or replication may be broken.
scenario #3: the master has crashed before sending all writes
if the binary logs of the master are no longer readable, you will probably lose the events that have not been sent to the slaves (your last chance is to be able to recover data from the crashed master, but that’s another story). in this case, you will have to promote the most up-to-date slave and reconfigure the other slaves as we did above.
so we will suppose that we can read the binary logs of the crashed master. the first thing to do after choosing which slave will be the new master is to recover the missing events with mysqlbinlog.
let’s say that we want to promote s1 as the new master. we need to know the coordinates of the last event executed:
s1> show slave status\g [...] executed_gtid_set: 219be3a9-c3ae-11e2-b985-0800272864ba:1, 3d3871d1-c3ae-11e2-b986-0800272864ba:1-4
we can see that it’s not obvious to know which was the last executed event: is it
? a ‘last_executed_gtid’ column would have been useful.
in our case we can check that
is the server uuid of s2, and that the other one is from s0 (for s0
which is crashed, the server uuid can be read in the auto.cnf file in
so the last executed event is
. how can i instruct mysqlbinlog to start reading from there? unfortunately, there is no
option or equivalent. see bug
it mean that we cannot easily recover the data with mysqlbinlog? there
is a solution of course, but very poor in my opinion: look for the
binlog file/position of the last executed event and use mysqlbinlog with
the good old
option! even with gtids, you cannot totally forget old-style replication positioning.
reconfiguring replication when using gtids is usually straightforward: just connect the slave to the correct master with
master_auto_position = 1
. this can even be made easier with mysqlfailover from the mysql utilities (this will be the topic of a future post).
unfortunately, this will not work for every use case, and until this is fixed, it is good to be aware of the current limitations.
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
What Is TTS and How Is It Implemented in Apps?
What Is JHipster?
Testing, Monitoring, and Data Observability: What’s the Difference?
Building a Java Payment App With Marqeta