(Last Updated On: August 14, 2019)

In today’s guide, I’ll walk you through the steps needed to Setup MariaDB Galera Cluster on Debian 10 (Buster). For guys running serious production workload in MariaDB, high availability of database server necessitates the deployment of Galera cluster.

MariaDB Galera Cluster is a synchronous multi-master cluster for MariaDB with support for XtraDB/InnoDB storage engines. It has the following top features.

  • It provides active-active multi-master topology
  • You can read and write to any cluster node
  • It has an automatic node joining
  • Automatic membership control, failed nodes drop from the cluster
  • Has true parallel replication, on row level
  • Direct client connections

We assume you have database servers ready, and you’re logged into the servers as a user with sudo privileges. Once you satisfy these basic requirements, proceed with the installation of MariaDB Galera Cluster on Debian 10 (Buster).

My setup is based on below details.

ServerHostnameIP Address
DB 1db1.computingforgeeks.com10.0.0.2
DB 2db2.computingforgeeks.com10.0.0.3
DB3db3.computingforgeeks.com10.0.0.4

Step 1: Update all servers

Let’s ensure all our servers are updated.

sudo apt update && sudo apt -y upgrade
sudo reboot

Step 2: Set Hostnames and DNS

Let’s configure proper hostames in our servers.

# DB1
sudo hostnamectl set-hostname db1.computingforgeeks.com --static

# DB2
sudo hostnamectl set-hostname db2.computingforgeeks.com --static

# DB3
sudo hostnamectl set-hostname db3.computingforgeeks.com --static

If you have DNS server, configure A record DNS entries for all machines. For basic setups, edit /etc/hosts file on each machine and populate required DNS entires.

sudo tee -a /etc/hosts<<EOF
10.0.0.2 db1.computingforgeeks.com db1
10.0.0.3 db2.computingforgeeks.com db2
10.0.0.4 db3.computingforgeeks.com db3
EOF

Try ping DNS name from one server to another.

# DB1
$ ping -c 1 db2
$ ping -c 1 db3

Step 3: Install MariaDB on all nodes

The version of MariaDB available on Debian 10 repositories is 10.3. If you want to go with MariaDB 10.4, install it from MariaDB repositories.

sudo apt update
sudo apt -y install mariadb-server mariadb-client

Secure each database server by running the initial configuration script.

$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Test by connecting to MariaDB as root user.

# Without password

$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

# With Authentication

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 59
Server version: 10.3.15-MariaDB-1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

Step 4: Configure MariaDB Galera Cluster

MariaDB service binds to 127.0.0.1 IP address by default. Comment out the bind line on the configuration file /etc/mysql/mariadb.conf.d/50-server.cnf.

$ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
#bind-address            = 127.0.0.1

Configure First Cluster node – DB1

Edit MariaDB configuration file and add below content to the end of the file.

$ sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf

[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"

Inititialize Galera cluster:

sudo galera_new_cluster
sudo systemctl restart mariadb 

Configure other database nodes

Configure DB2

$ sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf

[galera]
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Specify cluster nodes
wsrep_cluster_address="gcomm://db1,db2,db3"
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="db2"

Restart mariadb:

sudo systemctl restart mariadb

Configure DB3

$ sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf

[galera]
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Specify cluster nodes
wsrep_cluster_address="gcomm://db1,db2,db3"
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="db3"

Restart MariaDB

systemctl restart mariadb

Step 5: Confirm MariaDB Galera Cluster Settings

Login to MySQL console from a node in the cluster as root user.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.3.15-MariaDB-1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

Confirm if Cluster settings are OK.

$ MariaDB [(none)]> show status like 'wsrep_%'; 
+------------------------------+--------------------------------------+
| Variable_name                | Value                                |
+------------------------------+--------------------------------------+
| wsrep_apply_oooe             | 0.000000                             |
| wsrep_apply_oool             | 0.000000                             |
| wsrep_apply_window           | 0.000000                             |
| wsrep_causal_reads           | 0                                    |
| wsrep_cert_deps_distance     | 0.000000                             |
| wsrep_cert_index_size        | 0                                    |
| wsrep_cert_interval          | 0.000000                             |
| wsrep_cluster_conf_id        | 3                                    |
| wsrep_cluster_size           | 3                                    |
| wsrep_cluster_state_uuid     | 0f6dbe29-bec4-11e9-a243-eb8c0dc70c76 |
| wsrep_cluster_status         | Primary                              |
| wsrep_cluster_weight         | 3                                    |
| wsrep_commit_oooe            | 0.000000                             |
| wsrep_commit_oool            | 0.000000                             |
| wsrep_commit_window          | 0.000000                             |
| wsrep_connected              | ON                                   |
| wsrep_desync_count           | 0                                    |
| wsrep_evs_delayed            |                                      |
| wsrep_evs_evict_list         |                                      |
| wsrep_evs_repl_latency       | 0/0/0/0/0                            |
| wsrep_evs_state              | OPERATIONAL                          |
| wsrep_flow_control_paused    | 0.000000                             |
| wsrep_flow_control_paused_ns | 0                                    |
| wsrep_flow_control_recv      | 0                                    |
| wsrep_flow_control_sent      | 0                                    |
| wsrep_gcomm_uuid             | 0f6d51e5-bec4-11e9-bd50-52974fa5f2b4 |
| wsrep_incoming_addresses     | db1:3306,db2:3306,db3:3306           |
| wsrep_last_committed         | 0                                    |
| wsrep_local_bf_aborts        | 0                                    |
| wsrep_local_cached_downto    | 18446744073709551615                 |
| wsrep_local_cert_failures    | 0                                    |
| wsrep_local_commits          | 0                                    |
| wsrep_local_index            | 0                                    |
| wsrep_local_recv_queue       | 0                                    |
| wsrep_local_recv_queue_avg   | 0.100000                             |
| wsrep_local_recv_queue_max   | 2                                    |
| wsrep_local_recv_queue_min   | 0                                    |
| wsrep_local_replays          | 0                                    |
| wsrep_local_send_queue       | 0                                    |
| wsrep_local_send_queue_avg   | 0.000000                             |
| wsrep_local_send_queue_max   | 1                                    |
| wsrep_local_send_queue_min   | 0                                    |
| wsrep_local_state            | 4                                    |
| wsrep_local_state_comment    | Synced                               |
| wsrep_local_state_uuid       | 0f6dbe29-bec4-11e9-a243-eb8c0dc70c76 |
| wsrep_open_connections       | 0                                    |
| wsrep_open_transactions      | 0                                    |
| wsrep_protocol_version       | 9                                    |
| wsrep_provider_name          | Galera                               |
| wsrep_provider_vendor        | Codership Oy <[email protected]>    |
| wsrep_provider_version       | 3.25(rddf9876)                       |
| wsrep_ready                  | ON                                   |
| wsrep_received               | 10                                   |
| wsrep_received_bytes         | 790                                  |
| wsrep_repl_data_bytes        | 0                                    |
| wsrep_repl_keys              | 0                                    |
| wsrep_repl_keys_bytes        | 0                                    |
| wsrep_repl_other_bytes       | 0                                    |
| wsrep_replicated             | 0                                    |
| wsrep_replicated_bytes       | 0                                    |
| wsrep_thread_count           | 2                                    |
+------------------------------+--------------------------------------+
61 rows in set (0.002 sec)

We can confirm the cluster size of 3 – The three nodes we have in the cluster.

wsrep_cluster_size    3

Let’s create a test database on db1 and confirm status on other nodes.

[email protected]:~# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.3.15-MariaDB-1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE test_db;
Query OK, 1 row affected (0.012 sec)

[email protected]:~# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 39
Server version: 10.3.15-MariaDB-1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
4 rows in set (0.002 sec)

[email protected]:~# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.3.15-MariaDB-1 Debian 10

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
4 rows in set (0.001 sec)

Check Galera Cluster commands to help you monitor the cluster.

Step 6: Configure Galera Cluster Load Balancing with HAProxy

Configure HAproxy to ensure your Database service is always available. With this, you can easily isolate nodes for maintenance purposes.

Galera Cluster High Availability With HAProxy

Recommended MySQL/SQL Study books:

  • Getting Started With SQL – A Hands-On Approach for Beginners – a simple, to-the-point introductory read that’ll touch on the practical implications of SQL. Here, a reader gets introduced concisely to all the basics of the language;
  • Head First SQL – Your Brain on SQL – A Learner’s Guide;
  • SQL Cookbook: Query Solutions and Database Techniques for Database Developers – a book is full of hacks and tips that can be applied in day-to-day database management;
  • Teach Yourself MS SQL Server – a fairly old book, yet, it covers all the aspects of SQL Server on a high level;
  • Effective SQL – an easy-to-read guide book that explores SQL features. Keep in mind that you might need some SQL knowledge to apply the ideas that have been laid out.