在DB中是否有一个主碎片尚未执行sh.enableSharding()?

问题描述 投票:0回答:1

MongoDB sharding集群使用的是 "主碎片" 以在已启用sharding的数据库中保存收集数据(用 sh.enableSharding()),但集合本身还没有启用(使用了 sh.shardCollection()). 该 mongos 进程会自动选择主碎片,除非用户明确地将其作为参数写入 sh.enableSharding()

然而,在DB中会发生什么呢?sh.enableSharding() 没有 被执行了吗?是否有一些 "全局主控 "来处理这些情况?我怎么知道是哪一个?sh.status() 不显示相关信息......

我使用的是MongoDB 4.2版本。

谢谢!

mongodb sharding
1个回答
2
投票

文档中说。

mongos在创建新数据库时,会选择集群中数据量最少的shard来选择主shard。

如果 enableSharding 被调用到一个已经存在的数据库上,上面的引用将在启用sharding之前定义数据库的位置。

sh.status() 显示了数据库的存储位置。

MongoDB Enterprise mongos> use foo
switched to db foo
MongoDB Enterprise mongos> db.foo.insert({a:1})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5eade78756d7ba8d40fc4317")
  }
  shards:
        {  "_id" : "shard01",  "host" : "shard01/localhost:14442,localhost:14443",  "state" : 1 }
        {  "_id" : "shard02",  "host" : "shard02/localhost:14444,localhost:14445",  "state" : 1 }
  active mongoses:
        "4.3.6" : 2
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
        {  "_id" : "foo",  "primary" : "shard02",  "partitioned" : false,  "version" : {  "uuid" : UUID("ff618243-f4b9-4607-8f79-3075d14d737d"),  "lastMod" : 1 } }
        {  "_id" : "test",  "primary" : "shard01",  "partitioned" : false,  "version" : {  "uuid" : UUID("4d76cf84-4697-4e8c-82f8-a0cfad87be80"),  "lastMod" : 1 } }

foo 没有被分区,存储在shard02中。

如果 enableSharding 在一个还不存在的数据库上调用,数据库被创建,并且指定了主碎片,指定的碎片被用作主碎片。测试代码 此处.

© www.soinside.com 2019 - 2024. All rights reserved.