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> 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM