MySQL中主从复制不同步?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL中主从复制不同步?相关的知识,希望对你有一定的参考价值。

在linux中mysql的主从复制中出现信息不同步问题该咋办?

我也遇到了这个问题,我是这样解决的:
第一show variables like '%server_id%';看看是不是没有设置成功
第二看看是不是数据库的版本不同,
参考技术A 1.网络的延迟
由于mysql主从复制是基于binlog的一种异步复制,通过网络传送binlog文件,理所当然网络延迟是主从不同步的绝大多数的原因,特别是跨机房的数据同步出现这种几率非常的大,所以做读写分离,注意从业务层进行前期设计。
2.主从两台机器的负载不一致
由于mysql主从复制是主数据库上面启动1个io线程,而从上面启动1个sql线程和1个io线程,当中任何一台机器的负载很高,忙不过来,导致其中的任何一个线程出现资源不足,都将出现主从不一致的情况。
3.max_allowed_packet设置不一致
主数据库上面设置的max_allowed_packet比从数据库大,当一个大的sql语句,能在主数据库上面执行完毕,从数据库上面设置过小,无法执行,导致的主从不一致。
4.key自增键开始的键值跟自增步长设置不一致引起的主从不一致。
5.mysql异常宕机情况下,如果未设置sync_binlog=1或者innodb_flush_log_at_trx_commit=1很有可能出现binlog或者relaylog文件出现损坏,导致主从不一致。
6.mysql本身的bug引起的主从不同步。
7.版本不一致,特别是高版本是主,低版本为从的情况下,主数据库上面支持的功能,从数据库上面不支持该功能。追问

我的这两个虚拟机都是克隆过来的,不存在不一致的问题

MySQL学习(二十四)主从同步-半同步

概述

MySQL 默认是异步复制,半复制是为了数据一致性,防止异步同步数据过程中,事务丢失。同步复制的话可以保证数据的一致性,数据不丢失可以回滚,但是响应慢,master 必须等待 slave 返回的ack响应才算完整地完成事务,而异步复制则有可能出现数据不一致的问题,半复制处于异步复制和同步复制的中间。
半复制只需要等待(默认)一台 slave 完成了写入就算完成事务请求了。

半复制过程

Semisynchronous replication can be used as an alternative to asynchronous replication:

- 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.

为了理解,以下列出和同步复制和异步复制的对比。

To understand what the “semi” in “semisynchronous replication” means, compare it with asynchronous and fully synchronous replication:

With asynchronous replication, the master writes events to its binary log and slaves request them when they are ready. There is no guarantee that any event will ever reach any slave.

With fully synchronous replication, when a master commits a transaction, all slaves also will have committed the transaction before the master returns to the session that performed the transaction. The drawback of this is that there might be a lot of delay to complete a transaction.

Semisynchronous replication falls between asynchronous and fully synchronous replication. The master waits only until at least one slave has received and logged the events. It does not wait for all slaves to acknowledge receipt, and it requires only receipt, not that the events have been fully executed and committed on the slave side.

参考资料 :


以上是关于MySQL中主从复制不同步?的主要内容,如果未能解决你的问题,请参考以下文章

Mysql主从(主从不同步解决办法,常见问题及解决办法,在线对mysql做主从复制)

Mysql -- 主从复制(主从同步)

MySQL的3节点主从同步复制方案

MySQL的3节点主从同步复制方案

MySQL学习(二十四)主从同步-半同步

MySQL学习(二十四)主从同步-半同步