Build a High‑Availability MySQL Cluster with Corosync, Pacemaker & DRBD
Learn how to set up a fault‑tolerant MySQL environment by installing and configuring Corosync, Pacemaker, DRBD, and related resources across two servers, covering prerequisites, network sync, storage setup, resource definitions, and verification steps to achieve a fully operational HA cluster.
Implementation topology diagram:
Experiment Procedure:
先对两主机安装上corosync和pacemaker
两主机安装drbd服务,并且设置好drbd的组设备
选择drbd主节点上,进行数据库的初始化
进行资源的定义和配置
测试Pre‑experiment preparation:
1. Time must be synchronized
使用ntp服务器2. Nodes must be able to resolve each other's names
解析节点名称
/etc/host
集群中使用的主机名为`uname -n`表示的主机名;3. Ping node
仅偶数节点才需要;4. SSH key authentication for communication
Experiment Steps:
Install corosync and pacemaker:
yum install corosync pacemaker -yConfigure corosync and let pacemaker run as a plugin:
vim /etc/corosync/corosync.conf
compatibility: whitetank
totem {
version: 2
secauth: off
threads: 0
interface {
ringnumber: 0
bindnetaddr: 172.16.100.0
mcastaddr: 226.94.1.1
mcastport: 5405
ttl: 1
}
}
service {
ver: 0
name: pacemaker
}
aisexec {
user: root
group: root
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
to_syslog: yes
logfile: /var/log/cluster/corosync.log
debug: off
timestamp: on
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled
}Generate corosync key file:
corosync-keygenInstall DRBD and configure global and resource files (the partition /dev/sdb1 is prepared on both hosts):
rpm -ivh drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm
vim /etc/drbd.d/global_common.conf
global {
usage-count no;
}
common {
protocol C;
handlers {
# example handlers – customize as needed
}
startup {}
options {}
disk {
on-io-error detach;
}
net {
cram-hmac-alg "sha1";
shared-secret "mydrbdlab";
}
syncer {
rate 1000M;
}
}Configure DRBD resource for MySQL data:
vim /etc/drbd.d/mysqldata.res
resource mysqldata {
on www.bwei.com {
device /dev/drbd0;
disk /dev/sdb1;
address 172.16.100.1:7789;
meta-disk internal;
}
on www2.bwei.com {
device /dev/drbd0;
disk /dev/sdb1;
address 172.16.100.5:7789;
meta-disk internal;
}
}
drbdadm create-md mysqldataStart DRBD service and check status:
service drbd start
drbd-overview
# Example output:
0:mysqldata/0 Connected Secondary/Secondary Inconsistent/Inconsistent C r-----Promote one node to primary:
# drbdadm -- --overwrite-data-of-peer primary mysqldata
# drbd-overview
0:mysqldata/0 Connected Primary/Secondary UpToDate/UpToDate C r-----Format the DRBD device and create MySQL data directory:
mke2fs -t ext4 /dev/drbd0
mkdir /data
mount /dev/drbd0 /data
mkdir /data/mysqlCreate MySQL user/group and set ownership:
groupadd -r mysql -g 360
useradd mysql -g mysql -u 360 -r
chown mysql:mysql /data/mysql/Initialize MySQL (MariaDB) on the primary node:
tar xf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local/
ln -sv mariadb-10.0.10-linux-x86_64 mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/
cp mysql/support-files/my-huge.cnf /etc/my.cnf
vim /etc/my.cnf # set datadir = /data/mysql
cp mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld off
service mysqld start
mysql/bin/mysql # verify connectionThe secondary host must receive the same MySQL configuration files and startup script but does not repeat the initialization.
Install crmsh and define cluster resources:
rpm -ivh crmsh-1.2.6-4.el6.x86_64.rpm pssh-2.3.1-2.el6.x86_64.rpm
service corosync start
crm
# Inside crm shell:
configure property no-quorum-policy=ignore
configure property stonith-enabled=false
configure primitive mysqldrbd ocf:linbit:drbd params drbd_resource=mysqldata op monitor interval=30 role=Master timeout=30 op monitor interval=40 role=Slave timeout=30 op start timeout=240 op stop timeout=100
configure primitive mysqlfs ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/data/mysql fstype=ext4 op start timeout=60 op stop timeout=60
configure primitive mysqld lsb:mysqld
configure primitive mysqlvip ocf:heartbeat:IPaddr params ip=172.16.100.4
configure ms ms_mysqldrbd mysqldrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
configure colocation mysqld_with_mysqlfs inf: mysqld mysqlfs
configure order mysqlfs_after_ms_mysqldrbd inf: ms_mysqldrbd:promote mysqlfs:start
configure order mysqld_after_mysqlfs inf: mysqlfs mysqld
commit
crm statusThe services have been successfully configured!
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
