Chapter 3. Using MySQL
MySQL server is a high-performance, open source relational database management system (RDBMS). It offers an SQL interface for data access and includes advanced features, such as support for multiple storage engines.
Learn how to install and configure MySQL on a RHEL system, how to back up MySQL data, how to migrate from an earlier MySQL version, and how to replicate a MySQL.
3.1. Installing MySQL Copy linkLink copied to clipboard!
RHEL 9 provides MySQL 8.0 as the initial version of the Application Stream, which you can install as an RPM package. Additional MySQL versions are provided as modules with a shorter life cycle in minor releases of RHEL 9.
In RHEL 9, the MySQL server is available in the following versions, each provided by a separate stream:
- MySQL 8.0
- MySQL 8.4 - available since RHEL 9.6
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 MySQL server packages:
For MySQL 8.0 from the RPM package, enter:
dnf install mysql-server
# dnf install mysql-serverCopy to Clipboard Copied! Toggle word wrap Toggle overflow For MySQL 8.4 by selecting the
8.4stream from themysqlmodule and specifying the server profile:dnf module install mysql:8.4/server
# dnf module install mysql:8.4/serverCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Start the
mysqldservice:systemctl start mysqld.service
# systemctl start mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
mysqldservice to start at boot:systemctl enable mysqld.service
# systemctl enable mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Recommended: To improve security when installing MySQL, run the following command:
mysql_secure_installation
$ mysql_secure_installationCopy to Clipboard Copied! Toggle word wrap Toggle overflow The command launches a fully interactive script, which prompts for each step in the process. The script enables you to improve security in the following ways:
- Setting a password for root accounts
- Removing anonymous users
- Disallowing remote root logins (outside the local host)
3.2. Using containers to run multiple MariaDB and MySQL instances on a single host Copy linkLink copied to clipboard!
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
3.3. Configuring MySQL Copy linkLink copied to clipboard!
To configure the MySQL server for networking, use the following procedure.
Procedure
Edit the
[mysqld]section of the/etc/my.cnf.d/mysql-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 MySQL listens for TCP/IP connections.
Restart the
mysqldservice:systemctl restart mysqld.service
# systemctl restart mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.4. Setting up TLS encryption on a MySQL server Copy linkLink copied to clipboard!
By default, MySQL uses unencrypted connections. For secure connections, enable TLS support on the MariaDB server and configure your clients to establish encrypted connections.
3.4.1. Placing the CA certificate, server certificate, and private key on the MySQL server Copy linkLink copied to clipboard!
Before you can enable TLS encryption on the MySQL server, store the certificate authority (CA) certificate, the server certificate, and the private key on the MySQL 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 MySQL 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 MySQL 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
3.4.2. Configuring TLS encryption on a MySQL server Copy linkLink copied to clipboard!
By default, MySQL uses unencrypted connections. For more secure connections, you can enable Transport Layer Security (TLS) support on the MySQL server and configure your clients to establish encrypted connections.
Prerequisites
- You installed the MySQL server.
-
The
mysqldservice 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.
Procedure
Create the
/etc/my.cnf.d/mysql-server-tls.cnffile:Add the following content to configure the paths to the private key, server and CA certificate:
[mysqld] 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
[mysqld] 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 MySQL 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 only TLS 1.3, append:
tls_version = TLSv1.3
tls_version = TLSv1.3Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the server supports TLS 1.2 and TLS 1.3.
Restart the
mysqldservice:systemctl restart mysqld.service
# systemctl restart mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To simplify troubleshooting, perform the following steps on the MySQL server before you configure the local client to use TLS encryption:
Verify that MySQL now has TLS encryption enabled:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you configured the MySQL server to only support specific TLS versions, display the
tls_versionvariable:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the server uses the correct CA certificate, server certificate, and private key files:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.4.3. Requiring TLS encrypted connections for specific user accounts Copy linkLink copied to clipboard!
You can configure specific MySQL accounts to require TLS-encrypted connections to protect sensitive data transmission.
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 MySQL server has TLS support enabled.
- The user you configure to require secure transport exists.
- The CA certificate is stored on the client.
Procedure
Connect as an administrative user to the MySQL 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 MySQL server and connect to
localhost.Use the
REQUIRE SSLclause to enforce that a user must connect using a TLS-encrypted connection:MySQL [(none)]> ALTER USER 'example'@'%' REQUIRE SSL;
MySQL [(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
# mysql -u example -p -h server.example.com ... MySQL [(none)]>Copy to Clipboard Copied! Toggle word wrap Toggle overflow If no error is shown and you have access to the interactive MySQL console, the connection with TLS succeeds.
By default, the client automatically uses TLS encryption if the server provides it. Therefore, the
--ssl-ca=ca.crt.pemand--ssl-mode=VERIFY_IDENTITYoptions are not required, but improve the security because, with these options, the client verifies the identity of the server.Attempt to connect as the
exampleuser with TLS disabled:mysql -u example -p -h server.example.com --ssl-mode=DISABLED
# mysql -u example -p -h server.example.com --ssl-mode=DISABLED 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 (
--ssl-mode=DISABLED).
3.5. Globally enabling TLS encryption with CA certificate validation in MySQL clients Copy linkLink copied to clipboard!
If your MySQL server supports TLS encryption, configure your clients to establish only secure connections and to verify the server certificate. This procedure describes how to enable TLS support for all users on the server.
3.5.1. Configuring the MySQL client to use TLS encryption by default Copy linkLink copied to clipboard!
On RHEL, you can globally configure that the MySQL 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 MySQL server has TLS support enabled.
-
The CA certificate is stored in the
/etc/pki/tls/certs/ca.crt.pemfile on the client.
Procedure
Create the
/etc/my.cnf.d/mysql-client-tls.cnffile with the following content:[client] ssl-mode=VERIFY_IDENTITY ssl-ca=/etc/pki/tls/certs/ca.crt.pem
[client] ssl-mode=VERIFY_IDENTITY ssl-ca=/etc/pki/tls/certs/ca.crt.pemCopy to Clipboard Copied! Toggle word wrap Toggle overflow These settings define that the MySQL client uses TLS encryption and that the client compares the hostname with the CN in the server certificate (
ssl-mode=VERIFY_IDENTITY). Additionally, it specifies the path to the CA certificate (ssl-ca).
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-mode=VERIFY_IDENTITYparameter 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: error:0A000086:SSL routines::certificate verify failedCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.6. Backing up and restoring MySQL data with logical dumps Copy linkLink copied to clipboard!
A logical backup of MySQL 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 MySQL versions.
3.6.1. Performing a logical backup by using mysqldump Copy linkLink copied to clipboard!
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
3.7. Backing up and restoring MySQL data with physical copies Copy linkLink copied to clipboard!
A physical backup of MySQL data contains file and directories that store the content. This method is typically faster and smaller in size.
3.7.1. Performing a file system backup on a MySQL server Copy linkLink copied to clipboard!
You can create a file system backup of your MySQL server by copying data files, configuration files, and log files to a backup location.
Procedure
Stop the
mysqldservice:systemctl stop mysqld.service
# systemctl stop mysqld.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/mysql/* /backup-location/logs
# cp /var/log/mysql/* /backup-location/logsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
mysqldservice:systemctl start mysqld.service
# systemctl start mysqld.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
3.8. Replicating MySQL with TLS encryption Copy linkLink copied to clipboard!
You can set up MySQL replication with TLS encryption to create secure data replication between source and replica servers.
Replication itself is not a sufficient backup solution. Replication protects source servers against hardware failures, but it does not ensure protection against data loss.
3.8.1. Configuring a MySQL source server Copy linkLink copied to clipboard!
You can set configuration options required for a MySQL source server to properly run and replicate all changes made on the database server through the TLS protocol.
Prerequisites
- The source server is installed.
The source server has TLS set up.
ImportantThe source and replica certificates must be signed by the same certificate authority.
Procedure
Include the following options in the
/etc/my.cnf.d/mysql-server.cnffile under the[mysqld]section:bind-address=source_ip_adressThis option is required for connections made from replicas to the source.
server-id=idThe id must be unique.
log_bin=path_to_source_server_logThis option defines a path to the binary log file of the MySQL source server. For example:
log_bin=/var/log/mysql/mysql-bin.log.gtid_mode=ONThis option enables global transaction identifiers (GTIDs) on the server.
enforce-gtid-consistency=ONThe server enforces GTID consistency by allowing execution of only statements that can be safely logged using a GTID.
Optional:
binlog_do_db=db_nameUse this option if you want to replicate only selected databases. To replicate more than one selected database, specify each of the databases separately:
binlog_do_db=db_name1 binlog_do_db=db_name2 binlog_do_db=db_name3
binlog_do_db=db_name1 binlog_do_db=db_name2 binlog_do_db=db_name3Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional:
binlog_ignore_db=db_nameUse this option to exclude a specific database from replication.
Restart the
mysqldservice:systemctl restart mysqld.service
# systemctl restart mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.8.2. Configuring a MySQL replica server Copy linkLink copied to clipboard!
You can set configuration options required for a MySQL replica server to ensure a successful replication.
Prerequisites
- The replica server is installed.
The replica server has TLS set up.
ImportantThe source and replica certificates must be signed by the same certificate authority.
Procedure
Include the following options in the
/etc/my.cnf.d/mysql-server.cnffile under the[mysqld]section:server-id=idThe id must be unique.
relay-log=path_to_replica_server_logThe relay log is a set of log files created by the MySQL replica server during replication.
log_bin=path_to_replica_sever_logThis option defines a path to the binary log file of the MySQL replica server. For example:
log_bin=/var/log/mysql/mysql-bin.log.This option is not required in a replica but strongly recommended.
gtid_mode=ONThis option enables global transaction identifiers (GTIDs) on the server.
enforce-gtid-consistency=ONThe server enforces GTID consistency by allowing execution of only statements that can be safely logged using a GTID.
log-replica-updates=ONThis option ensures that updates received from the source server are logged in the replica’s binary log.
skip-replica-start=ONThis option ensures that the replica server does not start the replication threads when the replica server starts.
Optional:
binlog_do_db=db_nameUse this option if you want to replicate only certain databases. To replicate more than one database, specify each of the databases separately:
binlog_do_db=db_name1 binlog_do_db=db_name2 binlog_do_db=db_name3
binlog_do_db=db_name1 binlog_do_db=db_name2 binlog_do_db=db_name3Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional:
binlog_ignore_db=db_nameUse this option to exclude a specific database from replication.
Restart the
mysqldservice:systemctl restart mysqld.service
# systemctl restart mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.8.3. Creating a replication user on the MySQL source server Copy linkLink copied to clipboard!
You must create a replication user with appropriate permissions on the MySQL source server to enable replica servers to connect and receive data changes.
Prerequisites
- The source server is installed and configured as described in Configuring a MySQL source server.
Procedure
Create a replication user:
mysql> CREATE USER 'replication_user'@'replica_server_hostname' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> CREATE USER 'replication_user'@'replica_server_hostname' IDENTIFIED WITH mysql_native_password BY 'password';Copy to Clipboard Copied! Toggle word wrap Toggle overflow Grant the user replication permissions:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'replica_server_hostname';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'replica_server_hostname';Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reload the grant tables in the MySQL database:
mysql> FLUSH PRIVILEGES;
mysql> FLUSH PRIVILEGES;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the source server to read-only state:
mysql> SET @@GLOBAL.read_only = ON;
mysql> SET @@GLOBAL.read_only = ON;Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.8.4. Connecting the replica server to the source server Copy linkLink copied to clipboard!
On the MySQL replica server, you must configure credentials and the address of the source server. Use the following procedure to implement the replica server.
Prerequisites
- The source server is installed and configured as described in Configuring a MySQL source server.
- The replica server is installed and configured as described in Configuring a MySQL replica server.
- You have created a replication user. See Creating a replication user on the MySQL source server.
Procedure
Set the replica server to read-only state:
mysql> SET @@GLOBAL.read_only = ON;
mysql> SET @@GLOBAL.read_only = ON;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the replication source:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the replica thread in the MySQL replica server:
mysql> START REPLICA;
mysql> START REPLICA;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Unset the read-only state on both the source and replica servers:
mysql> SET @@GLOBAL.read_only = OFF;
mysql> SET @@GLOBAL.read_only = OFF;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Inspect the status of the replica server for debugging purposes:
mysql> SHOW REPLICA STATUS\G;
mysql> SHOW REPLICA STATUS\G;Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf the replica server fails to start or connect, you can skip a certain number of events following the binary log file position displayed in the output of the
SHOW MASTER STATUScommand. For example, skip the first event from the defined position:mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;Copy to Clipboard Copied! Toggle word wrap Toggle overflow and try to start the replica server again.
Optional: Stop the replica thread in the replica server:
mysql> STOP REPLICA;
mysql> STOP REPLICA;Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.8.5. Verifying replication on a MySQL server Copy linkLink copied to clipboard!
You can verify that MySQL replication works correctly by creating test databases and checking replication status on source and replica servers.
Procedure
Create an example database on the source server:
mysql> CREATE DATABASE test_db_name;
mysql> CREATE DATABASE test_db_name;Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Verify that the
test_db_namedatabase replicates on the replica server. Display status information about the binary log files of the MySQL server by executing the following command on either of the source or replica servers:
mysql> SHOW MASTER STATUS;
mysql> SHOW MASTER STATUS;Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
Executed_Gtid_Setcolumn, which shows a set of GTIDs for transactions executed on the source, must not be empty.NoteThe same set of GTIDs is displayed in the
Executed_Gtid_Setrow when you use theSHOW REPLICA STATUSon the replica server.
3.9. Migrating to a RHEL 9 version of MySQL 8.0 Copy linkLink copied to clipboard!
RHEL 8 contains the MySQL 8.0, MariaDB 10.3, and MariaDB 10.5 implementations of a server from the MySQL databases family. RHEL 9 provides MySQL 8.0 and MariaDB 10.5.
This procedure describes migration from a RHEL 8 version of MySQL 8.0 to a RHEL 9 version of MySQL 8.0 using the mysql_upgrade utility. The mysql_upgrade utility is provided by the mysql-server package.
Prerequisites
- Before performing the upgrade, back up all your data stored in the MySQL databases. See Backing up MySQL data.
Procedure
Ensure that the
mysql-serverpackage is installed on the RHEL 9 system:dnf install mysql-server
# dnf install mysql-serverCopy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure that the
mysqldservice is not running on either of the source and target systems at the time of copying data:systemctl stop mysqld.service
# systemctl stop mysqld.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 an 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 Start the MySQL server on the target system:
systemctl start mysqld.service
# systemctl start mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note: In earlier versions of MySQL, the
mysql_upgradecommand was needed to check and repair internal tables. This is now done automatically when you start the server.
3.10. Upgrading from MySQL 8.0 to MySQL 8.4 Copy linkLink copied to clipboard!
If you currently use MySQL 8.0 on Red Hat Enterprise Linux 9 and require features that are only available in a newer version, you can upgrade to MySQL 8.4. RHEL 9.6 and later provides MySQL 8.4 as an alternative Application Stream.
3.10.1. Notable differences between MySQL 8.0 and MySQL 8.4 Copy linkLink copied to clipboard!
MySQL 8.4 introduces significant changes including enhanced password management, improved authentication security, backup compatibility features, and removal of deprecated utilities.
Significant changes between MySQL 8.0 and MySQL 8.4 include:
- Enhancements to password management: Administrators can now enforce password expiration, lengths, strength, reuse policy, and other password-related settings.
-
Authentication: The
caching_sha2_passwordplugin is now the default and replaces themysql_native_passwordplugin to increase the security. -
Backup Compatibility: The
mysqldumputility now provides an--output-as-versionoption which enables logical backups to be compatible with older MySQL versions. -
EXPLAIN: This statement can now display results in JSON format. Deprecation and removal: The following features, which were previously deprecated have been removed:
-
The
mysqlpumputility -
The
mysql_native_passwordauthentication plugin -
The
mysql_upgradeutility
-
The
3.10.2. Upgrading from a RHEL 9 version of MySQL 8.0 to MySQL 8.4 Copy linkLink copied to clipboard!
You can upgrade your MySQL database from version 8.0 to 8.4 on RHEL 9 to access new features and improvements.
Prerequisites
- You run MySQL 8.0 on RHEL 9.
- You created a backup of the MySQL databases.
Procedure
Stop the
mysqldservice:systemctl stop mysqld.service
# systemctl stop mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow Switch from the non-modular MySQL 8.0 to the modular MySQL 8.4 stream:
dnf module switch-to mysql:8.4
# dnf module switch-to mysql:8.4Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Adjust the configuration so that files located in the
/etc/my.cnf.d/directory include only settings valid for MySQL 8.4. For details, see upstream documentation. Start the
mysqldservice:systemctl start mysqld.service
# systemctl start mysqld.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow When the service starts, MySQL automatically checks, repairs, and updates internal tables.
3.11. Developing MySQL client applications Copy linkLink copied to clipboard!
Red Hat recommends developing your MySQL client applications against the MariaDB client library. The communication protocol between client and server is compatible between MariaDB and MySQL. The MariaDB client library works for most common MySQL scenarios, with the exception of a limited number of features specific to the MySQL implementation.
The development files and programs necessary to build applications against the MariaDB client library 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.