Mongodb——对Collection启用分片功能


1、启用分片功能

对库启用分片

sh.enableSharding("loginserver")

mongos> 
mongos> 
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6")
}
  shards:
    {  "_id" : "shard1_repl",  "host" : "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201",  "state" : 1 }
    {  "_id" : "shard2_repl",  "host" : "shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202",  "state" : 1 }
    {  "_id" : "shard3_repl",  "host" : "shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203",  "state" : 1 }
  active mongoses:
    "3.4.4" : 1
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:
    {  "_id" : "loginserver",  "primary" : "shard1_repl",  "partitioned" : true }
    {  "_id" : "loginsaveserver",  "primary" : "shard2_repl",  "partitioned" : true }

mongos> 

2、对集合启用分片功能

创建索引

mongos> 
mongos> db
loginserver
mongos> db.users.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "loginserver.users"
    }
]
mongos> 
mongos> 
mongos> db.users.createIndex({name:1})
{
    "raw" : {
        "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201" : {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1,
            "$gleStats" : {
                "lastOpTime" : {
                    "ts" : Timestamp(1593475851, 1),
                    "t" : NumberLong(1)
                },
                "electionId" : ObjectId("7fffffff0000000000000001")
            }
        }
    },
    "ok" : 1
}
mongos> db.users.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "loginserver.users"
    },
    {
        "v" : 2,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "loginserver.users"
    }
]

再对集合users进行分片,name字段是片键。  片键的选择:利于分块、分散写请求、查询数据。

sh.shardCollection("loginserver.users", {"name":1})

mongos> sh.shardCollection("loginserver.users", {"name":1})
{ "collectionsharded" : "loginserver.users", "ok" : 1 }
mongos> 
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6")
}
  shards:
    {  "_id" : "shard1_repl",  "host" : "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201",  "state" : 1 }
    {  "_id" : "shard2_repl",  "host" : "shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202",  "state" : 1 }
    {  "_id" : "shard3_repl",  "host" : "shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203",  "state" : 1 }
  active mongoses:
    "3.4.4" : 1
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:
    {  "_id" : "loginserver",  "primary" : "shard1_repl",  "partitioned" : true }
        loginserver.users
            shard key: { "name" : 1 }
            unique: false
            balancing: true
            chunks:
                shard1_repl    1
            { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard1_repl Timestamp(1, 0) 
    {  "_id" : "loginsaveserver",  "primary" : "shard2_repl",  "partitioned" : true }

mongos> 

 

原文地址:https://www.cnblogs.com/xingchong/p/13211634.html