MySQL SBR vs RBR

MySQLreplication

Assuming a mixed replication of statement based replication and row based replication in MySQL 5.5. How exactly does it work in a master-master scenario?

Does the master that receives the query execute and then write the statement to the binlog which is then relayed onto the slave for execution? Or does the master simply write what should be updated and then slave picks up the changes?

Also it states that in MySQL 5.5 SBR is the default but it switches over to RBR when needed. How do I know when it is needed? Is there a way to force it to use SBR for just one query UPDATE or INSERT?

Best Answer

There is nothing special about master/master replication in MySQL - it is bidirectional Master/Slave where each server is just a slave of the other server. Otherwise, it behaves exactly as a "regular" master/slave topology. There are some suggestions on how to safeguard this configuration from duplicate primary keys, such as setting an auto_increment_offset and auto_increment_increment, only write to one master (run active/passive), and making sure that log_slave_updates is set.

So, master/master is just master/slave in two directions. Your next question is about how replication works in general, for which you should read this: Replication Implementation Details.

In summary:

  1. A query executes on the master
  2. After successful execution, the either the query itself (SBR) or a binary representation of the row(s) affected (RBR) are written into the local binary log along with some metadata.
  3. On the slave, there is an I/O Thread that runs that connects to the master and streams the master's binary logs and writes them to a local file called the relay log.
  4. On the slave, the SQL thread reads the relay log and executes the statements contained (SBR)/applies the rows (RBR)

You can see both the I/O thread and the SQL thread active in SHOW SLAVE STATUS and also in the processlist.

In MySQL 5.5, the default is statement based replication. (See Replication Formats)

What you are referring to is Mixed Binary Logging Format which will ordinarily use SBR, but switch to RBR under a number of circumstances where SBR is not considered safe because there is potential for the statement to have different results on the master and the slave. Those are described clearly in the documentation.

Because SBR is the default, in order to use MIXED, you have to explicitly set it in my.cnf for a persistent change:

binlog_format = MIXED

or dynamically by executing

-- Set binary log format globally
mysql> SET GLOBAL binlog_format = MIXED;
-- set binary log format just for this session
mysql> SET SESSION binlog_format = MIXED;

If you want to set the binlog_format for a particular statement, you can by setting binlog_format in the session (as shown above) and then executing your statement. However, you have to have SUPER privilege in order to do that, which no application should have. Really, except for certain administrative tasks, there is almost no reason to do that and MIXED replication should be your choice.