• MongoDB 副本集创建索引无响应


    MongoDB 创建索引无响应

    使用db.collection.createIndex()创建索引的时候无响应,如下所示:

    sit_rs1:PRIMARY> db.mytable.createIndex({"a":1}, {"name":"idx_a"});   # 等待状态
    
    ### 按 ctrl + C 后提示是否kill掉当前操作
    do you want to kill the current op(s) on the server? (y/n): y
    
    • 1
    • 2
    • 3
    • 4

    出现这个问题先排查下副本集的状态,三个节点是否都正常?

    sit_rs1:PRIMARY> rs.status()["members"]; 
    [
            {
                    "_id" : 0,
                    "name" : "192.168.88.11:27017",
                    "health" : 0,
                    "state" : 8,
                    "stateStr" : "(not reachable/healthy)",
                    ........
            },
            {
                    "_id" : 1,
                    "name" : "192.168.88.11:27018",
                    "health" : 1,
                    "state" : 2,
                    "stateStr" : "SECONDARY",
                    .......
            },
            {
                    "_id" : 2,
                    "name" : "192.168.88.11:27019",
                    "health" : 1,
                    "state" : 1,
                    "stateStr" : "PRIMARY",
                    ......
            }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    通过状态发现其中有一台节点故障了。 为啥 有一个节点故障 就创建不了, 副本集不是高可用吗? 这个还要看mongodb的版本特性,查下文档发现 V4.4 版本创建索引多了一个 commitQuorum 参数: 详细解释如下:

    ParameterTypeDescription
    commitQuoruminteger or stringOptional. The minimum number of data-bearing voting replica set members (i.e. commit quorum), including the primary, that must report a successful index build before the primary marks the indexes as ready. A “voting” member is any replica set member where
    members[n].votes
    is greater than 0.

    Supports the following values:

    “votingMembers” - all data-bearing voting replica set members (Default).

    “majority” - a simple majority of data-bearing voting replica set members.

    - a specific number of data-bearing voting replica set members.

    0 - Disables quorum-voting behavior. Members start the index build simultaneously but do not vote or wait for quorum before completing the index build. If you start an index build with a commit quorum of 0, you cannot later modify the commit quorum using setIndexCommitQuorum

    A replica set tag name.

    New in version 4.4.

    即默认为 “votingMembers” - 即所有带有数据的投票副本集成员。可以通过修改参数来创建,调整为 0 或者2 , 或者字符串: “majority” 表示大多数节点,如下:

    #### 查看数据库的版本 #####
    sit_rs1:PRIMARY> 
    sit_rs1:PRIMARY> db.version();
    4.4.15
    sit_rs1:PRIMARY> 
    sit_rs1:PRIMARY> db.mytable.createIndex({"x1":1}, {"name":"idx_x1"}, 2);
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 3,
            "numIndexesAfter" : 4,
            "commitQuorum" : 2,
            "ok" : 1,
            "$clusterTime" : {
                    "clusterTime" : Timestamp(1661329293, 6),
                    "signature" : {
                            "hash" : BinData(0,"pVH05py8KmGBjbqhyJLvlnpT2NI="),
                            "keyId" : NumberLong("7135350621929996292")
                    }
            },
            "operationTime" : Timestamp(1661329293, 6)
    }
    sit_rs1:PRIMARY> 
    sit_rs1:PRIMARY> 
    sit_rs1:PRIMARY> db.mytable.createIndex({"x2":1}, {"name":"idx_x2"}, "majority");
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 4,
            "numIndexesAfter" : 5,
            "commitQuorum" : "majority",
            "ok" : 1,
            "$clusterTime" : {
                    "clusterTime" : Timestamp(1661329312, 6),
                    "signature" : {
                            "hash" : BinData(0,"guE7KshVVHSRuHoNzrRt1ckxoNY="),
                            "keyId" : NumberLong("7135350621929996292")
                    }
            },
            "operationTime" : Timestamp(1661329312, 6)
    }
    sit_rs1:PRIMARY> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    当故障的节点恢复后,刚创建索引也会同步此节点。

  • 相关阅读:
    复习计算机网络——第二章记录(1)
    SpringCloud 之 服务提供者
    Linux 指令心法(十七)`nandwrite` 写入NAND闪存设备
    相机标定计算内参数:每次拍照,相机和标定板都可以变换位置吗?
    tmux随笔
    高阶数据结构:并查集
    10.16 校招 实习 内推 面经
    层次聚类之经典算法(一) Birch算法
    设置小于浏览器默认字体大小的显示方法
    【LINGO】lingo 软件简介
  • 原文地址:https://blog.csdn.net/qq_33158376/article/details/126507519