Re-Slaving a Crashed MySQL Master Server in Semisynchronous Replication Setup
Tags: mysql, replication, server, master
Published: 2019-12-09 21:42:06

---
title: Re-Slaving a Crashed MySQL Master Server in Semisynchronous Replication Setup
published: true
description: ScaleGrid blog - How to detect and fix extra transactions on a crashed MySQL master server in semisynchronous replication, and re-slave it back into your master-slave setup.
tags: MySQL, Replication, Server, Master,
canonical_url: https://scalegrid.io/blog/re-slaving-a-crashed-mysql-master-server-in-semisynchronous-replication-setup/
---
<link rel="canonical" href="https://scalegrid.io/blog/re-slaving-a-crashed-mysql-master-server-in-semisynchronous-replication-setup/" />

<a href="https://scalegrid.io/blog/re-slaving-a-crashed-mysql-master-server-in-semisynchronous-replication-setup/"><img src="https://scalegrid.io/blog/wp-content/uploads/2019/12/Re-Slaving-a-Crashed-MySQL-Master-Server-in-Semisynchronous-Replication-Setup-ScaleGrid-Blog.png" width="100%" height="auto"></a>

<p style="text-align: justify;">In a <a style="color: #2da964;" href="https://scalegrid.io/mysql.html" target="_blank">MySQL</a> 5.7 master-slave setup that uses the default semisynchronous replication setting for <tt><a style="color: #2da964;" href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-master.html#sysvar_rpl_semi_sync_master_wait_point" target="_blank">rpl_semi_sync_master_wait_point</a></tt>, a crash of the master and failover to the slave is considered to be lossless. However, when the crashed master comes back, you may find that it has transactions that are not present in the current master (which was previously a slave). This behavior may be puzzling, given that <a style="color: #2da964;" href="https://scalegrid.io/blog/mysql-high-availability-framework-explained-part-2/" target="_blank">semisynchronous replication</a> is supposed to be lossless, but this is actually an expected behavior in MySQL. Why exactly this happens is explained in full detail in the <a style="color: #2da964;" href="https://www.percona.com/community-blog/2018/08/23/question-about-semi-synchronous-replication-answer-with-all-the-details/" target="_blank">blog post</a> by <a style="color: #2da964;" href="https://www.percona.com/community-blog/author/author_jfgagne/" target="_blank">Jean-François Gagné</a> (JF).</p>
<p style="text-align: justify;">Given such a scenario, <a style="color: #2da964;" href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-master.html#sysvar_rpl_semi_sync_master_wait_point" target="_blank">MySQL documentation</a> recommends that the crashed master must be discarded and should not be restarted. However, discarding a server like this is expensive and inefficient. In this <a href="https://scalegrid.io/blog/re-slaving-a-crashed-mysql-master-server-in-semisynchronous-replication-setup/">blog post</a>, we will explain an approach to detect and fix transactions on the crashed MySQL master server in a semisynchronous replication setup, and how to re-slave it back into your master-slave setup.</p>

<h2 style="padding-top:15px;">Why Is It Important to Detect Extra Transactions on the Recovered Master?</h2>
<p style="text-align: justify;">The extra transactions on the recovered master can manifest in two ways:</p>

<h3 style="padding-top:15px;">1. MySQL replication failures when the recovered master is re-slaved</h3>
<p style="text-align: justify;">Typically, this happens when you have an auto-increment primary key. When the new MySQL master inserts a row into such a table, the replication will fail because the key already exists on the slave.</p>
<p style="text-align: justify;">Another scenario is when your app retries the transaction that had failed during master crash. On the recovered MySQL master (which is now a slave), this transaction would actually exist, and again, results in a replication error.</p>
<p style="text-align: justify;">Typically, the MySQL replication error would look like this:</p>
<table>
  <tr>
    <td style="padding:20px; background:#eeeeee;"><tt>[ERROR] Slave SQL for channel '': Worker 5 failed executing transaction 'fd1ba8f0-cbee-11e8-b27f-000d3a0df42d:5938858' at master log mysql-bin.000030, end_log_pos 10262184; Error 'Duplicate entry '5018' for key 'PRIMARY'' on query. Default database: 'test'. Query: 'insert into test values(5018,2019,'item100')', Error_code: 1062</tt></td>

  </tr>
</table>
<h3 style="padding-top:15px;">2. Silent inconsistency in data between the new MySQL master and slave (recovered master)</h3>
<p style="text-align: justify;">In cases where the application does not retry the failed transaction and there are no primary key collisions in future, a replication error may not occur. As a result, the data inconsistency may go undetected.</p>
<p style="text-align: justify;">In both the cases above, either the high-availability or data integrity of your MySQL setup is impacted, which is why it’s so important to detect this condition as early as possible.</p>

<h2 style="padding-top:15px;">How to Detect Extra Transactions on the Recovered MySQL Master</h2>
<p style="text-align: justify;">We can detect if there are any extra transactions on the recovered master using the MySQL GTID (global transaction identifier) function:</p>
<tt><a style="color: #2da964;" href="https://dev.mysql.com/doc/refman/5.7/en/gtid-functions.html#function_gtid-subset" target="_blank">GTID_SUBSET(<b><i>set1</i></b>,<b><i>set2</i></b>)</a>: Given two sets of global transaction IDs <b><i>set1</i></b> and <b><i>set2</i></b>, returns true if all GTIDs in <b><i>set1</i></b> are also in <b><i>set2</i></b>. Returns false otherwise.</tt>
<p style="text-align: justify;">Let’s use an example to understand this.</p>

<ul>
	<li style="padding-top:15px;">GTID set on the recovered master whose UUID is: ‘<strong><tt>54a63bc3-d01d-11e7-bf52-000d3af93e52</tt></strong>’ is:
<ul>
	<li><tt>'54a63bc3-d01d-11e7-bf52-000d3af93e52:1-9700,57956099-d01d-11e7-80bc-000d3af97c09:1-810’</tt></li>
</ul>
</li>
	<li style="padding-top:15px;">The GTID set of the new master whose UUID is: ‘<strong><tt>57956099-d01d-11e7-80bc-000d3af97c09</tt></strong>’ is:
<ul>
	<li style="padding-bottom:15px;"><tt>'54a63bc3-d01d-11e7-bf52-000d3af93e52:1-9690,57956099-d01d-11e7-80bc-000d3af97c09:1-870’</tt></li>
</ul>
</li>
</ul>
<p style="text-align: justify;">Now, if we call the GTID_SUBSET function as <tt>GTID_SUBSET(GTID set of recovered master, GTID set of new master)</tt>, the return value will be true, only if the recovered master does not have any extra transactions. In our example above, since the recovered master has extra transactions 9691 to 9700, the result of the above query is false.</p>

<h2 style="padding-top:15px;">How to Re-Slave the Recovered MySQL Master That Has Extra Transactions</h2>
<p style="text-align: justify;">Based on the above step, it is possible to know if the recovered master has extra transactions, and what these transactions are using the GTID function: <tt><a style="color: #2da964;" href="https://dev.mysql.com/doc/refman/5.7/en/gtid-functions.html#function_gtid-subtract" target="_blank">GTID_SUBTRACT</a>(GTID set of recovered master, GTID set of new master).</tt></p>
<p style="text-align: justify;">It is also possible to extract these extra transactions from the binary logs and save them. It may be useful for your business team to later review these transactions to make sure we are not inadvertently losing any important business information, even though it was uncommitted. Once this is done, we need a way to get rid of these extra transactions so that the recovered master can be re-slaved without issues.</p> 
<p style="text-align: justify;">One of the simplest ways to do this is to take a backup snapshot on the current master and restore the data onto your current slave. Remember that you need to retain the UUID of this server as before. After you’ve restored the data, the server can be re-slaved, and it will start replication from the point of the restored snapshot. You will soon have a healthy slave running again!</p>
<p style="text-align: justify;">The steps above are very tedious if you have to perform them manually, but ScaleGrid's fully managed <a style="color: #2da964;" href="https://scalegrid.io/mysql.html" target="_blank">MySQL hosting</a> service can automate the entire process for you without any intervention required. Here's how it works:</p>
<p style="text-align: justify;">If your current master crashes, ScaleGrid automates the failover process and promotes a suitable slave as the new master. The old master is then recovered, and we automatically detect if there are extra transactions on it. If any are found, the MySQL deployment is put in a degraded state we use automated tools to pull out the extra transactions and save them for your review. Our support team can then restore the old master to a good state, and re-slave it back into your master-slave setup so that you will have a healthy deployment!</p>
<p style="text-align: justify;padding-bottom:20px;">Want to give it a try? Start a <a style="color: #2da964;" href="https://console.scalegrid.io/users/register" target="_blank">free 30-day trial</a> to explore all the MySQL database management capabilities at ScaleGrid.</p>