1.8. Replicating MariaDB with Galera
You can replicate a MariaDB database using the Galera solution to create a synchronous multi-source cluster for high availability and data consistency.
Replication itself is not a sufficient backup solution. Replication protects source servers against hardware failures, but it does not ensure protection against data loss.
1.8.1. Introduction to MariaDB Galera Cluster 링크 복사링크가 클립보드에 복사되었습니다!
MariaDB Galera Cluster provides synchronous multi-source replication that allows all nodes to be writable and ensures data consistency across the cluster.
The interface between Galera replication and a MariaDB database is defined by the write set replication API (wsrep API).
The main features of MariaDB Galera Cluster are:
- Synchronous replication
- Active-active multi-source topology
- Read and write to any cluster node
- Automatic membership control, failed nodes drop from the cluster
- Automatic node joining
- Parallel replication on row level
- Direct client connections: users can log on to the cluster nodes, and work with the nodes directly while the replication runs
Synchronous replication means that a server replicates a transaction at commit time by broadcasting the write set associated with the transaction to every node in the cluster. The client (user application) connects directly to the Database Management System (DBMS), and experiences behavior that is similar to native MariaDB.
Synchronous replication guarantees that a change that happened on one node in the cluster happens on other nodes in the cluster at the same time.
Therefore, synchronous replication has the following advantages over asynchronous replication:
- No delay in propagation of the changes between particular cluster nodes
- All cluster nodes are always consistent
- The latest changes are not lost if one of the cluster nodes crashes
- Transactions on all cluster nodes are executed in parallel
- Causality across the whole cluster
1.8.2. Components to build MariaDB Galera Cluster 링크 복사링크가 클립보드에 복사되었습니다!
Before deploying a functional, synchronously replicated MariaDB Galera Cluster, you must first install and understand the function of the core software components, specifically the MariaDB Server, the Galera Replication library, and the supporting Galera packages.
To build MariaDB Galera Cluster, you must install the following packages on your system:
-
mariadb-server-galera- contains support files and scripts for MariaDB Galera Cluster. -
mariadb-server- is patched by MariaDB upstream to include the write set replication API (wsrep API). This API provides the interface between Galera replication and MariaDB. galera- is patched by MariaDB upstream to add full support for MariaDB. Thegalerapackage contains the following:- Galera Replication Library provides the whole replication functionality.
- The Galera Arbitrator utility can be used as a cluster member that participates in voting in split-brain scenarios. However, Galera Arbitrator cannot participate in the actual replication.
-
Galera Systemd service and Galera wrapper script which are used for deploying the Galera Arbitrator utility. RHEL 10 provides the upstream version of these files, located at
/usr/lib/systemd/system/garbd.serviceand/usr/sbin/garb-systemd.
1.8.3. Deploying MariaDB Galera Cluster 링크 복사링크가 클립보드에 복사되었습니다!
You can deploy MariaDB Galera Cluster by installing the required packages, configuring cluster settings, and bootstrapping the first node to create a new cluster.
Prerequisites
- All of the nodes in the cluster have TLS set up.
All certificates on all nodes must have the
Extended Key Usagefield set to:TLS Web Server Authentication, TLS Web Client Authentication
Procedure
Install the MariaDB Galera Cluster packages:
# dnf install mariadb-server-galeraAs a result, the following packages are installed together with their dependencies:
-
mariadb-server-galera -
mariadb-server galeraFor more information about these packages required to build MariaDB Galera Cluster, see Components to build MariaDB Cluster.
-
Update the MariaDB server replication configuration before the system is added to a cluster for the first time. The default configuration is distributed in the
/etc/my.cnf.d/galera.cnffile. Before deploying MariaDB Galera Cluster, set thewsrep_cluster_addressoption in the/etc/my.cnf.d/galera.cnffile on all nodes to start with the following string:gcomm://For the initial node, it is possible to set
wsrep_cluster_addressas an empty list:wsrep_cluster_address="gcomm://"For all other nodes, set
wsrep_cluster_addressto include an address to any node which is already a part of the running cluster. For example:wsrep_cluster_address="gcomm://10.0.0.10"For more information about how to set Galera Cluster address, see Galera Cluster Address.
-
Enable the
wsrepAPI on every node by setting thewsrep_on=1option in the/etc/my.cnf.d/galera.cnfconfiguration file. Add the
wsrep_provider_optionsvariable to the Galera configuration file with the TLS keys and certificates. For example:wsrep_provider_options="socket.ssl_cert=/etc/pki/tls/certs/source.crt;socket.ssl_key=/etc/pki/tls/private/source.key;socket.ssl_ca=/etc/pki/tls/certs/ca.crt"Bootstrap a first node of a new cluster by running the following wrapper on that node:
# galera_new_clusterThis wrapper ensures that the MariaDB server daemon (
mariadbd) runs with the--wsrep-new-clusteroption. This option provides the information that there is no existing cluster to connect to. Therefore, the node creates a new UUID to identify the new cluster.참고The
mariadbservice supports a systemd method for interacting with multiple MariaDB server processes. Therefore, in cases with multiple running MariaDB servers, you can bootstrap a specific instance by specifying the instance name as a suffix:# galera_new_cluster mariadb@node1Connect other nodes to the cluster by running the following command on each of the nodes:
# systemctl start mariadbAs a result, the node connects to the cluster, and synchronizes itself with the state of the cluster.
Verification
1.8.4. Checking the status of a MariaDB Galera cluster 링크 복사링크가 클립보드에 복사되었습니다!
It is important to monitor and ensure the health, performance, and synchronization of a MariaDB Galera cluster. For that, you can query status variables on each node to monitor the node and the cluster.
To check the status of a MariaDB Galera cluster, you can use the following queries:
Display the number of nodes in the cluster:
# mysql -u root -p -e 'show status like "wsrep_cluster_size";' +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 4 | +--------------------+-------+Display the node’s cluster component status:
# mysql -u root -p -e 'show status like "wsrep_cluster_status";' +----------------------+---------+ | Variable_name | Value | +----------------------+---------+ | wsrep_cluster_status | Primary | +----------------------+---------+The value of the
wsrep_cluster_statusvariable indicates the status of the cluster component the current node belongs to. Possible values are:-
Primary: The cluster is functioning normally. A quorum is present. In a healthy cluster, all nodes reportPrimary. -
Non-primary: The node has lost the connection to the primary component of the cluster and is no longer part of the active cluster. However, the node still can serve read queries but cannot process write operations. -
Disconnected: The node is not connected to any cluster component. Consequently, it cannot accept queries and is not replicating any data.
-
Display the node’s status:
# mysql -u root -p -e 'show status like "wsrep_local_state_comment";' +---------------------------+--------+ | Variable_name | Value | +---------------------------+--------+ | wsrep_local_state_comment | Synced | +---------------------------+--------+The following are frequent values of the
wsrep_local_state_commentvariable:-
Synced: The node is fully synchronized within the cluster and actively participating in replication. -
Desynced: The node is still part of the cluster but it is primarily busy with the state transfer. -
Joining: The node is in the process of joining a cluster. -
Joined: The node has successfully joined a cluster. It can receive and apply write sets from the cluster. -
Donor: The node currently provides a State Snapshot Transfer (SST) to a joining node. When a new node joins and requires a full state transfer, the cluster selects an existing node to send the necessary data.
-
Check whether the node accepts write sets from the cluster:
# mysql -u root -p -e 'show status like "wsrep_ready";' +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wsrep_ready | ON | +---------------+-------+When the
wsrep_readyvariable isON, the node has successfully initialized its components and is connected to a cluster. Additionally, the node is synchronized or has reached a state where it can serve queries.Check whether the node has network connectivity with other hosts:
# mysql -u root -p -e 'show status like "wsrep_connected";' +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | wsrep_connected | ON | +-----------------+-------+The
ONvalue means that node has connectivity to at least one member in the cluster.Display the average size of the local received queue for write sets since the last
FLUSH STATUScommand or since the server started:# mysql -u root -p -e 'show status like "wsrep_local_recv_queue_avg";' +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | wsrep_local_recv_queue_avg | 0.012 | +----------------------------+-------+A value near 0 is the ideal state and indicates that the node continues applying write sets as they are received. A persistently high or growing value can be an indicator of performance bottlenecks, such as slow disk I/O.
Display the flow control status:
# mysql -u root -p -e 'show status like "wsrep_flow_control_paused";' +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | wsrep_flow_control_paused | 0 | +---------------------------+-------+This variable represents the fraction of time a node has been paused and is unable to process new incoming transactions because its local receive queue was too full, triggering flow control. A value close to 0 indicates the node continues with the replication workload efficiently. A value approaching 1.0 means that the node frequently or constantly encounters difficulty in applying write sets and can be a bottleneck for the cluster.
If the node is frequently pausing, you can adjust the
wsrep_slave_threadsparameter in the/etc/my.cnf.d/galera.cnffile.Display the average distance between the lowest and highest sequence numbers the node can apply in parallel:
# mysql -u root -p -e 'show status like "wsrep_cert_deps_distance";' +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | wsrep_cert_deps_distance | 1 | +--------------------------+-------+A higher value indicates a greater degree of parallelism. It is the optimal value you can use in the
wsrep_slave_threadsparameter in the/etc/my.cnf.d/galera.cnffile.
1.8.5. Adding a new node to MariaDB Galera Cluster 링크 복사링크가 클립보드에 복사되었습니다!
You can add a new node to your MariaDB Galera Cluster or reconnect an existing one by configuring the cluster address in the node’s configuration file.
Procedure
On the particular node, provide an address to one or more existing cluster members in the
wsrep_cluster_addressoption within the[mariadb]section of the/etc/my.cnf.d/galera.cnfconfiguration file :[mariadb] wsrep_cluster_address="gcomm://192.168.0.1"When a new node connects to one of the existing cluster nodes, it is able to see all nodes in the cluster.
However, preferably list all nodes of the cluster in
wsrep_cluster_address.As a result, any node can join a cluster by connecting to any other cluster node, even if one or more cluster nodes are down. When all members agree on the membership, the cluster’s state is changed. If the new node’s state is different from the state of the cluster, the new node requests either an Incremental State Transfer (IST) or a State Snapshot Transfer (SST) to ensure consistency with the other nodes.
1.8.6. Restarting MariaDB Galera Cluster 링크 복사링크가 클립보드에 복사되었습니다!
If you shut down all nodes at the same time, you stop the cluster, and the running cluster no longer exists. However, the cluster’s data still exist.
To restart the cluster, bootstrap a first node as described in Deploying MariaDB Galera Cluster
If the cluster is not bootstrapped, and mariadb on the first node is started with only the systemctl start mariadb command, the node tries to connect to at least one of the nodes listed in the wsrep_cluster_address option in the /etc/my.cnf.d/galera.cnf file. If no nodes are currently running, the restart fails.