MySQL high availability cluster with DRBD+Heartbeat+MySQL – You can also download the cleaner version of this document by clicking here – Download here

1) Set up hostnames and network

On all Nodes. Please copy this file on all servers/nodes

Run this on db1 and db2 server

nano /etc/hosts

##### IP’s of MySQL DB servers ##### db1.wcws.net db1 db2.wcws.net db2

Run this on db1 and db2 server

ssh-copy-id root@db2
ssh-copy-id root@db1

In my configuration I, Installed CentOS with just one hard drive on both servers and the drive I just to boot centos was 10GB therefore I have added another drive with 10GB Space space, please note each server hard drive must be identical in size other wise you will run into problems.

I am assuming you have already added a secondary disk with 10GB so lets start some serious configuration and feel yourself proud.

Following these steps and you will thank me later

[root@db1 ~]# nano /etc/selinux/config


chkconfig iptables off

Make sure your firewall is OFF – you can also do that by running setup command on your shell and follow the steps

Partion setup on both server identical same with fdisk

Once I added a drive my drive was named as /dev/sdb – please verify that before you do any thing

So in my server after I added an additional disk my drive was named as /dev/sdb which you can also verify by running fdisk –l

Lets create partition

fdisk /dev/sdb

primary partition – press p

Partition number (1-4): 1


Press t and type 8e – HIT ENTER
TYPE w to write  once done simply type


Secondary we will create some logical volume

pvcreate /dev/sdb1

vgcreate vgdb /dev/sdb1

Create Logical volume partition

lvcreate -L +10000M -n lvdb vgdb

RUN ABOVE STEP ON BOTH DB1 & DB2 Server and please make sure the drive size are exact and accurate.

Once everything is done simply reboot your server

Now the fun part J YEA BABY

rpm -Uvh http://elrepo.org/elrepo-release-6-4.el6.elrepo.noarch.rpm

nano /etc/yum.repos.d/elrepo.repo


name=ELRepo.org Community Enterprise Linux Repository – el6







yum –enablerepo=elrepo install drbd83-utils kmod-drbd83 mysql mysql-server heartbeat heartbeat-pils heartbeat-stonith –y

Once installed simply run the following command as we don’t want mysql to start on boot time

chkconfig mysqld off
chkconfig heartbeat off
chkconfig drbd off

now reboot your server – PLEASE NOTE ALL ABOVE COMMANDS MUST BE RUN ON BOTH DB1 and DB2 Server.

Now your servers are up and running do the following

[root@db1 ~]modprobe drbd

[root@db2 ~]modprobe drbd

[root@db1 ~]echo “modprobe drbd” > /etc/rc.local

[root@db2 ~]echo “modprobe drbd” > /etc/rc.local

[root@db1 ~] nano /etc/drbd.conf

and delete all the lines from your current drbd.conf file and copy the following

global {usage-count no;


common {

syncer {

rate 3M;



resource r0 {

protocol C;

handlers {

split-brain “/usr/lib/drbd/notify-split-brain.sh root”;

pri-on-incon-degr “/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f”;

pri-lost-after-sb “/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f”;

local-io-error “/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f”;


startup {

degr-wfc-timeout 120;


disk {

on-io-error detach;


net {

# The following lines are dedicated to handle

# split-brain situations (e.g., if one of the nodes fails)

after-sb-0pri discard-zero-changes; # If both nodes are secondary, just make one of them primary

after-sb-1pri discard-secondary; # If one is primary, one is not, trust the primary node

after-sb-2pri call-pri-lost-after-sb; # If there are two primaries, make the unchanged one secondary

rr-conflict disconnect;


syncer {

rate 3M;

al-extents 257;


on db1.wcws.net {

device /dev/drbd0;

disk /dev/vgdb/lvdb;

address; #DEFINE YOUR DB1 IP

flexible-meta-disk internal;


on db2.wcws.net {

device /dev/drbd0;

disk /dev/vgdb/lvdb;

address; #DEFINE YOUR DB2 IP

flexible-meta-disk internal;




Once above file is saved – now simply copy that file to DB2 server

scp /etc/drbd.conf db2:/etc/

Run the following on both servers

echo “modprobe drbd” >> /etc/rc.local

now lets start drbd on both db1 and db2

service drbd start

Run the following commands on both db1 & db2

drbdadm create-md r0

drbdadm attach r0

drbdadm syncer r0

drbdadm connect r0

On Primary Node only

[root@db1 ~]#drbdadm — –overwrite-data-of-peer primary r0

On both Nodes: 

[root@db1 ~]#drbdadm up all

[root@db2 ~]#drbdadm up all

On Primary Node only

[root@db1 ~]#drbdadm — primary all #### ON Node one Only ####

You are done with DRBD Part – now check the sync status by

[root@db1 ~]#watch cat /proc/drbd

only on db1 ########## Primary Node ########

[root@db1 ~]#mkfs.ext3 /dev/drbd0

[root@db1 ~]#mkdir /data/

[root@db1 ~]#mount /dev/drbd0 /data/

On db2 ####### Secondry Node #######

[root@db2 ~]#mkdir /data

[root@db1 ~] nano /etc/ha.d/ha.cf ## Create this file and copy this text ##

logfacility local0
keepalive 2
#deadtime 30 # USE THIS!!!
deadtime 10
bcast eth0
#serial /dev/ttyS0
baud 19200
auto_failback off
node db1.wcws.net
node db2.wcws.net


Server db1 

 [root@db1 ~]#vi /etc/ha.d/haresources

db1.wcws.net IPaddr:: drbddisk::r0 Filesystem::/dev/drbd0::/data::ext3 mysqld

Server db2:

 [root@db2 ~]#vi /etc/ha.d/haresources

db2.wcws.net IPaddr:: drbddisk::r0 Filesystem::/dev/drbd0::/data::ext3 mysqld

On Both Servers:

 [root@db1 ~]#vi /etc/ha.d/authkeys

auth 3
3 md5 wcws

[root@db1 ~]#chmod 600 /etc/ha.d/authkeys

[root@db1 ~]#scp /etc/ha.d/authkeys db2:/etc/ha.d/authkeys

[root@db1 ~]#chkconfig heartbeat on

Mysql Configuration. 


cp /etc/my.cnf /etc/my.cnf.orig


vi /etc/my.cnf



# datadir=/var/lib/mysql




# Default to using old password format for compatibility with mysql 3.x

# clients (those using the mysqlclient10 compatibility package).
















Run the following command on both servers db1 & db2

chkconfig drbd on

Copy my.cnf file on db2 server

scp /etc/my.cnf db2:/etc/

 Now simply restart your server and connect your web server to your FLOATING IP which we define in haresource file i.e. – Connect to MYSQL setup your root password after you create your database In MySQL simply run this command to give grant access to your DB


In my case 212 IP is – IP of my web server


One last thing

nano /etc/aliases

go at the very bottom and on both db1 & db2 server and change root  like this

root: youremail@gmail.com

– Congrats you just setup a DRBD HIGH AVAILABILITY

Please do leave me comments and let me know what you think about this tutorial and if that help’s you – I would love to get your feedbacks.



  1. Saad OUACHE
      June 4, 2013

    thanks for the good work 🙂
    when i did the setup for the first time i used lcmc to setup heartbeat and pacemaker for automatic failover.

    • sohaibwcws
        June 4, 2013

      Thank you for your comments – Appreciate it . I am not a big fan of GUI especially LCMC – AS that confuse me a lot I have more control over command line and I feel more comfortable this way.

Leave a Reply

Your email address will not be published. Required fields are marked *