10bet网址
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr)- 42.0Mb
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

17.5.1.35 Replication and Transactions

Mixing transactional and nontransactional statements within the same transaction.In general, you should avoid transactions that update both transactional and nontransactional tables in a replication environment. You should also avoid using any statement that accesses both transactional (or temporary) and nontransactional tables and writes to any of them.

The server uses these rules for binary logging:

  • 如果最初的语句在事务非transactional, they are written to the binary log immediately. The remaining statements in the transaction are cached and not written to the binary log until the transaction is committed. (If the transaction is rolled back, the cached statements are written to the binary log only if they make nontransactional changes that cannot be rolled back. Otherwise, they are discarded.)

  • For statement-based logging, logging of nontransactional statements is affected by thebinlog_direct_non_transactional_updatessystem variable. When this variable isOFF(the default), logging is as just described. When this variable isON, logging occurs immediately for nontransactional statements occurring anywhere in the transaction (not just initial nontransactional statements). Other statements are kept in the transaction cache and logged when the transaction commits.binlog_direct_non_transactional_updateshas no effect for row-format or mixed-format binary logging.

Transactional, nontransactional, and mixed statements.To apply those rules, the server considers a statement nontransactional if it changes only nontransactional tables, and transactional if it changes only transactional tables. A statement that references both nontransactional and transactional tables and updatesanyof the tables involved is considered amixedstatement. Mixed statements, like transactional statements, are cached and logged when the transaction commits.

A mixed statement that updates a transactional table is considered unsafe if the statement also performs either of the following actions:

  • Updates or reads a temporary table

  • Reads a nontransactional table and the transaction isolation level is less than REPEATABLE_READ

A mixed statement following the update of a transactional table within a transaction is considered unsafe if it performs either of the following actions:

For more information, seeSection 17.2.1.3, “Determination of Safe and Unsafe Statements in Binary Logging”.

Note

A mixed statement is unrelated to mixed binary logging format.

In situations where transactions mix updates to transactional and nontransactional tables, the order of statements in the binary log is correct, and all needed statements are written to the binary log even in case of aROLLBACK. However, when a second connection updates the nontransactional table before the first connection transaction is complete, statements can be logged out of order because the second connection update is written immediately after it is performed, regardless of the state of the transaction being performed by the first connection.

Using different storage engines on source and replica.It is possible to replicate transactional tables on the source using nontransactional tables on the replica. For example, you can replicate anInnoDBsource table as aMyISAMreplica table. However, if you do this, there are problems if the replica is stopped in the middle of aBEGIN...COMMITblock because the replica restarts at the beginning of theBEGINblock.

It is also safe to replicate transactions fromMyISAMtables on the source to transactional tables, such as tables that use theInnoDBstorage engine, on the replica. In such cases, anAUTOCOMMIT=1statement issued on the source is replicated, thus enforcingAUTOCOMMITmode on the replica.

When the storage engine type of the replica is nontransactional, transactions on the source that mix updates of transactional and nontransactional tables should be avoided because they can cause inconsistency of the data between the source transactional table and the replica nontransactional table. That is, such transactions can lead to source storage engine-specific behavior with the possible effect of replication going out of synchrony. MySQL does not issue a warning about this, so extra care should be taken when replicating transactional tables from the source to nontransactional tables on the replicas.

Changing the binary logging format within transactions.Thebinlog_formatandbinlog_checksumsystem variables are read-only as long as a transaction is in progress.

Every transaction (including自动提交transactions) is recorded in the binary log as though it starts with aBEGINstatement, and ends with either aCOMMITor aROLLBACKstatement. This is even true for statements affecting tables that use a nontransactional storage engine (such asMyISAM).

Note

For restrictions that apply specifically to XA transactions, seeSection 13.3.8.3, “Restrictions on XA Transactions”.