Global Transaction Identifier (GTID) is a unique identifier assigned to each transaction executed on the master in MySQL. GTID is important because it simplifies replication and makes it easier to manage failover.
First, let’s discuss the format of the GTID. It consists of three parts:
source_id:transaction_id
- ‘source_id‘ is a unique identifier for each server in a replication topology.
- ‘transaction_id‘ is an incremental number assigned to each transaction on the source server.
Now let’s discuss how GTID is used in MySQL replication. In a replication topology, each slave tracks the GTID of the last transaction processed. Whenever a slave connects to a master, it sends its GTID position to the master, and the master starts sending transactions starting from that point. This eliminates the need for a slave to keep track of the binary log file and position.
GTID has several benefits over traditional binary log and position-based replication:
1. Simplified failover: In case of failover, the new master can easily continue replication from the last known GTID position, without having to worry about which binary log file and position to start from.
2. Easier to manage multi-source replication: With GTID, it’s easy to track transactions from multiple sources and avoid conflicts.
3. Better detection of conflicts: With traditional replication, conflicts can occur if a transaction is executed on multiple servers with different binary log coordinates. GTID eliminates this problem by providing a unique identifier for each transaction.
To use GTID in MySQL replication, you need to enable it on both the master and the slave servers. To enable GTID on the master, add the following lines to your my.cnf file:
server-id=1
log-bin
enforce-gtid-consistency
The ‘enforce-gtid-consistency‘ option ensures that all transactions are assigned a GTID.
To enable GTID on the slave, add the following line to your my.cnf file:
server-id=2
enforce-gtid-consistency
Then, start replication as usual, using the ‘CHANGE MASTER TO‘ command. You’ll need to specify the master server’s GTID position:
CHANGE MASTER TO MASTER_HOST='master', MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='secret', MASTER_AUTO_POSITION=1;
The ‘MASTER_AUTO_POSITION‘ option tells the slave to use the master’s GTID position.
In conclusion, GTID simplifies replication and makes it easier to manage failover and multi-source replication. It’s a powerful tool that is becoming increasingly popular in MySQL replication.