Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 2. Using MariaDB
MariaDB Server is a high-performance, open source relational database management system (RDBMS). Built on the MySQL technology, it offers a powerful SQL interface for data access and includes advanced features, such as support for multiple storage engines.
Learn how to install and configure MariaDB on a RHEL system, how to back up MariaDB data, how to migrate from an earlier MariaDB version, and how to replicate a database using the MariaDB Galera Cluster.
2.1. Installing MariaDB Link kopierenLink in die Zwischenablage kopiert!
RHEL 9 provides MariaDB 10.5 as the initial version of the Application Stream, which you can install as an RPM package. Additional MariaDB versions are provided as modules with a shorter life cycle in minor releases of RHEL 9.
In RHEL 9, the MariaDB server is available in the following versions, each provided by a separate stream:
- MariaDB 10.5
- MariaDB 10.11 - available since RHEL 9.4
By design, you can install only one version (stream) of the same module and, because of conflicting RPM packages, you cannot install MariaDB and MySQL on the same host. As an alternative, you can run the database server services in a container. See Using containers to run multiple MariaDB and MySQL instances on a single host.
Procedure
Install MariaDB server packages:
For MariaDB 10.5 from the RPM package:
dnf install mariadb-server
# dnf install mariadb-serverCopy to Clipboard Copied! Toggle word wrap Toggle overflow For MariaDB 10.11 by selecting stream (version)
11from themariadbmodule and specifying the server profile, for example:dnf module install mariadb:10.11/server
# dnf module install mariadb:10.11/serverCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Enable and start the
mariadbservice:systemctl enable --now mariadb.service
# systemctl enable --now mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2. Using containers to run multiple MariaDB and MySQL instances on a single host Link kopierenLink in die Zwischenablage kopiert!
If you install MariaDB or MySQL from packages, you can only run one of these services and only a single version of it on the same host. As an alternative, you can run the services in a container.
You can configure the following scenarios:
- You want to run multiple instances of MariaDB or MySQL on the same host.
- You want to run both MariaDB and MySQL on the same host.
Prerequisites
-
The
container-toolsmeta-package is installed.
Procedure
Use your Red Hat Customer Portal account to authenticate to the
registry.redhat.ioregistry:podman login registry.redhat.io
# podman login registry.redhat.ioCopy to Clipboard Copied! Toggle word wrap Toggle overflow Skip this step if you are already logged in to the container registry.
Run MySQL 8.0 in a container:
podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mysql_root_password> -p <host_port_1>:3306 rhel9/mysql-80
$ podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mysql_root_password> -p <host_port_1>:3306 rhel9/mysql-80Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about the usage of this container image, see the Red Hat Ecosystem Catalog.
Run MySQL 8.4 in a container:
podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mysql_root_password> -p <host_port_2>:3306 rhel9/mysql-84
$ podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mysql_root_password> -p <host_port_2>:3306 rhel9/mysql-84Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about the usage of this container image, see the Red Hat Ecosystem Catalog.
Run MariaDB 10.5 in a container:
podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mariadb_root_password> -p <host_port_3>:3306 rhel9/mariadb-105
$ podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mariadb_root_password> -p <host_port_3>:3306 rhel9/mariadb-105Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about the usage of this container image, see the Red Hat Ecosystem Catalog.
Run MariaDB 10.11 in a container:
podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mariadb_root_password> -p <host_port_4>:3306 rhel9/mariadb-1011
$ podman run -d --name <container_name> -e MYSQL_ROOT_PASSWORD=<mariadb_root_password> -p <host_port_4>:3306 rhel9/mariadb-1011Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about the usage of this container image, see the Red Hat Ecosystem Catalog.
NoteThe container names and host ports of the two database servers must differ.
To ensure that clients can access the database server on the network, open the host ports in the firewall:
firewall-cmd --permanent --add-port={<host_port_1>/tcp,<host_port_2>/tcp,<host_port_3>/tcp,<host_port_4>/tcp,...} firewall-cmd --reload# firewall-cmd --permanent --add-port={<host_port_1>/tcp,<host_port_2>/tcp,<host_port_3>/tcp,<host_port_4>/tcp,...} # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Display information about running containers:
podman ps
$ podman psCopy to Clipboard Copied! Toggle word wrap Toggle overflow Connect to the database server and log in as root:
mysql -u root -p -h localhost -P <host_port> --protocol tcp
# mysql -u root -p -h localhost -P <host_port> --protocol tcpCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.3. Configuring MariaDB Link kopierenLink in die Zwischenablage kopiert!
To configure the MariaDB server for networking, use the following procedure.
Procedure
Edit the
[mysqld]section of the/etc/my.cnf.d/mariadb-server.cnffile. You can set the following configuration directives:bind-address- is the address on which the server listens. Possible options are:- a host name
- an IPv4 address
- an IPv6 address
skip-networking- controls whether the server listens for TCP/IP connections. Possible values are:- 0 - to listen for all clients
- 1 - to listen for local clients only
-
port- the port on which MariaDB listens for TCP/IP connections.
Restart the
mariadbservice:systemctl restart mariadb.service
# systemctl restart mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.4. Setting up TLS encryption on a MariaDB server Link kopierenLink in die Zwischenablage kopiert!
By default, MariaDB uses unencrypted connections. For secure connections, enable TLS support on the MariaDB server and configure your clients to establish encrypted connections.
2.4.1. Placing the CA certificate, server certificate, and private key on the MariaDB server Link kopierenLink in die Zwischenablage kopiert!
Before you can enable TLS encryption in the MariaDB server, store the certificate authority (CA) certificate, the server certificate, and the private key on the MariaDB server.
Prerequisites
The following files in Privacy Enhanced Mail (PEM) format have been copied to the server:
-
The private key of the server:
server.example.com.key.pem -
The server certificate:
server.example.com.crt.pem -
The Certificate Authority (CA) certificate:
ca.crt.pem
For details about creating a private key and certificate signing request (CSR), as well as about requesting a certificate from a CA, see your CA’s documentation.
-
The private key of the server:
Procedure
Store the CA and server certificates in the
/etc/pki/tls/certs/directory:mv <path>/server.example.com.crt.pem /etc/pki/tls/certs/ mv <path>/ca.crt.pem /etc/pki/tls/certs/
# mv <path>/server.example.com.crt.pem /etc/pki/tls/certs/ # mv <path>/ca.crt.pem /etc/pki/tls/certs/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set permissions on the CA and server certificate that enable the MariaDB server to read the files:
chmod 644 /etc/pki/tls/certs/server.example.com.crt.pem /etc/pki/tls/certs/ca.crt.pem
# chmod 644 /etc/pki/tls/certs/server.example.com.crt.pem /etc/pki/tls/certs/ca.crt.pemCopy to Clipboard Copied! Toggle word wrap Toggle overflow Because certificates are part of the communication before a secure connection is established, any client can retrieve them without authentication. Therefore, you do not need to set strict permissions on the CA and server certificate files.
Store the server’s private key in the
/etc/pki/tls/private/directory:mv <path>/server.example.com.key.pem /etc/pki/tls/private/
# mv <path>/server.example.com.key.pem /etc/pki/tls/private/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set secure permissions on the server’s private key:
chmod 640 /etc/pki/tls/private/server.example.com.key.pem chgrp mysql /etc/pki/tls/private/server.example.com.key.pem
# chmod 640 /etc/pki/tls/private/server.example.com.key.pem # chgrp mysql /etc/pki/tls/private/server.example.com.key.pemCopy to Clipboard Copied! Toggle word wrap Toggle overflow If unauthorized users have access to the private key, connections to the MariaDB server are no longer secure.
Restore the SELinux context:
restorecon -Rv /etc/pki/tls/
# restorecon -Rv /etc/pki/tls/Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.4.2. Configuring TLS on a MariaDB server Link kopierenLink in die Zwischenablage kopiert!
To improve security, enable TLS support on the MariaDB server. As a result, clients can transmit data with the server using TLS encryption.
Prerequisites
- You installed the MariaDB server.
-
The
mariadbservice is running. The following files in Privacy Enhanced Mail (PEM) format exist on the server and are readable by the
mysqluser:-
The private key of the server:
/etc/pki/tls/private/server.example.com.key.pem -
The server certificate:
/etc/pki/tls/certs/server.example.com.crt.pem -
The Certificate Authority (CA) certificate
/etc/pki/tls/certs/ca.crt.pem
-
The private key of the server:
- The subject distinguished name (DN) or the subject alternative name (SAN) field in the server certificate matches the server’s host name.
- If the server runs RHEL 9.2 or later and the FIPS mode is enabled, clients must either support the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced enforced on RHEL 9.2 and later.
Procedure
Create the
/etc/my.cnf.d/mariadb-server-tls.cnffile:Add the following content to configure the paths to the private key, server and CA certificate:
[mariadb] ssl_key = /etc/pki/tls/private/server.example.com.key.pem ssl_cert = /etc/pki/tls/certs/server.example.com.crt.pem ssl_ca = /etc/pki/tls/certs/ca.crt.pem
[mariadb] ssl_key = /etc/pki/tls/private/server.example.com.key.pem ssl_cert = /etc/pki/tls/certs/server.example.com.crt.pem ssl_ca = /etc/pki/tls/certs/ca.crt.pemCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you have a Certificate Revocation List (CRL), configure the MariaDB server to use it:
ssl_crl = /etc/pki/tls/certs/example.crl.pem
ssl_crl = /etc/pki/tls/certs/example.crl.pemCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Reject connection attempts without encryption. To enable this feature, append:
require_secure_transport = on
require_secure_transport = onCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Set the TLS versions the server should support. For example, to support TLS 1.2 and TLS 1.3, append:
tls_version = TLSv1.2,TLSv1.3
tls_version = TLSv1.2,TLSv1.3Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the server supports TLS 1.1, TLS 1.2, and TLS 1.3.
Restart the
mariadbservice:systemctl restart mariadb.service
# systemctl restart mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To simplify troubleshooting, perform the following steps on the MariaDB server before you configure the local client to use TLS encryption:
Verify that MariaDB now has TLS encryption enabled:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the
have_sslvariable is set toyes, TLS encryption is enabled.If you configured the MariaDB service to only support specific TLS versions, display the
tls_versionvariable:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.4.3. Requiring TLS encrypted connections for specific user accounts Link kopierenLink in die Zwischenablage kopiert!
Users that have access to sensitive data should always use a TLS-encrypted connection to avoid sending data unencrypted over the network.
If you cannot configure on the server that a secure transport is required for all connections (require_secure_transport = on), configure individual user accounts to require TLS encryption.
Prerequisites
- The MariaDB server has TLS support enabled.
- The user you configure to require secure transport exists.
Procedure
Connect as an administrative user to the MariaDB server:
mysql -u root -p -h server.example.com
# mysql -u root -p -h server.example.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow If your administrative user has no permissions to access the server remotely, perform the command on the MariaDB server and connect to
localhost.Use the
REQUIRE SSLclause to enforce that a user must connect using a TLS-encrypted connection:MariaDB [(none)]> ALTER USER 'example'@'%' REQUIRE SSL;
MariaDB [(none)]> ALTER USER 'example'@'%' REQUIRE SSL;Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Connect to the server as the
exampleuser using TLS encryption:mysql -u example -p -h server.example.com --ssl
# mysql -u example -p -h server.example.com --ssl ... MariaDB [(none)]>Copy to Clipboard Copied! Toggle word wrap Toggle overflow If no error is shown and you have access to the interactive MariaDB console, the connection with TLS succeeds.
Attempt to connect as the
exampleuser with TLS disabled:mysql -u example -p -h server.example.com --skip-ssl
# mysql -u example -p -h server.example.com --skip-ssl ERROR 1045 (28000): Access denied for user 'example'@'server.example.com' (using password: YES)Copy to Clipboard Copied! Toggle word wrap Toggle overflow The server rejected the login attempt because TLS is required for this user but disabled (
--skip-ssl).
2.5. Configuring the MariaDB client to use TLS encryption by default Link kopierenLink in die Zwischenablage kopiert!
On RHEL, you can globally configure that the MariaDB client uses TLS encryption and verifies that the Common Name (CN) in the server certificate matches the hostname the user connects to. This prevents man-in-the-middle attacks.
Prerequisites
- The MariaDB server has TLS support enabled.
- If the certificate authority (CA) that issued the server’s certificate is not trusted by RHEL, the CA certificate has been copied to the client.
- If the MariaDB server runs RHEL 9.2 or later and the FIPS mode is enabled, this client supports the Extended Master Secret (EMS) extension or uses TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced on RHEL 9.2 and later.
Procedure
If RHEL does not trust the CA that issued the server’s certificate:
Copy the CA certificate to the
/etc/pki/ca-trust/source/anchors/directory:cp <path>/ca.crt.pem /etc/pki/ca-trust/source/anchors/
# cp <path>/ca.crt.pem /etc/pki/ca-trust/source/anchors/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set permissions that enable all users to read the CA certificate file:
chmod 644 /etc/pki/ca-trust/source/anchors/ca.crt.pem
# chmod 644 /etc/pki/ca-trust/source/anchors/ca.crt.pemCopy to Clipboard Copied! Toggle word wrap Toggle overflow Rebuild the CA trust database:
update-ca-trust
# update-ca-trustCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Create the
/etc/my.cnf.d/mariadb-client-tls.cnffile with the following content:[client-mariadb] ssl ssl-verify-server-cert
[client-mariadb] ssl ssl-verify-server-certCopy to Clipboard Copied! Toggle word wrap Toggle overflow These settings define that the MariaDB client uses TLS encryption (
ssl) and that the client compares the hostname with the CN in the server certificate (ssl-verify-server-cert).
Verification
Connect to the server using the hostname, and display the server status:
mysql -u root -p -h server.example.com -e status
# mysql -u root -p -h server.example.com -e status ... SSL: Cipher in use is TLS_AES_256_GCM_SHA384Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the
SSLentry containsCipher in use is…, the connection is encrypted.Note that the user you use in this command has permissions to authenticate remotely.
If the hostname you connect to does not match the hostname in the TLS certificate of the server, the
ssl-verify-server-certparameter causes the connection to fail. For example, if you connect tolocalhost:mysql -u root -p -h localhost -e status
# mysql -u root -p -h localhost -e status ERROR 2026 (HY000): SSL connection error: Validation of SSL server certificate failedCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6. Backing up and restoring MariaDB data with logical dumps Link kopierenLink in die Zwischenablage kopiert!
A logical backup of MariaDB data consists of the SQL statements necessary to restore the data. The advantage of logical backup over physical backup is that the data can be restored on other hardware configurations and MariaDB versions.
2.6.1. Performing a logical backup by using mysqldump Link kopierenLink in die Zwischenablage kopiert!
The mysqldump utility is a versatile backup tool that can export one or more databases. Its output is typically a file containing SQL statements designed to re-create the database structure and populate it with data, making it ideal for backups or server transfers.
To perform the mysqldump backup, you can use one of the following options:
- Back up one or more selected databases
- Back up all databases
- Back up a subset of tables from one database
Procedure
To dump a single database, run:
mysqldump [options] --databases db_name > backup-file.sql
# mysqldump [options] --databases db_name > backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To dump multiple databases at once, run:
mysqldump [options] --databases db_name1 [db_name2 ...] > backup-file.sql
# mysqldump [options] --databases db_name1 [db_name2 ...] > backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To dump all databases, run:
mysqldump [options] --all-databases > backup-file.sql
# mysqldump [options] --all-databases > backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To load one or more dumped full databases back into a server, run:
mysql < backup-file.sql
# mysql < backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To load a database to a remote MySQL server, run:
mysql --host=remote_host < backup-file.sql
# mysql --host=remote_host < backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To dump a subset of tables from one database, add a list of the chosen tables at the end of the
mysqldumpcommand:mysqldump [options] db_name [tbl_name ...] > backup-file.sql
# mysqldump [options] db_name [tbl_name ...] > backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow To load a literal,subset of tables dumped from one database, run:
mysql db_name < backup-file.sql
# mysql db_name < backup-file.sqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe db_name database must exist at this point.
To see a list of the options that mysqldump supports, run:
mysqldump --help
$ mysqldump --helpCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.7. Backing up and restoring MariaDB data with physical copies Link kopierenLink in die Zwischenablage kopiert!
A physical backup of MariaDB data contains file and directories that store the content. This method is typically faster and smaller in size.
2.7.1. Performing a physical online backup by using mariabackup Link kopierenLink in die Zwischenablage kopiert!
You can create physical online backups of your MariaDB server by using the mariabackup utility to backup InnoDB, Aria, and MyISAM tables while the server is running. The utility supports full backup capability for MariaDB server, which includes encrypted and compressed data.
Prerequisites
The
mariadb-backuppackage is installed on the system:dnf install mariadb-backup
# dnf install mariadb-backupCopy to Clipboard Copied! Toggle word wrap Toggle overflow
- You must provide Mariabackup with credentials for the user under which the backup will be run. You can provide the credentials either on the command line or by a configuration file.
-
Users of Mariabackup must have the
RELOAD,LOCK TABLES, andREPLICATION CLIENTprivileges.
To create a backup of a database using Mariabackup, use the following procedure.
Procedure
To create a backup while providing credentials on the command line, run:
mariabackup --backup --target-dir <backup_directory> --user <backup_user> --password <backup_passwd>
$ mariabackup --backup --target-dir <backup_directory> --user <backup_user> --password <backup_passwd>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
target-diroption defines the directory where the backup files are stored. If you want to perform a full backup, the target directory must be empty or not exist.The
userandpasswordoptions allow you to configure the user name and the password.To create a backup with credentials set in a configuration file:
-
Create a configuration file in the
/etc/my.cnf.d/directory, for example,/etc/my.cnf.d/mariabackup.cnf. Add the following lines into the
[xtrabackup]or[mysqld]section of the new file:[xtrabackup] user=myuser password=mypassword
[xtrabackup] user=myuser password=mypasswordCopy to Clipboard Copied! Toggle word wrap Toggle overflow Perform the backup:
mariabackup --backup --target-dir <backup_directory>
$ mariabackup --backup --target-dir <backup_directory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Create a configuration file in the
2.7.2. Restoring data by using mariabackup Link kopierenLink in die Zwischenablage kopiert!
If you have a MariaDB backup created by the mariabackup utility, you can use the same utility to restore the data.
Prerequisites
-
The
mariadbservice is stopped. - The data directory is empty.
-
Users of
mariabackupmust have theRELOAD,LOCK TABLES, andREPLICATION CLIENTprivileges.
Procedure
Use one of the following options to restore the data:
To restore data from the backup in the
/var/mariadb/backup/directory and keep the original backup files, enter:mariabackup --copy-back --target-dir=/var/mariadb/backup/
$ mariabackup --copy-back --target-dir=/var/mariadb/backup/Copy to Clipboard Copied! Toggle word wrap Toggle overflow To restore data from the backup in the
/var/mariadb/backup/directory and remove the original backup files, enter:mariabackup --move-back --target-dir=/var/mariadb/backup/
$ mariabackup --move-back --target-dir=/var/mariadb/backup/Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Fix the file permissions. For example, to recursively change ownership of the files to the
mysqluser and group, enter:chown -R mysql:mysql /var/lib/mysql/
# chown -R mysql:mysql /var/lib/mysql/Copy to Clipboard Copied! Toggle word wrap Toggle overflow When restoring a database,
mariabackuppreserves the file and directory privileges of the backup. However,mariabackupwrites the files to disk as the user and group restoring the database. Therefore, after restoring a backup, you must adjust the owner of the data directory to match the user and group for the MariaDB server.Start the
mariadbservice:systemctl start mariadb.service
# systemctl start mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.7.3. Performing a file system backup on a MariaDB server Link kopierenLink in die Zwischenablage kopiert!
You can create a file system backup of your MariaDB server by copying data files, configuration files, and log files to a backup location.
Procedure
Stop the
mariadbservice:systemctl stop mariadb.service
# systemctl stop mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the data files to the required location:
cp -r /var/lib/mysql /backup-location
# cp -r /var/lib/mysql /backup-locationCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Copy the configuration files to the required location:
cp -r /etc/my.cnf /etc/my.cnf.d /backup-location/configuration
# cp -r /etc/my.cnf /etc/my.cnf.d /backup-location/configurationCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Copy the log files to the required location:
cp /var/log/mariadb/* /backup-location/logs
# cp /var/log/mariadb/* /backup-location/logsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
mariadbservice:systemctl start mariadb.service
# systemctl start mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow When loading the backed up data from the backup location to the
/var/lib/mysqldirectory, ensure thatmysql:mysqlis an owner of all data in/var/lib/mysql:chown -R mysql:mysql /var/lib/mysql
# chown -R mysql:mysql /var/lib/mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.8. Replicating MariaDB with Galera Link kopierenLink in die Zwischenablage kopiert!
You can replicate a MariaDB database by using the Galera solution on Red Hat Enterprise Linux 9.
Replication itself is not a sufficient backup solution. Replication protects source servers against hardware failures, but it does not ensure protection against data loss.
2.8.1. Introduction to MariaDB Galera Cluster Link kopierenLink in die Zwischenablage kopiert!
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
2.8.2. Components to build MariaDB Galera Cluster Link kopierenLink in die Zwischenablage kopiert!
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 9 provides the upstream version of these files, located at
/usr/lib/systemd/system/garbd.serviceand/usr/sbin/garb-systemd.
2.8.3. Deploying MariaDB Galera Cluster Link kopierenLink in die Zwischenablage kopiert!
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
TLS Web Server Authentication, TLS Web Client AuthenticationCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
Install the MariaDB Galera Cluster packages:
dnf install mariadb-server-galera
dnf install mariadb-server-galeraCopy to Clipboard Copied! Toggle word wrap Toggle overflow As a result, the following packages are installed together with their dependencies:
-
mariadb-server-galera -
mariadb-server galeraFor more information about which packages you need to install 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://
gcomm://Copy to Clipboard Copied! Toggle word wrap Toggle overflow For the initial node, it is possible to set
wsrep_cluster_addressas an empty list:wsrep_cluster_address="gcomm://"
wsrep_cluster_address="gcomm://"Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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"
wsrep_cluster_address="gcomm://10.0.0.10"Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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”
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”Copy to Clipboard Copied! Toggle word wrap Toggle overflow Bootstrap a first node of a new cluster by running the following wrapper on that node:
galera_new_cluster
# galera_new_clusterCopy to Clipboard Copied! Toggle word wrap Toggle overflow This wrapper ensures that the MariaDB server daemon (
mysqld) 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.NoteThe
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@node1
# galera_new_cluster mariadb@node1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Connect other nodes to the cluster by running the following command on each of the nodes:
systemctl start mariadb.service
# systemctl start mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow As a result, the node connects to the cluster, and synchronizes itself with the state of the cluster.
Verification
2.8.4. Checking the status of a MariaDB Galera cluster Link kopierenLink in die Zwischenablage kopiert!
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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Display the node’s cluster component status:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.
2.8.5. Adding a new node to MariaDB Galera Cluster Link kopierenLink in die Zwischenablage kopiert!
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"
[mariadb] wsrep_cluster_address="gcomm://192.168.0.1"Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.
2.8.6. Restarting MariaDB Galera Cluster Link kopierenLink in die Zwischenablage kopiert!
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 Configuring MariaDB Galera Cluster.
If the cluster is not bootstrapped, and mariadb on the first node is started with only the systemctl start mariadb.service 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.
2.9. Migrating to MariaDB 10.5 Link kopierenLink in die Zwischenablage kopiert!
You can migrate your MariaDB database from version 10.3 on RHEL 8 to version 10.5 on RHEL 9 to take advantage of new features and improvements in the newer version.
In RHEL 8, the MariaDB server is available in versions 10.3, 10.5, and 10.11, each provided by a separate module stream. RHEL 9 provides MariaDB 10.5, MariaDB 10.11, and MySQL 8.0.
2.9.1. Notable differences between MariaDB 10.3 and MariaDB 10.5 Link kopierenLink in die Zwischenablage kopiert!
MariaDB 10.5 introduces significant changes including new authentication plugins, updated privileges, improved Galera Cluster features, and enhanced PAM plugin functionality.
Significant changes between MariaDB 10.3 and MariaDB 10.5 include:
-
MariaDB now uses the
unix_socketauthentication plugin by default. The plugin enables users to use operating system credentials when connecting to MariaDB through the local UNIX socket file. -
MariaDBaddsmariadb-*named binaries andmysql*symbolic links pointing to themariadb-*binaries. For example, themysqladmin,mysqlaccess, andmysqlshowsymlinks point to themariadb-admin,mariadb-access, andmariadb-showbinaries, respectively. -
The
SUPERprivilege has been split into several privileges to better align with each user role. As a result, certain statements have changed required privileges. -
In parallel replication, the
slave_parallel_modenow defaults tooptimistic. -
In the InnoDB storage engine, defaults of the following variables have been changed:
innodb_adaptive_hash_indextoOFFandinnodb_checksum_algorithmtofull_crc32. MariaDB now uses the
libeditimplementation of the underlying software managing the MariaDB command history (the.mysql_historyfile) instead of the previously usedreadlinelibrary. This change impacts users working directly with the.mysql_historyfile. Note that.mysql_historyis a file managed by the MariaDB or MySQL applications, and users should not work with the file directly. The human-readable appearance is coincidental.NoteTo increase security, you can consider not maintaining a history file. To disable the command history recording:
-
Remove the
.mysql_historyfile if it exists. Use either of the following approaches:
-
Set the
MYSQL_HISTFILEvariable to/dev/nulland include this setting in any of your shell’s startup files. Change the
.mysql_historyfile to a symbolic link to/dev/null:ln -s /dev/null $HOME/.mysql_history
$ ln -s /dev/null $HOME/.mysql_historyCopy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Set the
-
Remove the
MariaDB Galera Cluster has been upgraded to version 4 with the following notable changes:
- Galera adds a new streaming replication feature, which supports replicating transactions of unlimited size. During an execution of streaming replication, a cluster replicates a transaction in small fragments.
- Galera now fully supports Global Transaction ID (GTID).
-
The default value for the
wsrep_onoption in the/etc/my.cnf.d/galera.cnffile has changed from1to0to prevent end users from startingwsrepreplication without configuring required additional options.
Changes to the PAM plugin in MariaDB 10.5 include:
-
MariaDB 10.5 adds a new version of the Pluggable Authentication Modules (PAM) plugin. The PAM plugin version 2.0 performs PAM authentication using a separate
setuid roothelper binary, which enables MariaDB to use additional PAM modules. -
The helper binary can be executed only by users in the
mysqlgroup. By default, the group contains only themysqluser. Red Hat recommends that administrators do not add more users to themysqlgroup to prevent password-guessing attacks without throttling or logging through this helper utility. -
In MariaDB 10.5, the Pluggable Authentication Modules (PAM) plugin and its related files have been moved to a new package,
mariadb-pam. As a result, no newsetuid rootbinary is introduced on systems that do not use PAM authentication forMariaDB. -
The
mariadb-pampackage contains both PAM plugin versions: version 2.0 is the default, and version 1.0 is available as theauth_pam_v1shared object library. -
The
mariadb-pampackage is not installed by default with the MariaDB server. To make the PAM authentication plugin available in MariaDB 10.5, install themariadb-pampackage manually.
2.9.2. Migrating from a RHEL 8 version of MariaDB 10.3 to a RHEL 9 version of MariaDB 10.5 Link kopierenLink in die Zwischenablage kopiert!
This procedure describes migrating from the MariaDB 10.3 to the MariaDB 10.5 using the mariadb-upgrade utility.
The mariadb-upgrade utility is provided by the mariadb-server-utils subpackage, which is installed as a dependency of the mariadb-server package.
There are certain risks and known problems related to an in-place upgrade. For example, some queries might not work or they will be run in a different order than before the upgrade. For more information about these risks and problems, and for general information about an in-place upgrade, see MariaDB 10.5 Release Notes.
Prerequisites
- Before performing the upgrade, back up all your data stored in the MariaDB databases.
Procedure
Ensure that the
mariadb-serverpackage is installed on the RHEL 9 system:dnf install mariadb-server
# dnf install mariadb-serverCopy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure that the
mariadbservice is not running on either of the source and target systems at the time of copying data:systemctl stop mariadb.service
# systemctl stop mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Copy the data from the source location to the
/var/lib/mysql/directory on the RHEL 9 target system. Set the appropriate permissions and SELinux context for copied files on the target system:
restorecon -vr /var/lib/mysql
# restorecon -vr /var/lib/mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure that
mysql:mysqlis owner of all data in the/var/lib/mysqldirectory:chown -R mysql:mysql /var/lib/mysql
# chown -R mysql:mysql /var/lib/mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Adjust the configuration so that option files located in
/etc/my.cnf.d/include only options valid for MariaDB 10.5. For details, see upstream documentation for MariaDB 10.4 and MariaDB 10.5 Start the MariaDB server on the target system.
When upgrading a database running standalone:
systemctl start mariadb.service
# systemctl start mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow When upgrading a Galera cluster node:
galera_new_cluster
# galera_new_clusterCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
mariadbservice will be started automatically.
Execute the mariadb-upgrade utility to check and repair internal tables.
When upgrading a database running standalone:
mariadb-upgrade
$ mariadb-upgradeCopy to Clipboard Copied! Toggle word wrap Toggle overflow When upgrading a Galera cluster node:
mariadb-upgrade --skip-write-binlog
$ mariadb-upgrade --skip-write-binlogCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.10. Upgrading from MariaDB 10.5 to MariaDB 10.11 Link kopierenLink in die Zwischenablage kopiert!
You can upgrade your MariaDB database from version 10.5 to 10.11 on RHEL 9 to take advantage of new features and improvements in the newer version.
2.10.1. Notable differences between MariaDB 10.5 and MariaDB 10.11 Link kopierenLink in die Zwischenablage kopiert!
MariaDB 10.11 introduces significant changes including atomic DDL statements, new UUID data type, enhanced SSL support, improved Unicode collations, and updated systemd integration.
Significant changes between MariaDB 10.5 and MariaDB 10.11 include:
-
A new
sys_schemafeature is a collection of views, functions, and procedures to provide information about database usage. -
The
CREATE TABLE,ALTER TABLE,RENAME TABLE,DROP TABLE,DROP DATABASE, and related Data Definition Language (DDL) statements are now atomic. The statement must be fully completed, otherwise the changes are reverted. Note that when deleting multiple tables withDROP TABLE, only each individual drop is atomic, not the full list of tables. -
A new
GRANT … TO PUBLICprivilege is available. -
The
SUPERandREAD ONLY ADMINprivileges are now separate. -
You can now store universally unique identifiers in the new
UUIDdatabase data type. - MariaDB now supports the Secure Socket Layer (SSL) protocol version 3.
- The MariaDB server now requires correctly configured SSL to start. Previously, MariaDB silently disabled SSL and used insecure connections in case of misconfigured SSL.
-
MariaDB now supports the natural sort order through the
natural_sort_key()function. -
A new
SFORMATfunction is now available for arbitrary text formatting. -
The
utf8character set (and related collations) is now by default an alias forutf8mb3. - MariaDB supports the Unicode Collation Algorithm (UCA) 14 collations.
-
systemdsocket activation files for MariaDB are now available in the/usr/share/directory. Note that they are not a part of the default configuration in RHEL as opposed to upstream. -
Error messages now contain the
MariaDBstring instead ofMySQL. - Error messages are now available in the Chinese language.
- The default logrotate file has changed significantly. Review your configuration before migrating to MariaDB 10.11.
-
For MariaDB and MySQL clients, the connection property specified on the command line (for example,
--port=3306), now forces the protocol type of communication between the client and the server, such astcp,socket,pipe, ormemory. Previously, for example, the specified port was ignored if a MariaDB client connected through a UNIX socket.
2.10.2. Upgrading from a RHEL 9 version of MariaDB 10.5 to MariaDB 10.11 Link kopierenLink in die Zwischenablage kopiert!
You can upgrade your MariaDB database from version 10.5 to 10.11 on RHEL 9 to access new features and improvements.
There are certain risks and known problems related to an in-place upgrade. For example, some queries might not work or they will be run in a different order than before the upgrade. For more information about these risks and problems, and for general information about an in-place upgrade, see MariaDB 10.11 Release Notes.
Prerequisites
- Before performing the upgrade, back up all your data stored in the MariaDB databases.
Procedure
Stop the MariaDB server:
systemctl stop mariadb.service
# systemctl stop mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Switch from the non-modular MariaDB 10.5 to modular MariaDB 10.11:
dnf module switch-to mariadb:10.11
# dnf module switch-to mariadb:10.11Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Adjust the configuration so that option files located in
/etc/my.cnf.d/include only options valid for MariaDB 10.11. For details, see upstream documentation for MariaDB 10.6 and MariaDB 10.11. Start the MariaDB server.
When upgrading a database running standalone:
systemctl start mariadb.service
# systemctl start mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow When upgrading a Galera cluster node:
galera_new_cluster
# galera_new_clusterCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
mariadbservice will be started automatically.
Execute the mariadb-upgrade utility to check and repair internal tables.
When upgrading a database running standalone:
mariadb-upgrade
# mariadb-upgradeCopy to Clipboard Copied! Toggle word wrap Toggle overflow When upgrading a Galera cluster node:
mariadb-upgrade --skip-write-binlog
# mariadb-upgrade --skip-write-binlogCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.11. Developing MariaDB client applications Link kopierenLink in die Zwischenablage kopiert!
MariaDB client applications should be developed against the MariaDB client library. The development files and programs necessary for building these applications are provided by the mariadb-connector-c-devel package.
Instead of using a direct library name, use the mariadb_config program, which is distributed in the mariadb-connector-c-devel package. This program ensures that the correct build flags are returned.