Databases 7 min read

How to Build a High-Availability MySQL Cluster with MHA: Step‑by‑Step Guide

This tutorial walks through setting up a MySQL master‑slave replication cluster, configuring password‑less SSH, installing MHA‑node and MHA‑manager, creating the MHA configuration, validating the setup, and testing automatic failover when the master goes down.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Build a High-Availability MySQL Cluster with MHA: Step‑by‑Step Guide

The previous article introduced MHA's function and principle; this guide shows how to build an MHA environment, simulate a master failure, and verify automatic failover.

Goal

Build an MHA environment, simulate a master failure, and confirm that a new master is elected correctly.

Setup Process

Initially four servers were planned: host1 as master, host2 and host3 as slaves, and host4 as the MHA manager. Due to limited resources the demo uses three servers.

Steps

Build replication environment – configure one master and two slaves.

Configure SSH password‑less login – on each host run ssh-keygen and then

ssh-copy-id -i /root/.ssh/id_rsa -p 22 [email protected]
ssh-copy-id -i /root/.ssh/id_rsa -p 22 [email protected]
ssh-copy-id -i /root/.ssh/id_rsa -p 22 [email protected]

.

Install MHA‑node – on every server execute

yum -y install perl-DBD-MySQL ncftp perl-DBI.x86
rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm

.

Install MHA‑manager – on the designated manager host run

yum install epel-release-7-5.noarch
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager rrdtool perl-rrdtool rrdtool-devel perl-Params-Validate
rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm

. (CentOS‑specific; adjust for other distros.)

Create MHA configuration file – edit /etc/mha/mha.cnf with sections such as [server default] (user, password, work directories, ssh_user, repl_user, etc.) and individual server definitions, marking candidates with candidate_master=1 or no_master=1.

Validate SSH – run masterha_check_ssh --conf=/etc/mha/mha.cnf.

Validate replication – run masterha_check_repl --conf=/etc/mha/mha.cnf.

. If the error "Can't exec \"mysqlbinlog\"" appears, create symbolic links on all servers:

ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

(adjust paths for your environment).

Start manager – nohup masterha_manager --conf=/etc/mha/mha.cnf & and check status with masterha_check_status --conf=/etc/mha/mha.cnf.

Failover test – stop MySQL on the original master (192.168.31.204), then examine the manager log ( /home/mha/manager.log) and the slave status on 192.168.31.101. The new master IP should be 192.168.31.168, confirming that slave1 was promoted and replication threads are running.

Download links for MHA packages and documentation are provided at the end of the article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

high availabilityLinuxmysqlDatabase ReplicationMHAfailover
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.