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

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

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",
                ......
        }
]

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

Parameter Type Description
commitQuorum integer or string Optional. 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.

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

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

相关文章

学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习...
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面...
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生...
Can’t connect to local MySQL server through socket \'/v...
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 ...
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服...