2.8. Replicating MySQL with TLS encryption
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.
2.8.1. Configuring a MySQL source server 复制链接链接已复制到粘贴板!
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.
重要The 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 by 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_name3Optional:
binlog_ignore_db=db_nameUse this option to exclude a specific database from replication.
Restart the
mysqldservice:# systemctl restart mysqld.service
2.8.2. Configuring a MySQL replica server 复制链接链接已复制到粘贴板!
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.
重要The 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 by 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_name3Optional:
binlog_ignore_db=db_nameUse this option to exclude a specific database from replication.
Restart the
mysqldservice:# systemctl restart mysqld.service
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';Grant the user replication permissions:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'replica_server_hostname';*Reload the grant tables in the MySQL database:
mysql> FLUSH PRIVILEGES;Set the source server to read-only state:
mysql> SET @@GLOBAL.read_only = ON;
You must configure the replica server with source server credentials and start replication to establish a connection between the MySQL replica and source servers.
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;Configure the replication source:
mysql> CHANGE REPLICATION SOURCE TO SOURCE_HOST='source_hostname', SOURCE_USER='replication_user', SOURCE_PASSWORD='password', SOURCE_AUTO_POSITION=1, SOURCE_SSL=1, SOURCE_SSL_CA='path_to_ca_on_source', SOURCE_SSL_CAPATH='path_to_directory_with_certificates', SOURCE_SSL_CERT='path_to_source_certificate', SOURCE_SSL_KEY='path_to_source_key';Start the replica thread in the MySQL replica server:
mysql> START REPLICA;Unset the read-only state on both the source and replica servers:
mysql> SET @@GLOBAL.read_only = OFF;Optional: Inspect the status of the replica server for debugging purposes:
mysql> SHOW REPLICA STATUS\G;注意If 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;Afterwards, try to start the replica server again.
Optional: Stop the replica thread in the replica server:
mysql> STOP REPLICA;
2.8.5. Verifying replication on a MySQL server 复制链接链接已复制到粘贴板!
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;-
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;The
Executed_Gtid_Setcolumn, which shows a set of GTIDs for transactions executed on the source, must not be empty.注意The same set of GTIDs is displayed in the
Executed_Gtid_Setrow when you use theSHOW REPLICA STATUSon the replica server.