

Working of MySQL Replication Filters When Using Statement-based and Row-based Re...
source link: https://www.percona.com/blog/mysql-replication-filters-when-using-statement-based-and-row-based-replication/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Working of MySQL Replication Filters When Using Statement-based and Row-based Replication

A couple of days ago I was creating an index on the source and when I checked the replica side it was not replicated, so I just wanted to explain how the replication filter may increase the complexity of your DBA operations.
Replication occurs by reading events from the binary log of the source and then executing them on the replica. The events in the binary log are recorded in different formats, depending on the type of event. These formats are determined by the binary logging format used when the events were initially recorded on the source. The relationship between the binary logging formats and the terminology used during replication is as follows:
When using statement-based binary logging, the source writes SQL statements to the binary log. Replication of the source to the replica is performed by executing these SQL statements on the replica. This is known as statement-based replication (SBR), and it corresponds to the statement-based binary logging format in MySQL.
When using row-based binary logging, the source writes events to the binary log that shows how individual rows in tables are changed. Replication of the source to the replica is done by copying these events, representing the changes in the table rows, to the replica. This is known as row-based replication (RBR) and it corresponds to the row-based binary logging format in MySQL.
Row-based logging is the default method.
You can also configure MySQL to use a mix of both statement-based and row-based logging, depending on which is most appropriate for the change to be logged. This is called mixed-format binary logging.
MySQL supports two types of replication filters; those that apply at the database level and those that apply and the table level:
There are filters at the database level, known as binlog-do-db and binlog-ignore-db, that control what is included in the binary log. However, it’s important to note that if events are filtered out of the binary log, they can no longer be used for point-in-time recovery that involves those tables. It’s therefore recommended to use filters that apply to the replica rather than the binary log.
When using ROW-based replication, the filter applies to the specific table the change is made on. However, when using STATEMENT-based replication, the database-level filters apply to the default database. And when using table-level filters with STATEMENT-based replication, the filter applies to the table change.
Mixing database and table-level filters create more complex rules. First, the database-level filters are checked, and if the update qualifies, the table-level filters are also checked. This can result in different outcomes for STATEMENT-based and ROW-based replication when using non-default databases. Additionally, using the MIXED format for binary logging can also cause issues, as a small change in the query can change whether the statement is logged in the STATEMENT- or ROW-based format. For this reason, it’s safer to use table-level replication filters rather than database-level filters.
See Determination of Safe and Unsafe Statements in Binary Logging for details of how it is decided whether a statement is logged using the STATEMENT-based or ROW-based format when using MIXED mode replication.
Examples
All the following examples use the following schema:
mysql> CREATE DATABASE databasefilter; Query OK, 1 row affected (0.02 sec) mysql> CREATE DATABASE databasewithoutfilter; Query OK, 1 row affected (0.00 sec) mysql> use databasefilter; Database changed mysql> CREATE TABLE t1 (uid int unsigned NOT NULL PRIMARY KEY) ENGINE=InnoDB; Query OK, 0 rows affected (0.06 sec) mysql> CREATE TABLE t2 (uid int unsigned NOT NULL PRIMARY KEY) ENGINE=InnoDB; Query OK, 0 rows affected (0.04 sec) |
Example one
Replication filter:
replicate-wild-do-table = databasefilter.t1\_% |
Statements:
use databasefilter; INSERT INTO t1 VALUES (1); |
Replicates?
Statement-based replication: Yes
Row-based replication: Yes
The replication filter is set to “replicate-wild-do-table = databasefilter.t1_%”, meaning that any table in the “databasefilter” database with a name starting with “t1_” will be replicated. If the statement “use databasefilter; INSERT INTO t1 VALUES (1);” is executed, it will be replicated using both statement-based and row-based replication methods.
Example two
Replication filter:
replicate-wild-do-table = databasefilter.t2\_% |
Statements:
use databasefilter; INSERT INTO t1 VALUES (1); |
Replicates?
Statement-based replication: No
Row-based replication: No
The replication filter “replicate-wild-do-table = databasefilter.t2_%” is set, which means that only tables that match the pattern “databasefilter.t2_%” will be replicated. When the statement “use databasefilter;” is executed, the current database will be set to “databasefilter”.
However, when the statement “INSERT INTO t1 VALUES (1);” is executed, it will not replicate in either statement-based or row-based replication because the table “t1” does not match the pattern specified in the replication filter.
Example three
Replication filter:
replicate-do-db = databasefilter |
Statements:
use databasefilter; INSERT INTO t1 VALUES (1); |
Replicates?
Statement-based replication: Yes
Row-based replication: Yes
The replication filter “replicate-do-db = databasefilter” is set, which means that only statements executed in the “databasefilter” database will be replicated.When the statement “use databasefilter;” is executed, the current database will be set to “databasefilter”.
After that, when the statement “INSERT INTO t1 VALUES (1);” is executed, it will replicate in both statement-based and row-based replication because it is executed in the “databasefilter” database that matches the pattern specified in the replication filter.
Example four
Replication filter:
replicate-do-db = databasefilter |
Statements:
use databasewithoutfilter; INSERT INTO databasefilter.t1 VALUES (1); |
Replicates?
Statement-based replication: No
Row-based replication: Yes
The replication filter “replicate-do-db = databasefilter” is set, which means that only statements executed in the “databasefilter” database will be replicated. When the statement “use databasewithoutfilter;” is executed, the current database will be set to “databasewithoutfilter”, which does not match the pattern specified in the replication filter. However, when the statement “INSERT INTO databasefilter.t1 VALUES (1);” is executed, it will replicate in row-based replication but not in statement-based replication.
The reason for this is that statement-based replication replicates the entire statement, including the “use databasewithoutfilter” statement, which does not match the pattern specified in the replication filter. But in row-based replication, it only replicates the actual data change and it does not care about the current database.
Conclusion
MySQL replication filters can be used to control which events are replicated from the source to the replica. These filters can be applied at the database level or the table level and can increase the complexity of DBA operations. It is important to note that if events are filtered out of the binary log, they can no longer be used for point-in-time recovery that involves those tables. When using ROW-based replication, the filter applies to the specific table the change is made on. However, when using STATEMENT-based replication, the database-level filters apply to the default database. Mixing database and table-level filters create more complex rules and can cause issues when using MIXED format for binary logging.
Percona Distribution for MySQL is the most complete, stable, scalable, and secure, open-source MySQL solution available, delivering enterprise-grade database environments for your most critical business applications… and it’s free to use!
Recommend
-
41
You may have heard about the term “failover” in the context of MySQL replication. Maybe you wondered what it is as you...
-
53
MySQL 8.0.16 has been released last Thursday. In it, you can find some new replication features. Here is a quick summary. Follow-up blog p...
-
71
MySQL 8.0.17 is out. In addition to fixing a few bugs here and there, we also have a couple of new replication features that I would like to...
-
58
While we’ve had MySQL Group Replication support in ProxySQL since version 1.3 (native as of v1.4), development has continued in subsequent vers...
-
25
This blog discusses a few concepts about Docker and how we can use it to run a MySQL async replication environment. Docker is a tool designed t...
-
19
Executing Multiple Statement MySQL Scripts in Java JDBC You're running your Integration Tests, populate your MySQL database with a multi-statement MySQL Script and see the following response: You have an error in...
-
9
php mysql prepared statement bind param error advertisements i have been stuck on this error. Appreciate any help on this:
-
6
How to configure GTID-based replication on MySQL servers Using Global Transaction Identifiers for data replication makes rol...
-
4
Is MySQL Statement-Based / Mixed Replication Really Safe? ...
-
5
Microsoft Outlook Spam Filters are not working properly
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK