Configuring binary log file position replication with an external source instance (original) (raw)

You can set up replication between an RDS for MySQL or MariaDB DB instance and a MySQL or MariaDB instance that is external to Amazon RDS using binary log file replication.

Topics

Before you begin

You can configure replication using the binary log file position of replicated transactions.

The permissions required to start replication on an Amazon RDS DB instance are restricted and not available to your Amazon RDS master user. Because of this, make sure that you use the Amazon RDS mysql.rds_set_external_master (RDS for MariaDB and RDS for MySQL major versions 8.0 and lower) or mysql.rds_set_external_source (RDS for MySQL major versions 8.4 and higher), and mysql.rds_start_replication commands to set up replication between your live database and your Amazon RDS database.

To set the binary logging format for a MySQL or MariaDB database, update thebinlog_format parameter. If your DB instance uses the default DB instance parameter group, create a new DB parameter group to modify thebinlog_format parameter. In MariaDB and MySQL 8.0 and lower versions,binlog_format defaults to MIXED. However, you can also setbinlog_format to ROW or STATEMENT if you need a specific binary log (binlog) format. Reboot your DB instance for the change to take effect. In MySQL 8.4 and higher versions, binlog_format defaults toROW.

For information about setting the binlog_format parameter, see Configuring RDS for MySQL binary logging. For information about the implications of different MySQL replication types, see Advantages and disadvantages of statement-based and row-based replication in the MySQL documentation.

Configuring binary log file position replication with an external source instance

Follow these guidelines when you set up an external source instance and a replica on Amazon RDS:

To configure binary log file replication with an external source instance
  1. Make the source MySQL or MariaDB instance read-only.
mysql> FLUSH TABLES WITH READ LOCK;  
mysql> SET GLOBAL read_only = ON;  
  1. Run the SHOW MASTER STATUS command on the source MySQL or MariaDB instance to determine the binlog location.
    You receive output similar to the following example.
File                        Position  
------------------------------------  
 mysql-bin-changelog.000031      107  
------------------------------------  
  1. Copy the database from the external instance to the Amazon RDS DB instance usingmysqldump. For very large databases, you might want to use the procedure in Importing data to an Amazon RDS for MySQL database with reduced downtime.
    For Linux, macOS, or Unix:
mysqldump --databases database_name \  
    --single-transaction \  
    --compress \  
    --order-by-primary \  
    -u local_user \  
    -plocal_password | mysql \  
        --host=hostname \  
        --port=3306 \  
        -u RDS_user_name \  
        -pRDS_password  

For Windows:

mysqldump --databases database_name ^  
    --single-transaction ^  
    --compress ^  
    --order-by-primary ^  
    -u local_user ^  
    -plocal_password | mysql ^  
        --host=hostname ^  
        --port=3306 ^  
        -u RDS_user_name ^  
        -pRDS_password  
Note

Make sure that there isn't a space between the -p option and the entered password.
To specify the host name, user name, port, and password to connect to your Amazon RDS DB instance, use the --host, --user (-u),--port and -p options in the mysql command. The host name is the Domain Name Service (DNS) name from the Amazon RDS DB instance endpoint, for examplemyinstance.123456789012.us-east-1.rds.amazonaws.com. You can find the endpoint value in the instance details in the AWS Management Console. 4. Make the source MySQL or MariaDB instance writeable again.

mysql> SET GLOBAL read_only = OFF;  
mysql> UNLOCK TABLES;  

For more information on making backups for use with replication, see the MySQL documentation. 5. In the AWS Management Console, add the IP address of the server that hosts the external database to the virtual private cloud (VPC) security group for the Amazon RDS DB instance. For more information on modifying a VPC security group, see Security groups for your VPC in the Amazon Virtual Private Cloud User Guide.
The IP address can change when the following conditions are met:

host db_instance_endpoint  

The host name is the DNS name from the Amazon RDS DB instance endpoint. 6. Using the client of your choice, connect to the external instance and create a user to use for replication. Use this account solely for replication and restrict it to your domain to improve security. The following is an example.

CREATE USER 'repl_user'@'mydomain.com' IDENTIFIED BY 'password';  
Note

Specify a password other than the prompt shown here as a security best practice. 7. For the external instance, grant REPLICATION CLIENT andREPLICATION SLAVE privileges to your replication user. For example, to grant the REPLICATION CLIENT and REPLICATION SLAVE privileges on all databases for the 'repl_user' user for your domain, issue the following command.

GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'repl_user'@'mydomain.com';  
  1. Make the Amazon RDS DB instance the replica. To do so, first connect to the Amazon RDS DB instance as the master user. Then identify the external MySQL or MariaDB database as the source instance by using the mysql.rds_set_external_source (RDS for MySQL major versions 8.4 and higher) or mysql.rds_set_external_master (RDS for MariaDB and RDS for MySQL major versions 8.0 and lower) command. Use the master log file name and master log position that you determined in step 2. The following commands are examples.
    MySQL 8.4
CALL mysql.rds_set_external_source ('mysourceserver.mydomain.com', 3306, 'repl_user', 'password', 'mysql-bin-changelog.000031', 107, 1);  

MariaDB and MySQL 8.0 and 5.7

CALL mysql.rds_set_external_master ('mymasterserver.mydomain.com', 3306, 'repl_user', 'password', 'mysql-bin-changelog.000031', 107, 1);  
  1. On the Amazon RDS DB instance, issue the mysql.rds_start_replication command to start replication.
CALL mysql.rds_start_replication;