MySQL Multi Source Master Slave Replication using GTID
Prerequisite
Master Source 1- Channel name: DEVELOPMENT
- Database: mydb1
- Enable Replication For Tables
- testing1
- testing2
- testing3
- Mysql Server Running: 192.168.0.201:3307
Master Source 2
- Channel name: PRODUCTION
- Database: mydb1
- Enable Replication For Tables
- testing4
- testing5
- testing6
- Mysql Server Running: 192.168.0.201:3308
Slave
- Mysql Server Running: 192.168.0.209:3306
Configuration of masters(192.168.0.201:3307/192.168.0.201:3308) and slave (192.168.0.209:3306)
my.cnf config for master (192.168.0.201:3307)
my.cnf config for slave (192.168.0.209:3306)
Steps for taking a dump and restoring it
Run the following commands in both masters 192.168.0.201:3307 and 192.168.0.201:3308
- Dump the database on the master: mysqldump --set-gtid-purged=OFF -u root -p mydb1 > backup.sql
- Restore the database on the slave:
mysql -u root -p mydb1 < backup.sql
Step to enable replication on the master
Run the following commands in both masters 192.168.0.201:3307 and 192.168.0.201:3308
- Check GTID is ON: SHOW VARIABLES LIKE 'gtid_mode';
- Create a replication user on the master: CREATE USER 'replication_user'@'192.168.0.209' IDENTIFIED WITH mysql_native_password BY 'password';
- Grant replication privileges to the user: GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'192.168.0.209'; FLUSH PRIVILEGES;
- Show the master status: SHOW MASTER STATUS\G;
- Disable WRITE operation on master till we enable replication: SET @@GLOBAL.read_only = ON;
Steps to enable replication on the slave
Run the following commands in the slave 192.168.0.209:3306 server node.
- Check GTID is ON: SHOW VARIABLES LIKE 'gtid_mode';
- Stop and reset the slave on the slave server:
STOP REPLICA FOR CHANNEL "DEVELOPMENT";RESET REPLICA FOR CHANNEL "DEVELOPMENT"; - Add GTID on the slave for channel DEVELOPMENT: SET GLOBAL gtid_purged='ccb0d141-dc54-11ee-923c-0242c0a8b005:1-33'; <!--- Should be same as master 192.168.0.201:3307 --->
- Change the master settings on the slave for channel DEVELOPMENT: CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.0.201', SOURCE_PORT=3307, SOURCE_USER='replication_user', SOURCE_PASSWORD='password', SOURCE_AUTO_POSITION=1 FOR CHANNEL "DEVELOPMENT";
- Create table filters for replication for DEVELOPMENT channel: CHANGE REPLICATION FILTER REPLICATE_DO_TABLE = (mydb1.testing1, mydb1.testing2, mydb1.testing3) FOR CHANNEL "DEVELOPMENT";
- Start the slave for the specified channel: START REPLICA FOR CHANNEL "DEVELOPMENT";
- Show the slave status for the specified channel: SHOW REPLICA STATUS FOR CHANNEL "DEVELOPMENT"\G;
- Add GTID on the slave for channel PRODUCTION: SET GLOBAL gtid_purged='ccb0d141-dc54-11ee-923c-0242c0a8b005:1-33,180c0583-e1e0-11ee-84fa-0242c0a84003:1-107'; <!--- Add another GTID with existing and should be same as master 192.168.0.201:3308 --->
- Change the master settings on the slave for channel PRODUCTION: CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.0.201', SOURCE_PORT=3308, SOURCE_USER='replication_user', SOURCE_PASSWORD='password', SOURCE_AUTO_POSITION=1 FOR CHANNEL "PRODUCTION";
- Create table filters for replication for the PRODUCTION channel: CHANGE REPLICATION FILTER REPLICATE_DO_TABLE = (mydb1.testing4, mydb1.testing5, mydb1.testing6) FOR CHANNEL "PRODUCTION";
- Start the slave for the specified channel: START REPLICA FOR CHANNEL "PRODUCTION";
- Show the slave status for the specified channel: SHOW REPLICA STATUS FOR CHANNEL "PRODUCTION"\G;
- Disable Read Only permission on both master: SET @@GLOBAL.read_only = OFF;
To verify replication working
- Check masters GTID present in slave: show global variables like 'gtid_executed'; <!---Should be same as master--->
- Get detailed information about the replication applier status, including GTID-related information.
SELECT * FROM performance_schema.replication_applier_status_by_worker; - Check replication_user is present in the master: SELECT User, Host FROM mysql.user WHERE User = 'replication_user';
Thank you! 😀 Hope you like my bog post.
Under "Steps to enable replication on the slave" step, you have run "SET GLOBAL gtid_purged=" in step 3 and step8, will step 8 won't remove the value which has been set in step 3?
ReplyDeleteYou have mentioned "Add GTID on the slave for channel DEVELOPMENT" but the command "SET GLOBAL gtid_purged=" don't have any reference of DEVELOPMENT channel.
Hey Anant,
DeleteQuestion: will step 8 won't remove the value which has been set in step 3?
You are right!, I corrected the query in step 8. In step 8 we are adding another GTID of master server 192.168.0.201:3308.
SET GLOBAL gtid_purged='ccb0d141-dc54-11ee-923c-0242c0a8b005:1-33,180c0583-e1e0-11ee-84fa-0242c0a84003:1-107';
After Step 3 If we run below command will create channel DEVELOPMENT if not exist. This is mentioned in step 4
CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.0.201', SOURCE_PORT=3307, SOURCE_USER='replication_user', SOURCE_PASSWORD='password', SOURCE_AUTO_POSITION=1 FOR CHANNEL "DEVELOPMENT";