MySQL 5.6 5.7半同步的区别

1,967 total views, 2 views today

MySQL 5.6 主库执行事务后先提交,然后等待至少一个从库确认events写入到relay log并刷入到磁盘。但是,如果从库events并没有写成功,主库提交的事务并不回滚,主从不能保证一致性,此时主库比从库数据新。

MySQL 5.6半同步复制,主库先提交,然后等待从库relay log写入成功

 

MySQL 5.7 主库执行事务,要等待至少一个从库确认events写入relay log并刷入到磁盘后,再提交。这样就确保了主库、从库数据的一致性。因此,5.7的半同步也称为增强半同步(loose-less semi-sync replication)。

但是也有漏洞,比如从库commit成功,返回ACK给主库后,主库提交之前,主库宕机,此时从库数据反而比主库新。

MySQL 5.7 半同步复制,主库等待从库relay log写入成功后才提交,减少了数据丢失风险

 
MySQL 5.7
A slave indicates whether it is semisynchronous-capable when it connects to the master.

If semisynchronous replication is enabled on the master side and there is at least one semisynchronous slave, a thread that performs a transaction commit on the master blocks and waits until at least one semisynchronous slave acknowledges that it has received all events for the transaction, or until a timeout occurs.

The slave acknowledges receipt of a transaction’s events only after the events have been written to its relay log and flushed to disk.

If a timeout occurs without any slave having acknowledged the transaction, the master reverts to asynchronous replication. When at least one semisynchronous slave catches up, the master returns to semisynchronous replication.

Semisynchronous replication must be enabled on both the master and slave sides. If semisynchronous replication is disabled on the master, or enabled on the master but on no slaves, the master uses asynchronous replication.

 

MySQL 5.6
A slave indicates whether it is semisynchronous-capable when it connects to the master.

If semisynchronous replication is enabled on the master side and there is at least one semisynchronous slave, a thread that performs a transaction commit on the master blocks after the commit is done and waits until at least one semisynchronous slave acknowledges that it has received all events for the transaction, or until a timeout occurs.

The slave acknowledges receipt of a transaction’s events only after the events have been written to its relay log and flushed to disk.

If a timeout occurs without any slave having acknowledged the transaction, the master reverts to asynchronous replication. When at least one semisynchronous slave catches up, the master returns to semisynchronous replication.

Semisynchronous replication must be enabled on both the master and slave sides. If semisynchronous replication is disabled on the master, or enabled on the master but on no slaves, the master uses asynchronous replication.

发表评论

必填项已用*标注