How To Set Up A Load-Balanced MySQL Cluster With MySQL 5.1

Version 1.1
Author: Peter Okupski <okupski [at] widzew [dot] net>

This tutorial is based on Falko Timme's tutorial for MySQL Cluster 5.0. It shows how to configure a MySQL 5.1 cluster with five nodes: 1 x management, 2 x storage nodes and 2 x balancer nodes. This cluster is load-balanced by an Ultra Monkey package which provides heartbeat (for checking if the other node is still alive) and ldirectord (to split up the requests to the nodes of the MySQL cluster).

In this document I use Debian Etch 4.0 for all nodes. Therefore the setup might differ a bit for other distributions. The two data nodes were x64 to use all of the 8GB RAM. Servers were compiled from source so you should be able to make it running on any platform. The MySQL version I use in this setup is 5.1.73. It's a release candidate, but I wanted to use 5.1 to take advantage of Memory-Disk Based tables.

Beginning with MySQL 5.1.6, it is possible to store the non-indexed columns of NDB tables on disk, rather than in RAM as with previous versions of MySQL Cluster.[More here]

This howto is just a scratch to make it running, for many of you I am suggesting to read some off docs from MySQL page to be prepared to how manage the whole cluster and always know whats going on.

This document comes without warranty of any kind! Bare in mind you need to make tests and prepare your databases before using it in production mode.


1 My Servers

I will use the following Debian servers that are all in the same network (10.0.1.x in this example):

  • [ MySQL cluster management server ]
  • [ Load Balancer 1 ]
  • [ Load Balancer 2 ]
  • [ MySQL cluster node 1 ]
  • [ MySQL cluster node 2 ]

In addition to that we need a virtual IP address : It will be assigned to the MySQL cluster by the load balancer so that applications have a single IP address to access the cluster.

Although we want to have two MySQL cluster nodes in our MySQL cluster, we still need a third node, the MySQL cluster management server, for mainly one reason: if one of the two MySQL cluster nodes fails, and the management server is not running, then the data on the two cluster nodes will become inconsistent ("split brain"). We also need it for configuring the MySQL cluster.

2 MySQL data nodes + 1 cluster management server + 2 Load Balancers = 5

Here is my hardware configuration:

MySQL Data : DELL R300 Intel(R) Quad Core Xeon(R) CPU X3353 @ 2.66GHz, 2x SAS 146GB Drives (Raid 1), 8GB RAM

MySQL LoadBalancer : DELL R200 Intel(R) Xeon(R) CPU 3065 @ 2.33GHz, 2x SATA 250 GB Drives (Raid 1), 1GB RAM

MySQL Management : DELL R200 Intel(R) Celeron(R) CPU 430 @ 1.80GHz, 1x SATA 160 GB Drives (Raid 1), 1GB RAM

As the MySQL cluster management server does not use many resources, you can put additional load balancer on this machine or you can use it for monitoring the whole cluster by Nagios or Cacti.


2 Set Up The MySQL Cluster Management Server

First we have to download MySQL 5.1.73 (the sources version) and install the cluster management server (ndb_mgmd) and the cluster management client (ndb_mgm - it can be used to monitor what's going on in the cluster). The following steps are carried out on (

cd /usr/src
tar xvzf mysql-5.1.73.tar.gz
#Lets add proper user and group
groupadd mysql
useradd -g mysql mysql
./configure --prefix=/usr/local/mysql --enable-community-features \
--with-mysqld-user=mysql --with-mysqlmanager --with-plugins=ndbcluster
make install

This way we are wasting about 124MB space since we do not need all the actual MySQL files, but believe me it's way easier to make any cleanup/upgrade in just one dir /usr/local/mysql instead of searching for all files in /usr/bin and so on. After compiling we have two directories we are interested in /usr/local/mysql/bin and /usr/local/mysql/libexec [last one contains the ndb management exec].

Just to make life easier let's add this below to your PATH environment, to do so we have to edit file /root/.bash_profile:

echo "PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/libexec" >>/root/.bash_profile
echo "export PATH" >>/root/.bash_profile

Next, we must create the cluster configuration file, /usr/local/mysql/var/mysql-cluster/config.ini: [Hints here]

mkdir /usr/local/mysql/var/mysql-cluster
cd /usr/local/mysql/var/mysql-cluster
vi config.ini

DataMemory=80M    # How much memory to allocate for data storage
IndexMemory=18M   # How much memory to allocate for index storage
# For DataMemory and IndexMemory, we have used the
# default values. Since the "world" database takes up
# only about 500KB, this should be more than enough for
# this example Cluster setup.
# Section for the cluster management node
# IP address of the management node (this system)
# Section for the storage nodes
# IP address of the first storage node
# IP address of the second storage node

# one [MYSQLD] per storage node

Please replace the IP addresses in the file appropriately to your setup.

Then we proceed to DataSQL nodes to make necessary dirs and files setup.

/usr/local/mysql/libexec/ndb_mgmd -f /usr/local/mysql/var/mysql-cluster/config.ini

It makes sense to automatically start the management server at system boot time, so we create a very simple init script and the appropriate startup links:

echo '/usr/local/mysql/libexec/ndb_mgmd -f /usr/local/mysql/var/mysql-cluster/config.ini' > /etc/init.d/ndb_mgmd
chmod 755 /etc/init.d/ndb_mgmd
update-rc.d ndb_mgmd defaults


3 Set Up The MySQL Cluster Nodes (Storage Nodes)

Now we install mysql-5.1.73 on both and /

cd /usr/src
tar xvzf mysql-5.1.73.tar.gz
groupadd mysql
useradd -g mysql mysql
cd /usr/src/mysql-5.1.73
./configure --prefix=/usr/local/mysql --enable-community-features --with-mysqld-user=mysql --with-plugins=ndbcluster
make install
/usr/src/mysql-5.1.73/scripts/mysql_install_db --user=mysql
cd /usr/local/mysql
chown -R root:mysql .
chown -R mysql.mysql /usr/local/mysql/var

cd /usr/src/mysql-5.1.73
cp support-files/mysql.server /etc/init.d/
chmod 755 /etc/init.d/mysql.server
cd /etc/init.d
update-rc.d mysql.server defaults

Then we create the MySQL configuration file /etc/my.cnf on both nodes: /

vi /etc/my.cnf

# IP address of the cluster management node

#Those are for future tuning
#thread_concurrency = 4
# IP address of the cluster management node

Make sure to fill in the correct IP address of the MySQL cluster management server.

Lets add PATH env. to data nodes also: /

echo "PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/libexec" >>/root/.bash_profile
echo "export PATH" >>/root/.bash_profile

Next we create the data and backup directories and start the MySQL server on both cluster nodes: /

mkdir /usr/local/mysql/var/mysql-cluster
mkdir /usr/local/mysql/var/mysql-cluster/backup
cd /var/lib/mysql-cluster
/usr/local/mysql/libexec/ndbd --initial
/etc/init.d/mysql.server start

(Please note: we have to run ndbd --initial only when we start MySQL for the first time, and if /usr/local/mysql/mysql-cluster/config.ini on changes.)

Now is a good time to set a password for the MySQL root user: /

mysqladmin -u root password yourrootsqlpassword

We want to start the cluster nodes at boot time, so we create an ndbd init script and the appropriate system startup links: /

echo '/usr/local/mysql/libexec/ndbd' > /etc/init.d/ndbd
chmod 755 /etc/init.d/ndbd
update-rc.d ndbd defaults

Share this page:

4 Comment(s)

Add comment


From: Rico

je n'ai jamais réussi a faire fonctionner le cluster avec ce tutorial :

 debian1:/usr/src/mysql-5.1.35# /usr/src/mysql-5.1.35/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/
Installing MySQL system tables...
090721 13:30:36 [ERROR] /usr/local/mysql/libexec/mysqld: unknown option '--skip-bdb'
090721 13:30:36 [ERROR] Aborting

090721 13:30:36 [Warning] Forcing shutdown of 1 plugins
090721 13:30:36 [Note] /usr/local/mysql/libexec/mysqld: Shutdown complete


les options de mysql ont du changer. Avec la 5.1.35 ca ne marche plus !

Tutoriel intéressant tout de même. Merci




The problem with ndbd not found is due to not setting the PATH enviroment (which is a step in this tutorial). Since PATH may be a problem I have updated the howto with the full path to:

/usr/local/mysql/libexec/ndbd --initial

The error with --skip-bdb is easy to fix just edit /etc/mysql/my.cnf or your my.cnf file in other localisation and put a # before skip-bdb like this:

#skip-bdb. Restart MySQL and it should start without any problems.

From: Michael


 So I have folled this through and am stuck with the following block.

mkdir /usr/local/mysql/var/mysql-cluster
mkdir /usr/local/mysql/var/mysql-cluster/backup
cd /var/lib/mysql-cluster
ndbd --initial
/etc/init.d/mysql.server start 


The folder /var/lib/mysql-cluster does not exists and the command ndbd is not present in the build.  I am using version 5.1.39.

Is there something different between the two versions?  

I would apprechiate it if someone would come backt o me with an answer on how to complete the build.




From: Ron


 I am having the exact same problem, I am using version 5.1.50.  Did you every find the solution?  Can anyone please help?

I have no directory called:  /var/lib/mysql-cluster