Important Health Checks for your MySQL Master-Slave Servers
Tags: mysql, database, servers, monitoring
Published: 2020-02-17 20:17:01

---
title: Important Health Checks for your MySQL Master-Slave Servers
published: true
description: ScaleGrid Blog - MySQL health checks to monitor your high availability setup and detect potential issues so you can take corrective actions on your master slave servers
tags: MySQL, Database, Servers, Monitoring,
---
<link rel="canonical" href="https://scalegrid.io/blog/important-health-checks-for-your-mysql-master-slave-servers/" />
<a href="https://scalegrid.io/blog/important-health-checks-for-your-mysql-master-slave-servers/" target="_blank"><img src="https://scalegrid.io/blog/wp-content/uploads/2020/02/Important-Health-Checks-for-your-MySQL-Master-Slave-Servers-ScaleGrid-Blog.jpg" width="100%" height="auto"></a>


<p style="text-align: justify;">In a <a title="MySQL Hosting" href="https://scalegrid.io/mysql.html" target="_blank" style="color:#2da964;">MySQL</a> master-slave high availability (HA) setup, it is important to continuously monitor the health of the master and slave servers so you can detect potential issues and take corrective actions. In this blog post, we explain some basic <a href="https://scalegrid.io/blog/important-health-checks-for-your-mysql-master-slave-servers/" target="_blank" style="color:#2da964;">health checks you can do on your MySQL master and slave nodes</a> to ensure your setup is healthy. The monitoring program or script must alert the <a href="https://scalegrid.io/blog/mysql-high-availability-framework-explained-part-3/" target="_blank" style="color:#2da964;">high availability framework</a> in case any of the health checks fails, enabling the high availability framework to take corrective actions in order to ensure service availability.</p>

<h2 style="padding-top:15px;">MySQL Master Server Health Checks</h2>
<p style="text-align: justify;">We recommended that your MySQL master monitoring program or scripts runs at frequent intervals. Assuming that the monitoring script is running on the same server as your MySQL server, you can check for the following:</p>

<ol>
	<li class="has-heading">
<h3 class="heading" style="padding-top:15px;">Ensure the MySQL service is running</h3>
<p style="text-align: justify;">This can be done using a simple command like:</p>

<pre>&gt; pgrep mysqld</pre>
OR
<pre>&gt;service mysqld status</pre>
</li>
	<li class="has-heading">
<h3 class="heading" style="padding-top:15px;">Ensure you can connect to MySQL and do a simple query</h3>
<p style="text-align: justify; padding-bottom:8px;">We recommended having a short timeout for these commands so you can quickly detect if MySQL is unresponsive. This can be achieved from a call like:</p>

<pre>/usr/bin/timeout 5 mysql -u testuser -ptestpswd -e 'select * from mysql.test’</pre>
<p style="text-align: justify; padding-top:8px;">Be sure to examine the exit value of the above command:</p>
<p style="text-align: justify; padding-left: 30px;">Exit value=0 ⇒ Success</p>
<p style="text-align: justify; padding-left: 30px;">Exit value=1 ⇒ Failure</p>
<p style="text-align: justify; padding-left: 30px;">Exit-value=124 ⇒ Timeout</p>
<p style="text-align: justify;">If the command times out, it means that the MySQL service is not responsive enough. We advice you retry after some time so as to avoid false negative results. If the exit code indicates a failure, the return code from MySQL will tell us the failure reason. One example of a failure is the ‘Too many connections’ error from MySQL which happens if the number of connections to the server exceeds your ‘max_connections’ configuration value.</p>
</li>
	<li class="has-heading">
<h3 class="heading" style="padding-top:15px;">Ensure the MySQL master is running in read-write mode</h3>
<p style="text-align: justify; padding-bottom:8px;">You can use the following command to ensure the MySQL master is running in read-write mode:</p>
<pre>/usr/bin/timeout 5 mysql -u testuser -ptestpswd -e "SELECT @@global.read_only"</pre>
<p style="text-align: justify; padding-top:8px;">The master is expected to be always running in read-write mode, and hence, the value of  read_only should be ‘OFF’.</p>
<p style="text-align: justify;">It is also possible to club this step with step 2, and instead of doing the test query 'select * from mysql.test, we can just do the query to get the read_only value.</p>
</li>
</ol>

<h2 style="padding-top:15px;">MySQL Slave Server Health Checks</h2>
<p style="text-align: justify;">You can run the monitoring for your MySQL slaves at a lesser frequency compared to the master, as they are not handling data writes. The first 3 steps for your slave health check can be the same as that of the master, except that we need to ensure the slave is running in read-only mode - the value of the variable read_only should be ‘ON’ in step-3.</p>
<p style="text-align: justify;">In addition, we can do more checks on the slave to ensure its replication status is healthy, such as:</p>

<ol>
	<li style="text-align: left;"><p style="text-align: justify;">The slave is configured to replicate from the right master.</p></li>
	<li style="text-align: left;"><p style="text-align: justify;">The slave’s connection to the master is healthy.</p></li>
	<li style="text-align: left;"><p style="text-align: justify;">The slave is able to apply the master events it has received.</p></li>
</ol>
<p style="text-align: justify; padding-bottom:8px;">It's possible to check for all the above using the ‘show slave status’ command. For example:</p>

<pre>mysql&gt; show slave status \G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.31.17.43

Master_User: repl_user

Master_Port: 3306

Connect_Retry: 10

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 7510

Relay_Log_File: relay-log.000006

Relay_Log_Pos: 414

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

******************Truncated*********************************</pre>
<ul>
	<li><p style="text-align: justify; padding-top:8px;">The Master_Host value indicates the master server is configured for replication.</p></li>
	<li><p style="text-align: justify;">For the Slave_IO_Running value, "Yes" indicates that the slave has connected to the master and is receiving the replication stream.</p></li>
	<li><p style="text-align: justify;">For the Slave_SQL_Running value, "Yes" indicates that the slave’s applier is running and able to apply all the events received from the master.</p></li>
</ul>
<p style="text-align: justify; padding-bottom:20px;">In this blog post, we discussed some simple checks that can detect if there are basic issues in your MySQL master and slave servers. In general, the failure detection mechanism in a high availability setup is a complex subject and needs a robust high availability framework through which health check monitoring should be implemented. You can learn more about the details of our high availability framework in our <a href="https://scalegrid.io/blog/mysql-high-availability-framework-explained-part-1/"  target="_blank" style="color:#2da964;">MySQL High Availability Framework Explained – Part I: Introduction</a> blog post.</p>