简体   繁体   English

事务和主+从复制

[英]Transactions and Master + Slave Replication

I'm looking to clear up a bit of confusion.我想澄清一些困惑。 I have a master + slaves setup.我有一个主+从设置。 I have one master, and 3 slaves.我有一个主人,三个奴隶。 All writes ( INSERT|UPDATE|DELETE ) go the master.所有写入( INSERT|UPDATE|DELETE )都归主。 All reads ( SELECT ) go to one of the slaves, which is chosen at random.所有读取( SELECT )都转到随机选择的其中一个从属设备。 All of my tables are using the InnoDB storage engine.我所有的表都使用 InnoDB 存储引擎。

I'm curious how MySQL/InnoDB handles transactions in this setup.我很好奇 MySQL/InnoDB 在这个设置中是如何处理事务的。 If MySQL writes each change within the transaction to the binlog, then all should be fine.如果 MySQL 将事务中的每个更改都写入 binlog,那么一切都应该没问题。 However I can see there being some big problems if the binlog isn't written to until the transaction is commited.但是,如果在提交事务之前不写入 binlog,我可以看到存在一些大问题。

Can anyone explain what's going on within MySQL during transaction with replication in place?谁能解释在复制到位的事务期间 MySQL 中发生了什么?

According to this , the slaves can only see changes after the transaction is committed. 根据这一点 ,从属只能在事务提交后看到更改。

Transactions that fail on the master do not affect replication at all. 在主服务器上失败的事务根本不会影响复制。 MySQL replication is based on the binary log where MySQL writes SQL statements that modify data. MySQL复制基于二进制日志,MySQL编写修改数据的SQL语句。 A transaction that fails (for example, because of a foreign key violation, or because it is rolled back) is not written to the binary log, so it is not sent to slaves. 失败的事务(例如,由于外键冲突,或因为它被回滚)不会写入二进制日志,因此不会发送到从属服务器。

And this confirms: 证实了:

The binary log is crash-safe. 二进制日志是崩溃安全的。 Only complete events or transactions are logged or read back. 仅记录或回读完整的事件或事务。

Generally, if a transaction isn't written to the binary log until it is committed. 通常,如果事务在提交之前未写入二进制日志。 Assuming the table is transactional, all the statements in the transaction are grouped together into one BEGIN \\ COMMIT statement. 假设该表是事务性的,则事务中的所有语句都组合在一起成为一个BEGIN \\ COMMIT语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM