How to create a MariaDB cluster on Debian 10?

0
50

We often use MariaDB in our tutorials. In fact, along with PostgreSQL, they are the most powerful relational database management systems out there. At least, among the community alternatives. Then, we can make hateful comparisons against payment solutions like Oracle or DB2. The point is, MariaDB has very special features that make it quite good. One of those features is the possibility to create our own database cluster using MariaDB. The reality is that it is quite simple. So in this post, I will show you how to create a MariaDB cluster on Debian 10?

The cluster environment

A cluster in informatics is understood as the union of different components of hardware or software that work together as if it were a single one. In this case, our objective will be that the databases that we make in the first node, automatically, are replicated in the second.

For it, I will use in both nodes the following configuration:

Node 1:

OS: Debian 10 Buster
Hostname: osradar1
IP address: 192.168.250.100
RAM Memory: 512 mb
Hard drive: 20gb

Node 2:

OS: Debian 10 Buster
Hostname: osradar2
IP address: 192.168.250.101
RAM Memory: 512 mb
Hard drive: 20gb

So we can get started.

Installing MariaDB Galera Cluster

To make a cluster with MariaDB it is necessary to install Galera cluster. However, from the most recent versions of MariaDB, this utility is incorporated into the main server package. Therefore, there is nothing strange to do. We will take this opportunity to install a necessary package such as rsync.

It is necessary that your user is able to execute commands with sudo, if not, read our tutorial to enable it.

Read, How to enable sudo on Debian 10?

Once you’ve done that, we can start with the installation:

:~$ sudo apt install rsync mariadb-server mariadb-client

This process has to be done in both nodes.

Configure the MariaDB cluster

Now it is necessary to configure a little the cluster so that the nodes can communicate between them. In the first node, you have to edit the file /etc/mysql/mariadb.conf.d/50-server.cnf and modify some things.

:~$ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

And add the following:

[galera]
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
wsrep_cluster_name="galera_cluster"
wsrep_node_address="db1"
wsrep_node_name="osradar1"

The following must be edited within this text file:

wsrep_cluster_address="gcomm://[NODE1_IP_ADDRESS,NODE2_IP_ADDRESS..]"

For example:

wsrep_cluster_address="gcomm://192.168.250.100,192.168.250.101"

Further down in the same file you have to edit the node configuration:

wsrep_node_address="192.168.250.100"
wsrep_node_name="osradar1"
1.-configuring a MariaDB cluster on Debian 10
1.-configuring a MariaDB cluster on Debian 10

Then save the changes and close the file.

In node2, in the same configuration file, the changes would be as follows:

wsrep_cluster_address="gcomm://192.168.250.100,192.168.250.101"

And:

wsrep_node_address="192.168.250.101"
wsrep_node_name="osradar2"

In both nodes, a root password must be defined for MariaDB. You can do this with the following command:

:~$ sudo mysql_secure_installation

If you use a firewall, then you need to open the following ports:

  • 3306/tcp
  • 4444/tcp
  • 4567/tcp
  • 4568/tcp
  • 4567/udp

This has to be done in both nodes.

Initializing and testing the cluster

Now we can initialize the cluster. To do this, stop the MariaDB service in both nodes:

:~$ sudo systemctl stop mariadb

Then, at the first node, start the cluster with the following command:

:~$ sudo galera_new_cluster

Once you start it, you won’t see a screen output. But if you check the status of the cluster, you will notice that it already recognizes the first node:

:~$ sudo mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
2.- Check the cluster status
2.- Check the cluster status

At the second node, start the MariaDB service:

:~$ sudo systemctl start mariadb

With this, the second node will be automatically added to the cluster. To check it, just execute this command in the first node:

:~$ sudo mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
3.- The second node has been added
3.- The second node has been added

And now we’ll see that this is the second node added.

To test the cluster just create a database in the first node and see how it automatically replicates in the second:

:~$ sudo mysql -u root -p
> SHOW DATABASES;
> CREATE DATABASE example;

In the second node:

:~$ sudo mysql -u root -p
> SHOW DATABASES;
4.- Testing the MariaDB cluster on Debian 10
4.- Testing the MariaDB cluster on Debian 10

This way you can easily create a MariaDB cluster on Debian 10.

Conclusion

A MariaDB cluster can be created in Debian 10, quite simply. Since the application incorporates it in the same package. Just install it and make some configurations.

Please share this post and join our Telegram channel.

LEAVE A REPLY

Please enter your comment!
Please enter your name here