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.