Wednesday 4 March 2015

MySQL: Enabling GTIDs

With MySQL 5.6, Oracle has introduced  a new concept of GTIDs. 

So, what's GTID? And, how's it useful for me??

GTID stands for Global Transaction ID. That means, each committed transaction will be logged in with an Unique Identification ID, called, GTID.

GTID = source_id : transaction_id

Where in,
source_id --> Originating server's UUID
transaction_id --> Sequence number in which the transactions are being committed.


The main advantage of GTID is, you don't have to track Master's binlog file and position to resume/setup the replication process. The sync will happen automatically, if both master and slaves are GTID enabled.

And, there's no disadvantage in enabling the GTID, except, you can't follow the traditional way to skip certain transactions and restore ad-hoc data from master. You need to follow different methodology.

Anyhow, here's how to enable GTID:

* Make both Master and Slave as Read-only:

SET @@global.read_only=ON

* Restart/Start both Master and Slave with the below options:
--gtid-mode=ON
--log-bin
--log-slave-updates
--enforce-gtid-consistency 
--skip-slave-start   [Of course, Only for Slave]

Now, to start the replication, execute the below on slave:

CHANGE MASTER TO
MASTER_HOST=*****,
MASTER_PASSWORD=****,
MASTER_PORT=****,
MASTER_AUTO_POSITION=1;

Note that, Binlog info is not required.
Slave reads all the transactions, based on the GTID and skips if it's already been processed.