• Mongodb数据同步采坑


    从mongodb2.0开始支持链式复制,并且默认是开启的,是根据second节点之间的ping time和网络距离进行选择那个second作为数据的同步节点,链式复制的优点:可以减少master的资源消耗,减少负载。缺点:节点之间同步数据本来就不可避免会有数据的延迟,执行链式复制的过程会使这个时间增大,该second节点的数据就会比其他的更落后于master,所以在读取数据的时候就会有一些问题,比如读取数据读不到,后端服务就会抛错,导致用户能感知到,非常不好,当然可以通过降低数据延迟来缓解,保证节点见的网络带宽流畅、io等。

    方案

    了解了副本集之间的复制方式,接下来就开始着手解决这个数据延迟的问题,从官网来看有两种方式:使用rs.syncFrom()设置同步源、禁用掉链式复制

    1、rs.syncFrom

    官网对于这个命令的介绍:

    Provides a wrapper around the replSetSyncFrom, which allows administrators to temporarily override the default sync target for the current member. Specify the name of the member you want to replicate from in the form of [hostname]:[port].

    Changed in version 3.2: MongoDB 3.2 replica set members with vote cannot sync from members with 0votes

    >rs.syncFrom("myHost:27017");

    2、禁用掉链式复制

    o disable chained replication, set the settings.chainingAllowed field in Replica Set Configuration to false.

    If chained replication is disabled, you still can use replSetSyncFrom to specify that a secondary replicates from another secondary. But that configuration will last only until the secondary recalculates which member to sync from.(禁用了链式复制以后,还是可以通过replSetSyncFrom指定复制源为second,但是必须要有投票权

    You can use the following sequence of commands to set settings.chainingAllowed to false:

    cfg = rs.config()
    cfg.settings.chainingAllowed = false
    rs.reconfig(cfg)

    恢复链式复制

    cfg = rs.config()
    cfg.settings.chainingAllowed = true
    rs.reconfig(cfg)

    一些思考

    排查问题时使用的几个命令:

    mongostat

    mongotop

    rs.status()

    rs.printSlaveReplicationInfo()  打印数据同步延迟信息

    rs.printReplicationInfo()  打印oplog信息

    具体遇到数据同步延迟的,需要具体分析当时的情况,不能盲目的修改。首先考虑节点服务器的负载情况和当时的网络环境是否正常,有的时候可能是网络环境导致的,排除这些原因后,再去考虑修改同步源。这样要做好读写分离,否则master的压力会非常大。如果master的压力太大就要做一些处理,比如切换一下msater升级资源或使用replSetSyncFrom切换同步源到second上。

  • 相关阅读:
    228. Summary Ranges
    227. Basic Calculator II
    224. Basic Calculator
    222. Count Complete Tree Nodes
    223. Rectangle Area
    221. Maximal Square
    220. Contains Duplicate III
    219. Contains Duplicate II
    217. Contains Duplicate
    Java编程思想 4th 第4章 控制执行流程
  • 原文地址:https://www.cnblogs.com/fat-girl-spring/p/13432069.html
Copyright © 2020-2023  润新知