Databases 16 min read

How to Build a MariaDB High‑Availability Cluster with Corosync & Pacemaker on CentOS

This step‑by‑step guide shows how to configure a three‑node MariaDB HA cluster on CentOS using NFS shared storage, corosync multicast heartbeat, pacemaker resource management, and a virtual IP, including installation, configuration files, and failover testing.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a MariaDB High‑Availability Cluster with Corosync & Pacemaker on CentOS

Experiment Description

The goal is to achieve high availability for MariaDB by manually configuring the corosync configuration file, using multicast for cluster heartbeat.

Three nodes: node5.redhat.com (172.16.100.5), node6.redhat.com (172.16.100.6), node7.redhat.com (172.16.100.7).

Backend storage is provided by NFS at 172.16.0.254.

Virtual IP address: 172.16.100.100.

All three nodes run CentOS 7.2; the NFS node runs CentOS 6.5.

The first three steps for the HA cluster are time synchronization, hostname resolution, and SSH trust.

Configuration Steps

1. NFS Node Configuration

Install NFS utilities and create shared directories.

[root@centos6 ~]# yum install nfs-utils -y
[root@centos6 ~]# mkdir /data/{mydata,logs} -pv
[root@centos6 ~]# vim /etc/exports

Export the directory to the 172.16.0.0/16 network with read‑write permissions and no root squashing.

Start the NFS service.

Create the mysql user and group with matching IDs on all nodes.

[root@centos6 ~]# groupadd -r -g 240 mysql
[root@centos6 ~]# useradd -r -u 240 -g 240 mysql
[root@centos6 ~]# chmod -R 775 /data
[root@centos6 ~]# chown -R mysql.root /data

2. Install and Configure MariaDB on Each Node

On node5, install nfs‑utils, create the mysql user/group, mount the NFS share, extract the MariaDB binaries, and create a symbolic link.

[root@node5 ~]# yum install nfs-utils -y
[root@node5 ~]# groupadd -r -g 240 mysql
[root@node5 ~]# useradd -r -u 240 -g 240 mysql
[root@node5 ~]# mkdir /data
[root@node5 ~]# mount -t nfs 172.16.0.254:/data /data
[root@node5 ~]# tar xf mariadb-10.0.24-linux-x86_64.tar.gz -C /usr/local/
[root@node5 ~]# cd /usr/local/
[root@node5 local]# ln -sv mariadb-10.0.24-linux-x86_64 mysql

Install and configure MariaDB.

[root@node5 local]# cd mysql/
[root@node5 mysql]# ./scripts/mysql_install_db --datadir=/data/mydata --user=mysql
[root@node5 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@node5 mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@node5 mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysqld.sh
[root@node5 mysql]# . /etc/profile.d/mysqld.sh
[root@node5 mysql]# vim /etc/my.cnf

Example my.cnf configuration:

[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 2
datadir = /data/mydata
innodb_file_per_table = 1
log-bin=/data/logs/mysql-bin
binlog_format=mixed
server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

Start MariaDB, create a test user, then stop the service and unmount the NFS share.

[root@node5 mysql]# /etc/init.d/mysqld start
[root@node5 mysql]# mysql -e "GRANT ALL ON *.* TO 'ha'@'%' IDENTIFIED BY 'redhat'; FLUSH PRIVILEGES;"
[root@node5 mysql]# /etc/init.d/mysqld stop
[root@node5 ~]# umount /data

Copy my.cnf to the other nodes and repeat the above steps.

Note: In step 3 you only need to create the directory; mounting is not required again. Also copy the startup script: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

Distribute the configuration file:

[root@node5 ~]# scp /etc/my.cnf node6.redhat.com:/etc/my.cnf
[root@node5 ~]# scp /etc/my.cnf node7.redhat.com:/etc/my.cnf

3. Configure Corosync Cluster for MariaDB HA

Install corosync and pacemaker on all three nodes.

[root@node5 ~]# yum install corosync pacemaker -y
[root@node6 ~]# yum install corosync pacemaker -y
[root@node7 ~]# yum install corosync pacemaker -y

Create /etc/corosync/corosync.conf (example below).

totem {
  version: 2
  crypto_cipher: aes128
  crypto_hash: sha1
  secauth: on
  interface {
    ringnumber: 0
    bindnetaddr: 172.16.0.0
    mcastaddr: 239.255.1.1
    mcastport: 5405
    ttl: 1
  }
}

nodelist {
  node { ring0_addr: 172.16.100.5 nodeid: 1 }
  node { ring0_addr: 172.16.100.6 nodeid: 2 }
  node { ring0_addr: 172.16.100.7 nodeid: 3 }
}

logging {
  fileline: off
  to_stderr: no
  to_logfile: yes
  logfile: /var/log/cluster/corosync.log
  to_syslog: yes
  debug: off
  timestamp: on
  logger_subsys { subsys: QUORUM debug: off }
}

quorum {
  provider: corosync_votequorum
}

Generate the authentication key with corosync-keygen. If the command hangs on random number generation, replace /dev/random with /dev/urandom temporarily.

[root@node5 corosync]# cd /dev/
[root@node5 dev]# mv random random.bak
[root@node5 dev]# ln -sv urandom random
[root@node5 dev]# corosync-keygen
[root@node5 dev]# rm -rf random
[root@node5 dev]# mv random.bak random

Copy corosync.conf and authkey to the other nodes.

[root@node5 ~]# scp /etc/corosync/corosync.conf authkey node6.redhat.com:/etc/corosync/
[root@node5 ~]# scp /etc/corosync/corosync.conf authkey node7.redhat.com:/etc/corosync/

Start corosync and pacemaker services on all nodes.

[root@node5 ~]# systemctl start corosync.service
[root@node5 ~]# systemctl start pacemaker.service
[root@node6 ~]# systemctl start corosync.service
[root@node6 ~]# systemctl start pacemaker.service
[root@node7 ~]# systemctl start corosync.service
[root@node7 ~]# systemctl start pacemaker.service

Verify cluster membership with crm_mon (image).

4. Install crmsh and Configure Resources

Install crmsh on node5 and configure the cluster resources.

[root@node5 ~]# yum install crmsh-2.2.0-7.1.noarch.rpm crmsh-scripts-2.2.0-7.1.noarch.rpm python-parallax-1.0.1-10.1.noarch.rpm -y
[root@node5 ~]# crm configure
crm(live)configure# property stonith-enabled=false
crm(live)configure# primitive mysqlip ocf:heartbeat:IPaddr params ip='172.16.100.100' op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# primitive mysqlstore ocf:heartbeat:Filesystem params device='172.16.0.254:/data' directory='/data' fstype='nfs' op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# primitive mysqlserver lsb:mysqld params op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# group mysqlservice mysqlip mysqlstore mysqlserver
crm(live)configure# order mysqlip_before_mysqlstore Mandatory: mysqlip mysqlstore
crm(live)configure# order mysqlstore_before_mysqlserver Mandatory: mysqlstore mysqlserver
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# bye

The resources are now fully configured.

Testing Steps

Check node status with crm status (image).

Install MySQL client on the NFS server and connect to the virtual IP.

[root@centos6 ~]# yum install mysql -y
[root@centos6 ~]# mysql -h172.16.100.100 -uha -predhat

Create a test database and verify access.

Put node5 into standby mode and observe resource migration (image).

[root@node5 ~]# crm node standby node5.redhat.com
[root@node5 ~]# crm status

Set node6 to standby and verify migration to node7 (image).

[root@node5 ~]# crm node standby node6.redhat.com
[root@node5 ~]# crm status

Bring the nodes back online and confirm the cluster is healthy (image).

[root@node5 ~]# crm node online node5.redhat.com
[root@node5 ~]# crm node online node6.redhat.com
[root@node5 ~]# crm status

The cluster now provides high‑availability access to MariaDB via the virtual IP.

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 availabilityClusterNFSCentOSMariaDBCorosyncPacemaker
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.