There is a new version of this tutorial available for Ubuntu 20.04 (Focal Fossa).

How To Install MariaDB Galera Cluster on Ubuntu 16.04

MariaDB Galera is a multi-master cluster for MariaDB. Since MariaDB 10.1, the MariaDB Server and MariaDB Galera Server packages have been combined and Galera packages and their dependencies get installed automatically when installing MariaDB. Currently, MariaDB Galera Cluster only supports the InnoDB/XtraDB storage engine. In MariaDB 10.0 and 5.5, you will need to download it separately. MariaDB Galera adds redundancy for a site's database. In MariaDB Galera Cluster, multiple database servers interconnected with each other and keep synchronized.

MariaDB Galera provides lots of features, some of them are listed below:

  1. Synchronous replication.
  2. Automatic node joining.
  3. Active-active multi-master topology.
  4. True parallel replication, on row level.
  5. Read and write scalability, Smaller client latencies.
  6. Failed nodes, automatic drop from the cluster.

In this tutorial, we will explain how to setup MariaDB Galera Cluster 10.1 with 3 nodes running on Ubuntu 16.04 server. We will also test database replication between all the nodes.

Requirements

  • Three Nodes running Ubuntu 16.04 server.
  • Node1 with static IP address 192.168.0.102, Node2 with static IP address 192.168.0.103 and Node3 with static IP address 192.168.0.104 configure on your server.
  • Non-root user with sudo privileges setup on all the nodes.

Getting Started

First, you will need to update all the nodes with the latest version. You can update all of them with the following command:

sudo apt-get update -y
sudo apt-get upgrade -y

Next, restart all the nodes to apply these changes. Then, log in with sudo user and proceed to the next step.

Install MariaDB Galera

MariaDB Server and MariaDB Galera Server packages are combined in the version MariaDB 10.1. By default, MariaDB 10.1 is not available in the default Ubuntu repositories, so you will need to add the MariaDB repository on all the nodes.

First, add the MariaDB repository key with the following command:

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8

Next, add the repository and update the APT cache with the following command:

sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://ftp.utexas.edu/mariadb/repo/10.1/ubuntu xenial main'
sudo apt-get update -y

Once the repository is updated, install MariaDB with the following command:

sudo apt-get install mariadb-server rsync -y

The above command will install MariaDB with Galera and several dependencies. The Galera parts remain dormant until configured, like a plugin or storage engine. Once the MariaDB is installed on all the nodes, you can proceed to secure MariaDB.

By default MariaDB installation is not secured, so you will need to secure MariaDB installation. You can do this by running the mysql_secure_installation script:

sudo mysql_secure_installation

In this process, you will be asked to set root password, remove anonymous users, disallow root login remotely and remove test database. Answer all the questions as shown below:

Enter current password for root (enter for none):
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Once the MariaDB is secured on all the nodes, you can proceed to the next step.

Setup MariaDB Cluster on Node1

First, go to the Node1 and create a configuration file for Galera. By default, MariaDB reads configuration from /etc/mysql/conf.d/ directory. To do so, run the following command:

sudo nano /etc/mysql/conf.d/galera.cnf

Add the following lines:

[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://192.168.0.102,192.168.0.103,192.168.0.104"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.102"
wsrep_node_name="Node1"

Save the file when you are finished.

Note: 192.168.0.102 is the IP address of the Node1

Add Node2 on Galera Cluster

Next, go to the Node2 and create a configuration file for Galera:

sudo nano /etc/mysql/conf.d/galera.cnf

Add the following lines:

[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://192.168.0.102,192.168.0.103,192.168.0.104"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.103"
wsrep_node_name="Node2"

Save the file when you are finished.

Note: 192.168.0.103 is the IP address of the Node2.

Add Node3 on Galera Cluster

Next, go to the Node3 and create a configuration file for Galera:

sudo nano /etc/mysql/conf.d/galera.cnf

Add the following lines:

[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://192.168.0.102,192.168.0.103,192.168.0.104"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.104"
wsrep_node_name="Node3"

Save the file when you are finished.

Note: 192.168.0.104 is the IP address of the Node3.

Configure Firewall

Galera Cluster uses four ports 3306 for MySQL client connection, 4444 for State Snapshot Transfer, 4567 for Galera Cluster replication traffic and 4568 for Incremental State Transfer. So you will need to allow all these ports using the UFW firewall. You can do this by running the following command on all the nodes:

First, enable the UFW firewall with the following command:

sudo ufw enable

Next, allow all the ports with the following command:

sudo ufw allow 3306/tcp
sudo ufw allow 4444/tcp
sudo ufw allow 4567/tcp
sudo ufw allow 4568/tcp
sudo ufw allow 4567/udp

You can then check the status of the firewall with the following command:

sudo ufw status

Once the UFW firewall is configured on all the nodes, you can proceed to the next step.

Start MariaDB Galera Cluster

After successfully configuring all the nodes, go to the Node1 and start Galera Cluster.

Before Galera can start, you need to ensure that MariaDB service is stopped on all the nodes.

Run the following command on all the nodes:

sudo systemctl stop mysql

Now, start the Galera Cluster on Node1 with the following command:

sudo galera_new_cluster

Now, check whether the cluster is running or not with the following command:

mysql -u root -p -e "show status like 'wsrep_cluster_size'"

If everything is fine you should see the following output:

+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 1     |
+--------------------+-------+

On the Node2, start the MariaDB service:

sudo systemctl start mysql

You can check the status of MariaDB service whether it is working or not with the following command:

sudo systemctl status mysql

If everything is fine, you should see the following output:

?? mariadb.service - MariaDB database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2017-09-17 10:11:20 EDT; 10min ago
  Process: 715 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 713 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
  Process: 545 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set
  Process: 535 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 514 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
 Main PID: 661 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 26 (limit: 4915)
   CGroup: /system.slice/mariadb.service
           ??????661 /usr/sbin/mysqld

Sep 17 10:11:11 debian systemd[1]: Starting MariaDB database server...
Sep 17 10:11:15 debian mysqld[661]: 2017-09-17 10:11:15 140287134630464 [Note] /usr/sbin/mysqld (mysqld 10.1.26-MariaDB-0+deb9u1) starting as process
Sep 17 10:11:20 debian systemd[1]: Started MariaDB database server.

Now, your second node should've automatically linked to the cluster. You can verify that with the following command:

mysql -u root -p -e "show status like 'wsrep_cluster_size'"

If everything is working well, the cluster size should be set to two:

+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 2     |
+--------------------+-------+

On the Node3, start the MariaDB service:

sudo systemctl start mysql
mysql -u root -p -e "show status like 'wsrep_cluster_size'"

If everything is working well, the cluster size should be set to three:

+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+

You cluster is now working and communicating each other.

Test Database Replication

Now, all the nodes are online, it's time to test database replication across the Galera Cluster. Let's start by creating a database on Node1 and check whether it is replicated on all the nodes.

First, login to MariaDb console with the following command:

mysql -u root -p

Enter your root password and create a database with name test_db:

MariaDB [(none)]> create database test_db;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| test_db            |
+--------------------+

Now go to the Node2 and Node3, then check replication is working or not:

mysql -u root -p
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| test_db            |
+--------------------+

Congratulations! you have successfully installed and configured MariaDB Galera Cluster on Ubuntu 16.04 server.

Conclusion

I hope you have now enough knowledge to install and configure MariaDB Galera Cluster on Ubuntu 16.04. You can now easily scaled up to several, or even dozens, of distinct nodes. If you have any doubt or more query, then refer the link Galera Cluster Doc

Share this page:

7 Comment(s)