Operations 8 min read

Building a Three‑Server High‑Availability MySQL Cluster with HAProxy on Almalinux

This guide explains why three servers are needed for high availability, walks through hardware and software preparation, network configuration, MySQL master‑slave replication setup, HAProxy load‑balancing, and firewall/SELinux adjustments, providing complete command‑line examples for each step.

IT Xianyu
IT Xianyu
IT Xianyu
Building a Three‑Server High‑Availability MySQL Cluster with HAProxy on Almalinux

This article demonstrates how to create a reliable three‑server environment on Almalinux, covering the reasons for using multiple machines, required hardware, essential software tools, network configuration, MySQL master‑slave replication, HAProxy load balancing, and firewall/SELinux tuning.

Why Use Three Servers?

Running a single server creates a single point of failure; if the database crashes, the hard drive fails, or a destructive command like rm -rf /* is executed, the service becomes unavailable. Three servers provide redundancy, load balancing, and automatic failover.

Equipment Preparation

Minimum hardware : 4‑core CPU, 8 GB RAM, 100 GB SSD (or larger).

Network : All three machines must be on the same LAN with at least gigabit NICs.

OS : Almalinux 9.2 (avoid outdated CentOS 7.9).

Software Tools

Install the basic utilities on every node:

sudo yum install -y vim net-tools openssl sshd
sudo systemctl start sshd
# enable password‑less SSH between nodes
ssh-copy-id user@other-node

Step‑by‑Step: Network Configuration

After installation, the network may not work. Edit the appropriate interface file (e.g., /etc/sysconfig/network-scripts/ifcfg-ens192 ) and set static parameters:

BOOTPROTO=static
IPADDR=192.168.1.101   # use .102 and .103 for the other two nodes
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8

Restart the network service with sudo systemctl restart network .

YUM Repository Fix

If yum install cannot find packages, replace the default repo with the Alibaba Cloud mirror:

sudo mv /etc/yum.repos.d/almalinux.repo /etc/yum.repos.d/almalinux.repo.backup
curl -o /etc/yum.repos.d/almalinux.repo https://mirrors.aliyun.com/almalinux/almalinux.repo
sudo yum clean all && sudo yum makecache

MySQL Master‑Slave Replication

Install MySQL on all three machines and configure the master (192.168.1.101):

sudo yum install mysql-server -y
sudo systemctl start mysqld
# edit /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-format=ROW
# create replication user
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'YourPassword123!';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

On the two slaves (192.168.1.102 and 192.168.1.103) set a unique server-id and point to the master:

[mysqld]
server-id=2   # or 3 for the third node
relay-log=mysql-relay-bin
# connect to master
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.101', MASTER_USER='repl', MASTER_PASSWORD='YourPassword123!', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
mysql> START SLAVE;

If the slave reports “Got fatal error from master”, run SHOW MASTER STATUS on the master and update the slave with the correct binlog file and position.

HAProxy Load Balancing

Install HAProxy on any node (e.g., the master) and configure it to balance MySQL traffic:

sudo yum install haproxy -y
# /etc/haproxy/haproxy.cfg
frontend mysql_front
    bind *:3306
    mode tcp
    default_backend mysql_back
backend mysql_back
    mode tcp
    balance roundrobin
    server db1 192.168.1.101:3306 check
    server db2 192.168.1.102:3306 check backup
    server db3 192.168.1.103:3306 check backup

Clients connect to the HAProxy port 3306; HAProxy will route reads/writes and automatically fail over if the master goes down.

Firewall and SELinux

Typical connectivity problems stem from firewalls or SELinux. Either disable them (not recommended for production) or open the required port:

# stop and disable firewalld (use with caution)
sudo systemctl stop firewalld
sudo systemctl disable firewalld
# or open MySQL port
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
# temporarily disable SELinux
sudo setenforce 0
# permanent disable: edit /etc/selinux/config and set disabled

Conclusion

Deploying three servers for a MySQL cluster provides high availability and load balancing, but it requires careful hardware selection, network setup, repository configuration, replication tuning, HAProxy integration, and security adjustments. Remember to back up frequently and use tools like grep and journalctl for troubleshooting; future automation with Ansible can reduce deployment time to minutes.

High AvailabilityMySQLLinux operationsHAProxyserver clusteringAlmaLinux
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login 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.