How to setup MariaDB Galera Cluster 10.0 on CentOS

Introduction

MariaDB is a drop-in replacement for MySQL. MariaDB Galera Cluster is a synchronous multi-master cluster for MariaDB. It is available on Linux only, and only supports the XtraDB/InnoDB storage engines.

This blog post explains step by step how to setup MariaDB Galera Cluster 10.0 with 3 nodes running on CentOS 6.5 x86_64 resulting in a HA (high-availability) database cluster.

Cluster details

I’m using 3 freshly deployed VMs running a minimal install of CentOS 6.5 x86_64.

Cluster node 1 has hostname db1 and IP address 10.0.0.9
Cluster node 2 has hostname db2 and IP address 10.0.0.11
Cluster node 3 has hostname db3 and IP address 10.0.0.13

Step 1 – set SELinux in permissive mode

It’s currently not possible to have SELinux in enforcing mode because there are no policies (yet) for MariaDB Galera Cluster 10.0. I have filed BZ1117233. Hopefully the SELinux developers will soon add proper policies. For now put SELinux into permissive mode on all nodes:

Update: SELinux policies for MariaDB Galera 10 will be in RHEL 6.6 and CentOS 6.6.

Step 2 – configure the firewall

MariaDB Galera Cluster on each cluster node must be able to talk to MariaDB Galera Cluster on the other nodes so we need to open some firewall ports. Execute the following commands on all nodes:

Please not that these rules allow any host on your local IP network to access the database nodes. For production make it more secure by specifying the source IP range from which connections to the database nodes are accepted.

Step 3 – install the MariaDB repository

I’m using the MariaDB Galera Cluster RPM packages from mariadb.org. Add the MariaDB RPM repository by executing the following command on all nodes:

Step 4 – install MariaDB Galera Cluster 10.0 software

If you did a CentOS 6 minimal installation then make sure you install the socat package from the EPEL repository before proceeding with installing the MariaDB Galera Cluster 10.0 software. You can find the socat package at http://dl.fedoraproject.org/pub/epel/6/x86_64/repoview/socat.html.
Either install it directly from EPEL with the following command (for x86_64):

Or install the EPEL repository and then install it the regular way:

On CentOS 7 the socat package is part of the distro and is installed as part of the minimal install so should already be there. If it’s not then just install it with:

Install the MariaDB Galera Cluster 10.0 software by executing the following command on all nodes:

Step 5 – setup MariaDB security

The default installation is not very secure so let’s improve that. Start the mysql service (yes the init script in MariaDB 10.0 is still called mysql) on all nodes:

Next we need to run the mysql_secure_installation script so we can improve the security. Execute the following command on all nodes:

For this tutorial I changed the password to ‘dbpass’ and accepted all defaults (so answered yes to all questions).

Step 6 – setup MariaDB Galera Cluster users

Now we need to setup some users that must be able to access the database. The ‘sst_user’ is the user which a database node will use for authenticating to another database node in the State Transfer Snapshot (SST) phase. Execute the following command on all nodes:

Please not that the ‘%’ means that the root or sst_user is allowed to access the database from any host. For production make it more secure by specifying the hostname(s) or IP addresses from which those users can access the database.

Step 7 – create the MariaDB Galera Cluster config

First stop the mysql services on all nodes:

Now let’s create the MariaDB Galera Cluster configuration by executing the following command on all nodes (read the IMPORTANT NOTE after the config about required changes for db2, and db3):

IMPORTANT NOTE: when executing this command on db2 and db3 do not forget to adjust the wsrep_node_address and wsrep_node_name variables.

On db2 they should be:

On db3 they should be:

Step 8 – initialize the first cluster node

On node db1 only start MariaDB with the special ‘‐‐wsrep-new-cluster’ option so the primary node of the cluster is initialized:

Check if everything went ok by executing the following command on db1 only:

And the output:

The important information are the following lines:

There is also a lot of information in the logfile which is located in /var/lib/mysql and it’s called db1.<FQDN>.err. Here are the last couple of lines after initializing db1:

Step 9 – add the other cluster nodes

Make 100% sure that both nodes db2 and db3 have the correct configuration in /etc/my.cnf.d/server.cnf under the [mariadb-10.0] heading as described in step 7.

With the correct configuration in place, all that is required to make db2 and db3 a member of the cluster is to start them like you would start any regular service. On db2 issue the following command:

On node db2 check /var/lib/mysql/db2.<FQDN>.err and you should see:

And on node db1 check /var/lib/mysql/db1.<FQDN>.err and you should see:

Check what has changed in the cluster status by executing the following command on db1 or db2:

And you should see that node db2 is now known as the cluster size is ‘2’ and the IP address of node db2 is listed:

Repeat the same step for node db3. On node db3 only execute the following command:

On node db3 check /var/lib/mysql/db3.<FQDN>.err and you should see something like this:

Check what has changed in the cluster status by executing the following command on for example db1:

And you should see that node db3 is now known as the cluster size is ‘3’ and the IP address of node db3 is listed:

Step 10 – verify replication

The cluster is now up and running. Let’s check if it’s actually working. On db1 create a database ‘clustertest’ by issuing the following command:

Check if the database, table and data exists:

That looks good. Now do the check on node db2 to see if it good replicated:

Everything was successfully replicated from db1 to db2. Now do the same check on node db3:

As you can see everything was successfully replicated by node db1 across all other nodes.

Step 11 – starting a cluster that is completely down

If the entire cluster (each node) is down then this is how you start the cluster.

First decide which node you consider the primary node. Normally that would be the node with the database which you consider (the most) correct. Then start that node with the following command. Note that the command will not touch or delete data. It will just start the first node as the primary cluster node.

Once the primary node is successfully started you can start the other nodes in the cluster with the regular command:

And it’s probably a good idea to check if the cluster is fully operational by looking on all cluster nodes at the output of this command:

Step 12 – MariaDB Galera limitations

MariaDB Galera Cluster software has some known limitations. Make sure to read them.

Severalnines has an excellent explanation what those limitations mean in practical terms and how they can be dealt with in their “Avoiding Deadlocks in Galera…” article.

A workaround to prevent the rollbacks and deadlocks is to use a single node for high concurrency writes and send updates to only that node while using multiple nodes for reads. This can be realized by adding HAProxy & Keepalived to the setup described in this tutorial and is also available on this blog.