MySQL Synchronous Replication on Ubuntu 12.04 with DRBD

mysql-5.5Ubuntu

I have been looking around for some easy and open-source ways to handle database synchronous replication.

DRDB on a Linux server seems to be one of the fastest and easiest methods to handle database synchronization for DR purposes and HA.

I want to set up the above fact between two ubuntu 12.04 Machines treating one machine as active and another as passive.

I need some handy guidance and useful steps to setup this fact.

Best Answer

I would suggest you to use active-passive cluster with drbd. Active-Active leads to more problems then it solves especially if you use MySQL as database because you can't run 2 MySQL Daemons on the same Database!

Creating drbd cluster is simple. You have to do the following steps on both servers.

  1. Create a partition for share
  2. Create a drbd config with the partition, this brings you a new block device
  3. Start the Cluster with one as Master and the other as slave
  4. Create and Mount a filesystem (ext4, xfs or on the block device on the master
  5. Copy the database to the new filesystem and start db and you are done.

Thats the concept.

Here a short overview of the commands to use:

Server1 and Server2: Create a partition for the complete Harddisk sdb

fdisk /dev/sdb
n
p
<enter>
<enter>
w

Install drbd

apt-get install drbd8-utils

Create a config file at /etc/drbd.conf:

common {
  syncer { 
    rate 100M; 
  }
}

resource r0 {
  # Protokoll-Version
  protocol C;

  startup {
    wfc-timeout         0;
    degr-wfc-timeout  120;
  }
  disk {
    on-io-error detach;
  }
  on server1 {
    device     /dev/drbd0;
    disk       /dev/sdb1;
    address    <IPSERVER1>:7788;
    meta-disk  internal;                 
  }
  on server2 {
    device     /dev/drbd0;
    disk       /dev/sdb1;
    address    <IPSERVER2>:7788;
    meta-disk  internal;                 
  }
}

Create the drbd device on both servers

drbdadm create-md r0
drbdadm up r0

Check the state

cat /proc/drbd

There should be a line with "cs:Connected ro:Secondary/Secondary" The first "Secondary" is the server you are logged in and the 2nd "Secondary" is the other server.

Now make the first server to the master and override all data on the server2

drbdadm primary --force r0

You could watch this process in /proc/drbd

No you can mount your new drbd device on the master:

mount /dev/drbd0 /mnt

if you like to switch master and slave you can use the commands:

drbdadm primary r0

or

drbdadm secondary r0

Based on the server where you are logged in. But remember only one server could be primary.

For Active-Active scenario you need a cluster file system like ocfs2.

Use this manual with caution because this could delete your data.

You will find more information at drbd.org: http://www.drbd.org/users-guide-8.4/

A complete alternative is to use the DRBD MC - the Management console created by LINBIT, its a java application running on a workstation and connects to the servers over ssh and make the configuration for you. http://www.drbd.org/mc/lcmc/