oval s
How To Install a MariaDB Galera Cluster on Ubuntu 20.04
HowtoInstallaMariaDBGaleraClusteronUbuntu20 04 ded4b086ad0374dbd554bf28878310ad 2000
oval s
Table of Contents

Load balancing and clustering are key components of database system availability in a production environment. MariaDB Galera Cluster supports the storage engines XtraDB and InnoDB. It enables read and writes operations on any cluster node. Changes made to a single node are propagated to all others. Galera clusters can also be used in cloud and wide area network (WAN) systems for global clustering.

We will demonstrate to you how to create a three-node MariaDB Galera cluster on an Ubuntu 20.04 server,

Prerequisites

  • Three Ubuntu 20.04 servers
  • A root password is set up on the server.

Getting Started

Before beginning, you must upgrade your system packages to the most recent version. You can use the following command to update them:

apt-get update -y

After updating your server, you can move to the following step.

Install MariaDB Server

To begin, you must install the MariaDB server on each node. You can execute the following command to install it:

apt-get install mariadb-server -y

Once the installation is complete, start the MariaDB service and configure it to start automatically upon system reboot:

systemctl start mariadb
systemctl status mariadb

Following that, you’ll need to secure the MariaDB installation and assign each node a MariaDB root password. You can accomplish this by issuing the following command:

mysql_secure_installation

As illustrated below, you will be prompted to create a MariaDB root password.

Enter current password for root (enter for none):
Switch to unix_socket authentication [Y/n] n
Change the root password? [Y/n] Y
New password:
Re-enter new password:
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

After securing your MariaDB server, you can move to the following step.

Configure Galera Cluster

Following that, you’ll need to generate a Galera configuration file on each node to enable communication between them.

Create a galera.cnf file on the first node using the following command:

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://node1-ip-address,node2-ip-address,node3-ip-address"
Galera Synchronization Configuration
wsrep_sst_method=rsync
Galera Node Configuration
wsrep_node_address="node1-ip-address"
wsrep_node_name="node1"

When finished, save and close the file.

Create a galera.cnf file on the second node using the following command:

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://node1-ip-address,node2-ip-address,node3-ip-address"
Galera Synchronization Configuration
wsrep_sst_method=rsync
Galera Node Configuration
wsrep_node_address="node2-ip-address"
wsrep_node_name="node2"

When you’re finished, save and close the file.

On the third node, use the following command to create a galera.cnf file:

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://node1-ip-address,node2-ip-address,node3-ip-address"
Galera Synchronization Configuration
wsrep_sst_method=rsync
Galera Node Configuration
wsrep_node_address="node3-ip-address"
wsrep_node_name="node3"

Save and close the file when you are finished.

Initialize the Galera Cluster

All nodes are now set to communicate with one another.

Following that, you must terminate the MariaDB service on all nodes. To terminate the MariaDB service, run the following command:

systemctl stop mariadb

On the first node, run the following command to initialize the MariaDB Galera cluster:

galera_new_cluster

Now, use the following command to determine the cluster’s status:

mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

You should see the following output:

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

On the second node, run the following command to start the MariaDB service:

systemctl start mariadb

Following that, use the following command to determine the status of the MariaDB Galera cluster:

mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

You should see the following output:

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

On the third node, run the following command to start the MariaDB service:

systemctl start mariadb

Following that, use the following command to determine the status of the MariaDB Galera cluster:

mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

You should see the following output:

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

The MariaDB Galera cluster is now initialized. You are now ready to move on to the next phase.

Verify Cluster Replication

Following that, you’ll need to verify whether or not the replication is working.

Connect to MariaDB using the following command on the first node:

mysql -u root -p

Once connected, use the following command to create a database:

MariaDB [(none)]> create database db1;
MariaDB [(none)]> create database db2;

Following that, exit MariaDB using the following command:

MariaDB [(none)]> exit;

Then, on the second node, run the following command to connect to MariaDB:

mysql -u root -p

Following that, execute the following command to display all databases:

MariaDB [(none)]> show databases;

As you can see, both databases generated on the first node have been replicated on the second node:

+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)

Then, on the third node, run the following command to connect to MariaDB:

mysql -u root -p

Following that, execute the following command to display all databases:

MariaDB [(none)]> show databases;

As you can see, both databases generated on the first node have been replicated on the third node:

+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)

Conclusion

We learned how to set up a three-node MariaDB Galera cluster on an Ubuntu 20.04

server in the preceding guide. You may now quickly add additional nodes to the MariaDB Galera cluster. Please do not hesitate to contact me if you have any questions.

Share
Do You Enjoyed This Article?
Join our community of 3 million people and get updated every week We have a lot more just for you! Lets join us now

One Comment

  1. magnifique tutorial
    everything worked perfectly. but.
    After i reboot my servers nothing worked again.
    i did modify de file grastate.dat and started galera_new_cluster fine. but after i restart the cluster nothing worked again.
    i started the server cluster in order nod1 , 2 and 3, nothing,
    did the opposite and nothing
    started only one, nothing.
    i keep getting the Job for mariadb.service failed because the control process exited with error code.
    See “systemctl status mariadb.service” and “journalctl -xe” for details.

    and the logs what its saying is :
    systemctl status mariadb.service
    â—Ź mariadb.service – MariaDB 10.3.38 database server
    Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Fri 2023-05-19 22:39:21 UTC; 5min ago
    Docs: man:mysqld(8)
    https://mariadb.com/kb/en/library/systemd/
    Process: 6960 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 6962 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 6980 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environmen>
    Process: 7121 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION (code=exited, status=1/FAILURE)
    Main PID: 7121 (code=exited, status=1/FAILURE)
    Status: “MariaDB server is down”

    May 19 22:39:19 node1 systemd[1]: Starting MariaDB 10.3.38 database server…
    May 19 22:39:21 node1 sh[6981]: WSREP: Recovered position 54288f30-f531-11ed-8a2f-2651c619a33c:5621
    May 19 22:39:21 node1 systemd[1]: mariadb.service: Main process exited, code=exited, status=1/FAILURE
    May 19 22:39:21 node1 systemd[1]: mariadb.service: Failed with result ‘exit-code’.
    May 19 22:39:21 node1 systemd[1]: Failed to start MariaDB 10.3.38 database server.

Comments are closed.

Recent Post

Contact us to request a no-charge business process assessment!

Contact Form

Posts You Might

View Our Other Brands and Websites

MariaDB Galera Cluster

Pegas

Technology Solutions

MariaDB Galera Cluster

Pegas

Content Hub

Want to do some if it yourself? We have hundreds of follow along videos. 

MariaDB Galera Cluster

Pegas

Chat Inbox (Coming Soon)

We have built a platform that allows you to you have one live chat inbox.

MariaDB Galera Cluster

Pegas

Social (Coming Soon)

We provide our clients with a free social media manager so you don’t have to pay extra.