10bet网址
MySQL 8.0再保险ference Manual
Related Documentation Download this Manual
PDF (US Ltr)- 41.9Mb
PDF (A4)- 42.0Mb
Man Pages (TGZ)- 266.3Kb
Man Pages (Zip)- 376.1Kb
Info (Gzip)- 4.0Mb
Info (Zip)- 4.0Mb
Excerpts from this Manual

5.4.4.2 Setting The Binary Log Format

You can select the binary logging format explicitly by starting the MySQL server with--binlog-format=type. The supported values fortypeare:

  • STATEMENTcauses logging to be statement based.

  • ROWcauses logging to be row based. This is the default.

  • MIXEDcauses logging to use mixed format.

Setting the binary logging format does not activate binary logging for the server. The setting only takes effect when binary logging is enabled on the server, which is the case when thelog_binsystem variable is set toON. From MySQL 8.0, binary logging is enabled by default, and is only disabled if you specify the--skip-log-binor--disable-log-binoption at startup.

The logging format also can be switched at runtime, although note that there are a number of situations in which you cannot do this, as discussed later in this section. Set the global value of thebinlog_formatsystem variable to specify the format for clients that connect subsequent to the change:

mysql> SET GLOBAL binlog_format = 'STATEMENT'; mysql> SET GLOBAL binlog_format = 'ROW'; mysql> SET GLOBAL binlog_format = 'MIXED';

An individual client can control the logging format for its own statements by setting the session value ofbinlog_format:

mysql> SET SESSION binlog_format = 'STATEMENT'; mysql> SET SESSION binlog_format = 'ROW'; mysql> SET SESSION binlog_format = 'MIXED';

Changing the globalbinlog_formatvalue requires privileges sufficient to set global system variables. Changing the sessionbinlog_formatvalue requires privileges sufficient to set restricted session system variables. SeeSection 5.1.9.1, “System Variable Privileges”.

There are several reasons why a client might want to set binary logging on a per-session basis:

  • A session that makes many small changes to the database might want to use row-based logging.

  • A session that performs updates that match many rows in theWHEREclause might want to use statement-based logging because it is more efficient to log a few statements than many rows.

  • Some statements require a lot of execution time on the source, but result in just a few rows being modified. It might therefore be beneficial to replicate them using row-based logging.

There are exceptions when you cannot switch the replication format at runtime:

  • The replication format cannot be changed from within a stored function or a trigger.

  • If theNDBstorage engine is enabled.

  • 如果一个会话打开临时表,replication format cannot be changed for the session (SET @@SESSION.binlog_format).

  • If any replication channel has open temporary tables, the replication format cannot be changed globally (SET @@GLOBAL.binlog_formatorSET @@PERSIST.binlog_format).

  • If any replication channel applier thread is currently running, the replication format cannot be changed globally (SET @@GLOBAL.binlog_formatorSET @@PERSIST.binlog_format).

Trying to switch the replication format in any of these cases (or attempting to set the current replication format) results in an error. You can, however, usePERSIST_ONLY(SET @@PERSIST_ONLY.binlog_format) to change the replication format at any time, because this action does not modify the runtime global system variable value, and takes effect only after a server restart.

Switching the replication format at runtime is not recommended when any temporary tables exist, because temporary tables are logged only when using statement-based replication, whereas with row-based replication and mixed replication, they are not logged.

Switching the replication format while replication is ongoing can also cause issues. Each MySQL Server can set its own and only its own binary logging format (true whetherbinlog_formatis set with global or session scope). This means that changing the logging format on a replication source server does not cause a replica to change its logging format to match. When usingSTATEMENTmode, thebinlog_formatsystem variable is not replicated. When usingMIXEDorROWlogging mode, it is replicated but is ignored by the replica.

A replica is not able to convert binary log entries received inROWlogging format toSTATEMENTformat for use in its own binary log. The replica must therefore useROWorMIXEDformat if the source does. Changing the binary logging format on the source fromSTATEMENTtoROWorMIXEDwhile replication is ongoing to a replica withSTATEMENTformat can cause replication to fail with errors such asError executing row event: 'Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.'Changing the binary logging format on the replica toSTATEMENT当源格式仍在使用MIXEDorROWformat also causes the same type of replication failure. To change the format safely, you must stop replication and ensure that the same change is made on both the source and the replica.

If you are usingInnoDBtables and the transaction isolation level isREAD COMMITTEDorREAD UNCOMMITTED, only row-based logging can be used. It ispossibleto change the logging format toSTATEMENT, but doing so at runtime leads very rapidly to errors becauseInnoDBcan no longer perform inserts.

With the binary log format set toROW, many changes are written to the binary log using the row-based format. Some changes, however, still use the statement-based format. Examples include all DDL (data definition language) statements such asCREATE TABLE,ALTER TABLE, orDROP TABLE.

When row-based binary logging is used, thebinlog_row_event_max_sizesystem variable and its corresponding startup option--binlog-row-event-max-sizeset a soft limit on the maximum size of row events. The default value is 8192 bytes, and the value can only be changed at server startup. Where possible, rows stored in the binary log are grouped into events with a size not exceeding the value of this setting. If an event cannot be split, the maximum size can be exceeded.

The--binlog-row-event-max-sizeoption is available for servers that are capable of row-based replication. Rows are stored into the binary log in chunks having a size in bytes not exceeding the value of this option. The value must be a multiple of 256. The default value is 8192.

Warning

When usingstatement-based loggingfor replication, it is possible for the data on the source and replica to become different if a statement is designed in such a way that the data modification isnondeterministic; that is, it is left up to the query optimizer. In general, this is not a good practice even outside of replication. For a detailed explanation of this issue, seeSection B.3.7, “Known Issues in MySQL”.