skip to content

In MySQL replication, what does a GTID identify, and what does SOURCE_AUTO_POSITION=1 change compared with binlog file and position?

level: middleimportance: must knowfreq 65%

answer

  1. one name per transaction, everywhere
  2. server UUID plus a sequence number
  3. replica sends what it already has
  4. repoint without translating coordinates
  5. both GTID variables default to OFF

basics

~20 s

A GTID names each committed transaction as source_uuid:sequence on every server it reaches. With SOURCE_AUTO_POSITION=1 a replica sends the GTID set it already holds and the source sends the rest, so no binlog file and offset is needed.

solid answer

~50 s

A global transaction identifier is `source_id:transaction_id`, normally the originating server's `server_uuid` plus a sequence number, and it travels with the transaction to every replica, which records it in `gtid_executed`. Under file and position, a replica tracks a byte offset in one server's binlog, and that offset means nothing on any other server, so repointing a replica after a failover means translating coordinates by hand. With `CHANGE REPLICATION SOURCE TO ... SOURCE_AUTO_POSITION = 1`, the replica sends its received and executed GTID set in the handshake and the source streams every transaction not in it, so any replica can follow any server that has the history. Both `gtid_mode` and `enforce_gtid_consistency` default to `OFF` in MySQL 8.4, and `enforce_gtid_consistency` must be `ON` first; and the source refuses to start replication with `ER_SOURCE_HAS_PURGED_REQUIRED_GTIDS` if it already purged binlogs the replica needs.

code

sql · 7 lines
sql
STOP REPLICA;
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST = 'mysql-b.internal',
  SOURCE_PORT = 3306,
  SOURCE_SSL = 1,
  SOURCE_AUTO_POSITION = 1;
START REPLICA USER = 'repl' PASSWORD = 'change-me';

go deeper

for a junior

Recall that a GTID names each transaction with the originating server's UUID and a sequence number, and that the same GTID appears on every replica.

for a middle

Explain the auto-positioning handshake, why file and position coordinates do not survive a failover, and why enforce_gtid_consistency must be ON before gtid_mode.

for a senior

Show you can handle the failures GTIDs expose: purged binlogs blocking a replica, errant transactions found with GTID_SUBTRACT, and skipping a bad transaction with an empty one.

for a principal

Argue for making GTIDs and super_read_only on replicas a fleet standard, including the online migration path through the permissive gtid_mode steps.

## What a GTID is A **global transaction identifier (GTID)** is a name MySQL gives each transaction when it commits on the server where it originated. Its form is: ```plaintext GTID = source_id:transaction_id 3E11FA47-71CA-11E1-9E33-C80AA9429562:23 ``` The `source_id` is normally that server's `server_uuid`, and the `transaction_id` is a sequence number in commit order, starting at 1. MySQL 8.4 also supports **tagged GTIDs**, `source_id:tag:transaction_id`, for marking a group of transactions with a user-defined tag. The GTID is written to the binary log with the transaction and **keeps its name on every replica** that applies it. Each server tracks the transactions it has committed as a GTID set in `gtid_executed` (shown as `Executed_Gtid_Set` in `SHOW REPLICA STATUS` and `SHOW BINARY LOG STATUS`), and a replica also shows `Retrieved_Gtid_Set`, everything it has received into its relay log. ## File and position versus auto-positioning Without GTIDs, a replica remembers **where it is in one particular server's binlog**: a file name and a byte offset such as `binlog.000187` at position `4529`. That coordinate is local. The same transaction sits at a different file and offset on every other server, so after a failover each surviving replica has to be repointed with a coordinate translated by hand, and a mistake means skipped or doubled transactions. With GTIDs and `SOURCE_AUTO_POSITION = 1`: 1. In the connection handshake the replica sends the set of GTIDs it has already received, committed, or both. 2. The source sends every transaction in its binary log whose GTID is **not** in that set. 3. A transaction whose GTID the replica already has is skipped automatically, so no transaction is applied twice, even in a diamond topology with two upstream paths. The practical result is that a replica can be pointed at **any** server holding the history, the new primary after a failover included, by host and port alone. `SOURCE_LOG_FILE` and `SOURCE_LOG_POS` cannot be combined with `SOURCE_AUTO_POSITION = 1`. ## Turning GTIDs on | Variable | 8.4 default | Role | |---|---|---| | `enforce_gtid_consistency` | `OFF` | refuses statements that cannot be logged safely with one GTID | | `gtid_mode` | `OFF` | `OFF`, `OFF_PERMISSIVE`, `ON_PERMISSIVE`, `ON` | `enforce_gtid_consistency` must be `ON` before `gtid_mode` can be `ON`, and `gtid_mode` moves **one step at a time** (`OFF` to `OFF_PERMISSIVE` to `ON_PERMISSIVE` to `ON`), which is how a running topology is converted online. The manual's online procedure, run on every server and finished everywhere before the next step: 1. Set `enforce_gtid_consistency = WARN` and watch the error log under normal load until the application produces no warnings. 2. Set `enforce_gtid_consistency = ON`. 3. Set `gtid_mode = OFF_PERMISSIVE`, then `ON_PERMISSIVE`. 4. Wait until the `Ongoing_anonymous_transaction_count` status variable reads 0 and every anonymous transaction has replicated. 5. Once binlogs without GTIDs are no longer needed for backups, set `gtid_mode = ON` and persist both settings in the option file. With consistency enforced, statements that would break the one-transaction-one-GTID rule fail with an error, chiefly: - updating a nontransactional table such as MyISAM in the same statement or transaction as an InnoDB table; - `CREATE TEMPORARY TABLE` or `DROP TEMPORARY TABLE` inside a transaction when `binlog_format` is `STATEMENT`. ## Failure modes GTIDs make visible - **Purged history.** If the transactions a replica lacks have already been purged from the source's binlog, the source returns `ER_SOURCE_HAS_PURGED_REQUIRED_GTIDS` and replication does not start; the replica must be reprovisioned, for example from a clone or backup. - **A replica ahead of its source.** If the replica holds transactions with the source's UUID that the source never committed, the source returns `ER_REPLICA_HAS_MORE_GTIDS_THAN_SOURCE`. - **Errant transactions.** A write made directly on a replica gets the replica's own UUID. It sits in that replica's `gtid_executed` but nowhere else, and if the replica is later promoted, other servers fetch it from the new source. `GTID_SUBTRACT(replica_set, source_set)` finds such writes, and `super_read_only` on replicas prevents them. - **Skipping a broken transaction.** `sql_replica_skip_counter` does not work with GTIDs. Instead you commit an **empty transaction** under the failing GTID, after which the replica skips the real one: ```sql SET GTID_NEXT = '3E11FA47-71CA-11E1-9E33-C80AA9429562:23'; BEGIN; COMMIT; SET GTID_NEXT = 'AUTOMATIC'; ``` ## Version notes MySQL 8.4 removed the old `CHANGE MASTER TO` statement and its `MASTER_AUTO_POSITION` option, so the current spelling is `CHANGE REPLICATION SOURCE TO ... SOURCE_AUTO_POSITION = 1`, followed by `START REPLICA`. GTIDs are still off by default, so a new topology has to opt in deliberately.

  • A MySQL replica with SOURCE_AUTO_POSITION=1 will not start and reports ER_SOURCE_HAS_PURGED_REQUIRED_GTIDS. What happened and what do you do?
    The replica is missing transactions that the source has already purged from its binary log, so auto-positioning cannot stream them. The missing GTIDs are listed in the source's error log. You cannot recover them from that source, so you reprovision the replica from a fresh clone or backup, and you size `binlog_expire_logs_seconds` so that binlogs outlive the longest time a replica might be stopped.
  • How do you find and prevent errant transactions on MySQL GTID replicas?
    Compare GTID sets with `GTID_SUBTRACT()`: pass the replica's `gtid_executed` value as the first argument and the source's as the second, and whatever is left was committed only on the replica, under its own UUID. Prevent them by running replicas with `super_read_only=ON`, so even privileged accounts cannot write locally.

File and position is like telling a courier to collect from the third box on the second shelf of one warehouse: the address is useless at any other warehouse. A GTID is the parcel's tracking number, so any depot can check which parcels you already hold and send only the missing ones.

  • A GTID is just the binlog file name and offset with a new label.
  • GTIDs are enabled by default in MySQL 8.4, so no configuration is needed.
  • With auto-positioning you still pass SOURCE_LOG_POS so the replica knows where to start.
  • Skipping a failed GTID transaction works with sql_replica_skip_counter like before.
  • A write made directly on a replica is harmless because GTIDs ignore local transactions.
  • Once GTIDs are on, the source can purge binlogs freely because replicas track sets.

Assumes MySQL 8.4 LTS: gtid_mode and enforce_gtid_consistency still default to OFF, CHANGE MASTER TO was removed in 8.4, and 8.4 adds tagged GTIDs.