Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MySQL Replication: Errant Transactions in GTID-Based Replication

DZone's Guide to

MySQL Replication: Errant Transactions in GTID-Based Replication

GTID-based replication allows for globally unique replica sets, though you need to keep an eye on errant transactions, which can corrupt data and cause errors.

Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

GTID or Global Transaction Identifier was introduced in MySQL 5.6.5. A GTID is a globally unique id given to all transactions executed on a GTID-enabled MySQL server. GTIDs are a combination of the UUID of the server where a particular transaction has been committed, and the sequence number of that transaction on that particular server. This makes the GTIDs globally unique.

MySQL Replication

GTID-based replication is much more flexible compared to the older binlog-based replication. In a GTID-based setup, the slave does not need a master binlog file and position to start replication. Read more about GTID-based replication. In this article, we will discuss some common MySQL replication issues caused when deploying a GTID-based replica set.

Errant transactions are transactions that are applied to one or more slaves that do not need to be replicated on other nodes. These could be intermittent fixes applied on the slave or accidental writes to the slave by an application.

The problem with these errant transactions arises when the slave that contains an errant transaction is promoted to master. In the case of GTID-based replication, this would cause an issue. The new master now realizes that slaves have not executed the errant transaction. One of two things can happen:

  1. The errant transaction is still present in the master’s binlog and it will send it to the slaves, this can corrupt the data or cause an error.

  2. The transaction is not present in the binlog, and hence cannot be sent over to the slave, which causes a replication error.

Prevention

Errant transactions can actively be prevented following these steps.If you have to apply a fix to a slave, one way to mitigate errant transactions is by temporarily turning off binary logging on the slave. Executing sql_bin_log = 0 before executing the errant query should do the trick. You can later enable binlog by running sql_bin_log =1.To prevent any application writes to slaves, Read-Only should be enabled on a server when it's configured as a slave.

Detection

Detecting an errant transaction in a GTID-based MySQL replica set is easy. MySQL stores all executed GTID’s in its Performance Schema/Information Schema table based on what version MySQL you are using. Taking the current slave’s executed GTID’s and subtracting them from the GTID’s executed on the current master should give you all the errant transactions on that particular slave. Utilities such as mysqlfailover or mysqlrpladmin can also help in detecting errant transactions.

Solution

Once an errant transaction has been detected, there are two ways you can fix the replication errors caused after a failover. One way is to delete the GTID of the errant transaction from the slave GTID executed history. This way, when the slave gets promoted to the master, the errant transaction would not be replicated to all nodes. Another way of handling errant transaction is to tell all the other slaves to skip the errant transaction. That would include inserting an empty transaction with the same GTID as the errant transaction to all the other nodes in the replica set.This shall make all the other nodes think that they have already applied this transaction and hence shall skip it. MySQL has a utility called Mysqlslavetrx dedicated for this. This utility can be used to insert empty transactions with the given GTID. Adding empty transactions can have other uses as well, as discussed here.

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:
database transactions ,gtid ,mysql ,database ,replication ,tutorial

Published at DZone with permission of Neeraj Chinthireddy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}