Multi-source replication in MySQL 8 allows you to replicate data from multiple source databases into a single destination database. It’s a powerful feature for consolidating data from different sources. To enable multi-source replication, make sure you have MySQL 8 installed and configured. Then, open your MySQL configuration file
This involves setting up two master databases, MASTER1 and MASTER2, and a slave database, SLAVE, to replicate data from both masters.
MASTER1,MASTER2 Configuration:
STEP1: Edit MySQL Configuration File
vi /etc/my.cnf
STEP2: Add the following lines
server-id=1
log_bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_do_db=medical
server-id=2
log_bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_do_db=medical
STEP3: Restart MySQL Service
systemctl restart mysqld
STEP4: Access MySQL Console
mysql -uroot -pTest@123
STEP5: Create Database and User
CREATE DATABASE medical;
CREATE USER 'gtiduser'@'%' IDENTIFIED WITH mysql_native_password BY 'Meena@123!';
GRANT REPLICATION SLAVE ON *.* TO 'gtiduser'@'%';
FLUSH PRIVILEGES;
STEP6: Enable Read-Only Mode and Get Master Status
SET @@GLOBAL.read_only = ON;
STEP7: master status
SHOW MASTER STATUS\G
SLAVE Configuration:
STEP1: Edit MySQL Configuration File
vi /etc/my.cnf
STEP2: Add the following lines
server-id=3
log_bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency=ON
STEP3: Restart MySQL Service
systemctl restart mysqld
STEP4: Access MySQL Console
mysql -uroot -pTest@123
STEP5: Create Databases
CREATE DATABASE medical;
CREATE DATABASE ecom;
STEP6: Enable Read-Only Mode
SET @@GLOBAL.read_only = ON;
EXIT;
STEP7 : Install Telnet and Test Connectivity
yum install -y telnet
telnet -x 3.81.74.123 3306
telnet -x 3.89.69.142 3306
STEP8: Access MySQL Console and Configure Replication
mysql -uroot -pTest@123
STEP9 : Insert the master IP
CHANGE MASTER TO
MASTER_HOST='3.81.74.123',
MASTER_USER='gtiduser',
MASTER_PORT=3306,
MASTER_PASSWORD='Meena@123!',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=835 FOR CHANNEL 'channel1';
CHANGE MASTER TO
MASTER_HOST='3.89.69.142',
MASTER_USER='gtiduser',
MASTER_PORT=3306,
MASTER_PASSWORD='Meena@123!',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=835 FOR CHANNEL 'channel2';
STEP10 : Start Slave
START SLAVE;
STEP11 : Replication for slave status
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 3.81.74.123
Master_User: gtiduser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 835
Relay_Log_File: ip-172-31-47-144-relay-bin-channel1.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 835
Relay_Log_Space: 556
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: a9e33cdd-41aa-11ee-b596-0ecd4152823f
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: ad5203cd-41aa-11ee-a752-0e9982aff945:1-2
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: channel1
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
*************************** 2. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 3.89.69.142
Master_User: gtiduser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 835
Relay_Log_File: ip-172-31-47-144-relay-bin-channel2.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 835
Relay_Log_Space: 556
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: ab38097d-41aa-11ee-b07d-0ed011794589
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: ad5203cd-41aa-11ee-a752-0e9982aff945:1-2
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: channel2
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
2 rows in set, 1 warning (0.00 sec)
mysql>
mysql>
mysql>
mysql>
By following these steps, you will have successfully set up multi-source replication in MySQL. The slave database will replicate data from both MASTER1 and MASTER2, allowing for a more flexible and redundant data replication setup.