3.6. Replicating MySQL
MySQL provides various configuration options for replication, ranging from basic to advanced. This section describes a transaction-based way to replicate in MySQL on freshly installed MySQL servers using global transaction identifiers (GTIDs). Using GTIDs simplifies transaction identification and consistency verification.
To set up replication in MySQL, you must:
If you want to use existing MySQL servers for replication, you must first synchronize data. See the upstream documentation for more information.
3.6.1. Configuring a MySQL source server
This section describes configuration options required for a MySQL source server to properly run and replicate all changes made on the database server.
Conditions préalables
- The source server is installed.
Procédure
Include the following options in the
/etc/my.cnf.d/mysql-server.cnf
file under the[mysqld]
section:bind-address=source_ip_adress
This option is required for connections made from replicas to the source.
server-id=id
The id must be unique.
log_bin=path_to_source_server_log
This 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=ON
This option enables global transaction identifiers (GTIDs) on the server.
enforce-gtid-consistency=ON
The server enforces GTID consistency by allowing execution of only statements that can be safely logged using a GTID.
Optional:
binlog_do_db=db_name
Use 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
Optional:
binlog_ignore_db=db_name
Use this option to exclude a specific database from replication.
Restart the
mysqld
service:# systemctl restart mysqld.service
3.6.2. Configuring a MySQL replica server
This section describes configuration options required for a MySQL replica server to ensure a successful replication.
Conditions préalables
- The replica server is installed.
Procédure
Include the following options in the
/etc/my.cnf.d/mysql-server.cnf
file under the[mysqld]
section:server-id=id
The id must be unique.
relay-log=path_to_replica_server_log
The relay log is a set of log files created by the MySQL replica server during replication.
log_bin=path_to_replica_sever_log
This 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=ON
This option enables global transaction identifiers (GTIDs) on the server.
enforce-gtid-consistency=ON
The server enforces GTID consistency by allowing execution of only statements that can be safely logged using a GTID.
log-replica-updates=ON
This option ensures that updates received from the source server are logged in the replica’s binary log.
skip-replica-start=ON
This option ensures that the replica server does not start the replication threads when the replica server starts.
Optional:
binlog_do_db=db_name
Use 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
Optional:
binlog_ignore_db=db_name
Use this option to exclude a specific database from replication.
Restart the
mysqld
service:# systemctl restart mysqld.service
3.6.3. Creating a replication user on the MySQL source server
You must create a replication user and grant this user permissions required for replication traffic. This procedure shows how to create a replication user with appropriate permissions. Execute these steps only on the source server.
Conditions préalables
- The source server is installed and configured as described in Configuring a MySQL source server.
Procédure
Create a replication user:
mysql> CREATE USER 'replication_user'@'replica_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';
Grant the user replication permissions:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'replica_server_ip';
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;
3.6.4. Connecting the replica server to the source server
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.
Conditions préalables
- 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.
Procédure
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_ip_address', -> SOURCE_USER='replication_user', -> SOURCE_PASSWORD='password', -> SOURCE_AUTO_POSITION=1;
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;
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 STATUS
command. For example, skip the first event from the defined position:mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
and try to start the replica server again.
Optional: Stop the replica thread in the replica server:
mysql> STOP REPLICA;
3.6.5. Verification steps
Create an example database on the source server:
mysql> CREATE DATABASE test_db_name;
-
Verify that the
test_db_name
database 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_Set
column, 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_Set
row when you use theSHOW SLAVE STATUS
on the replica server.