MySQL/MariaDB replication filter is an important feature when we need to replicate only certain databases or tables. Having this configuration option change dynamically is really convenient, but in this article, we’ll note that some replication filters are not dynamic, and you should be aware of that.

The use case here is to replicate one database from primary to a replica under a different name. That is, to replicate the database named “db_primary” from primary to “db_replica” on a replica MariaDB server.

Here we have db_primary and db_replica as two MariaDB servers. Let’s begin.

On the primary, I created a database named “db_primary”, created a table, and inserted a single record.

On the replica, created a database “db_replica”.

I then used mysqldump to export the database without the create-db command so I could easily import it into a replica database named “db_replica”.

The next step is to set up replication filtering and rewrite. While doing that, I noticed that CHANGE REPLICATION FILTER commands don’t work in MariaDB 10.5 but it works with MySQL.

As per the MariaDB docs, you have to use SET GLOBAL to set up these variables dynamically.

However, while doing so, I noticed that there is no such variable available to set dynamically.

Thus, to configure this database rewrite feature, the replicate_rewrite_db parameter has to be changed in the configuration file, and MariaDB on the replica server has to be restarted. I also added replicate_do_db=”db_primary” in my.cnf to persist it. 

After the MariaDB restart, I set up the replication between the MariaDB primary and replica instance.

Then I inserted a record into the MariaDB primary instance.

Replicate-rewrite-db problem

However, that did not get replicated to the MariaDB replica instance.

This is expected behavior because –replicate-* replication filtering rules are applied locally on the replica instance and not from the primary. For such filtering rules SQL thread would look for the database name specified in –replicate-do-db and restrict the replication to that particular database (in this case, db_replica). And because of the same reason replication did not fail here on replica.

This can be verified from the relay log entry on the replica instance.

Our configuration of replicate_do_db=”db_primary” was wrong. So I changed the replicate_do_db = “db_replica” on the replica instance.

The next step, reconfigure the replication on the instance, insert a new record, and start replication.

And now a new record is inserted on the replica instance.

Inserted one more record on the primary, and it got replicated successfully on the replica.

Will a database named “db_primary2” replicate?

I tested one more thing: I created a new database on the primary to make sure that replication filtering works as expected on the replica, and neither this new database gets replicated nor breaks the replication on the replica.

On replica, there were no issues because replication filtering worked as expected.

As per the MariaDB document, replicate_rewrite_db option can not be set dynamically until MariaDB 10.11. So I tested it for MariaDB 10.11 and noticed that it is available to be set dynamically and visible under global variables as well as in show slave output.

In conclusion, when a database with a different name needs to be replicated on the replica server in MariaDB versions prior to 10.11, the inability to dynamically set the replicate-rewrite-db parameter presents a challenge. This constraint necessitates careful planning, as altering database replication configurations requires restarting the MariaDB service.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Georgi Sotirov

The problem with replicate_do_db is understandable, because rewrite-db filter is applied before any other replication filters.