Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / MySQL

MariaDB Galera Replication Cluster

4.18/5 (3 votes)
10 Jun 2014CPOL2 min read 13.8K  
A very simple replication cluster with MariaDB

Introduction

High availability is crucial for E-business, web services and others. With this simple replication cluster, we do a very useful DB server.

Installing MariaDB and Galera

A MariaDB replication cluster needs two or more nodes/servers, we'll work with Ubuntu server 12.04 as operating system for the nodes. In this case, I'll setup the replication cluster with 3 nodes (Server1, Server2 and Server3), each one has one network interface with the following IP address respectively: 192.168.198.130, 192.168.198.131, and 192.168.198.132.

Ubuntu 12.04 doesn't contains the MariaDB repositories so we have to add them manually and import the key for the repository.

apt-get install python-software-properties
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
add-apt-repository ’deb http://mirror3.layerjet.com/mariadb/repo/5.5/ubuntu precise main’
apt-get update

Then, install the following packages:

  • rsync
  • galera
  • mariadb-galera-server
sudo apt-get install rsync mariadb-galera-server galera

Configuring Galera

Create a configuration file for galera cluster is needed. It contains configuration for both MariaDB and Galera (Galera configuration variables start with wsrep_). In this file, the wsrep_cluster_address variable will store the IP addresses of each node allowed to join to the cluster.

The file should be created at /etc/mysql/conf.d/ and should be named as galera.cnf. Copy this file to all nodes in the cluster.

#/etc/mysql/conf.d/galera.cnf
[mysqld]
#mysql settings
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_size=0
query_cache_type=0
bind-address=0.0.0.0
#galera settings
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_name="cluster_db"
wsrep_cluster_address="gcomm://192.168.198.130,192.168.198.131,192.168.198.132"
wsrep_sst_method=rsync

wsrep_sst_method variable sets the synchronization method between all the nodes, rsync is the most simple method and dosen't need credentials.

Choose one node and open the file in /etc/mysql/debian.cnf, the file should be like this:

[client]
host = localhost
user = debian-sys-maint
password = 03P8rdlknkXr1upf
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = 03P8rdlknkXr1upf
socket = /var/run/mysqld/mysqld.sock
basedir = /usr

Copy it (in the same location) to all nodes in the cluster, after that stop mysql service in each node.

Server1#: sudo service mysql stop
Server2#: sudo service mysql stop
Server3#: sudo service mysql stop

Running Galera

Start the mysql service in one node and add the arg for a new cluster:

Server1#: service mysql start --wsrep-new-cluster

The new cluster should be running with one node in it, check the wsrep_cluster_size variable with the following query:

Server1#: mysql -u root -e ’SELECT VARIABLE_VALUE as "cluster size" 
FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE VARIABLE_NAME="wsrep_cluster_size"’

+--------------+
| cluster size |
+--------------+
| 1            |
+--------------+

In the other nodes, just start normally the mysql service:

Server2#: service mysql start
Server3#: service mysql start

You can check the size of the cluster on any node with the SQL query mentioned a few lines above.

Points of Interest

MariaDB galera cluster it is currently available on Linux only.

The library in wsrep_provider is installed by the package galera.

More information is available at https://mariadb.com/kb/en/what-is-mariadb-galera-cluster/.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)