MENU
Replication with GTIDs
Transaction-based replication using global transaction identifiers (GTIDs) allows for seamless failover, in which one of the slaves is promoted to a master when the master crashes.A GTID is represented as:
GTID = source_id:transaction_id
which uniquely identifies a transaction across the entire replication setup. source_id, which is usually the server’s server_uuid, refers to the originating server. transaction_id is an integer that increases by one every time a transaction executes within the master. For example, a GTID looks like:
3E11FA47-71CA-11E1-9E33-C80AA9429562:23
In the binary log, each transaction is associated with a GTID. During a replication process when the slave reads a GTID from the master, it retains the same GTID after committing the corresponding transaction. The slave does not generate a new GTID. In other words, a GTID remains the same throughout the replication setup after the transaction was first committed on the master.
The following steps describe how to set up transaction-based replication:
Step 1: Make the master and slave read-only.
SET @@global.read_only = ON; |
Then, allow the slave to catch up with the master.
Step 2: Stop both servers. In the command line:
mysqladmin –uusername –p shutdown |
Step 3: Restart both servers with GTIDs enabled. For the master, enter in the command line:
mysqld_safe --gtid_mode=ON --log-bin --log-slave-updates --enforce-gtid-consistency & |
For the slave, add the option --skip-slave-start:
mysqld_safe --gtid_mode=ON --log-bin --log-slave-updates --enforce-gtid-consistency --skip-slave-start & |
Step 4: Connect the slave to the master.
CHANGE MASTER TO
MASTER_HOST = host,
MASTER_PORT = port,
MASTER_USER = user,
MASTER_PASSWORD = password,
MASTER_AUTO_POSITION =1;
START SLAVE;
SET @@global.read_only = OFF; |
To see the GTIDs in the binary log, enter SHOW MASTER STATUS, SHOW SLAVE STATUS, or SHOW BINLOG EVENTS. You can read the system variable gtid_exceuted. Moreover, the log file can be read using mysqlbinlog --base-64-output=DECODE-ROWS in the command line. You should notice that consecutive GTIDs may be collapsed into the range form:
3E11FA47-71CA-11E1-9E33-C80AA9429562:20-23
3E11FA47-71CA-11E1-9E33-C80AA5698468:40-99
During a failover, the slave sends to the master the range of GTIDs that have been committed. The master then sends all other transactions that have not been committed on the salve, along with the corresponding GTIDs.
When a slave has just been set up, executing the entire transaction history from the master can be time consuming. To speed things up, you can manually copy the data and transactions to the slave:
- To copy the data, you can use
mysql --gtid-mode=ON
(slave) to import a dump file created with
mysqldump --master_data --set-gtid-purged (master), or copy the contents of the master’s data directory to the slave data’s directory. - To copy the transactions, import the binary log using mysqlbinlog, with the options
--read-from-remote-server and
--read-from-remote-master. - You can also copy the master’s binary log files to the slave directly. To read the logs on the slave, update the slave’s binlog.index file to point to the copied logs, then execute a CHANGE MASTER TO statement to point to the first log file, and START SLAVE to read them. To read the logs on the slave, you can also use mysqlbinlog > file to export the binary log files to SQL files that can be processed by the mysql client.
SET GTID_NEXT='aaa-bbb-ccc-ddd:N';
BEGIN;
COMMIT;
SET GTID_NEXT='AUTOMATIC';
FLUSH LOGS;
PURGE BINARY LOGS TO 'master-bin.00000N';
Instead of committing empty transactions and using the PURGE BINARY LOGS statement, you can also set the system variable gtid_purged on the slave directly, based on the value of gtid_executed on the master. gtid_purged contains the set of all transactions that have been purged from the master’s binary log.
Note that the following are not supported by GTID-based replication:
- Non-transactional storage engines such as MyISAM.
- CREATE TABLE … SELECT statements.
- Temporary tables.
- sql_slave_skip_counter. Use the master’s gtid_executed variable instead.
- Importing a dump made using mysqldump into a server running with GTID mode enabled, when there are GTIDs in the target server’s binary log.