分片整体信息
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("60545017224c766911a9c440")
}
shards:
{ "_id" : "hdshard1", "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001", "state" : 1 }
{ "_id" : "hdshard2", "host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002", "state" : 1 }
{ "_id" : "hdshard3", "host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003", "state" : 1 }
active mongoses:
"4.2.12" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 5
Last reported error: Could not find host matching read preference { mode: "primary" } for set hdshard2
Time of Reported error: Wed Apr 14 2021 19:16:07 GMT+0800 (CST)
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
hdshard1 342
hdshard2 341
hdshard3 341
too many chunks to print, use verbose if you want to force print
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
db5.db5
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
hdshard2 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : hdshard2 Timestamp(1, 0)
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : false, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : false, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }
列出分片信息
mongos> use admin
switched to db admin
mongos> db.runCommand( { listshards : 1 } )
{
"shards" : [
{
"_id" : "hdshard1",
"host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001",
"state" : 1
},
{
"_id" : "hdshard2",
"host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002",
"state" : 1
},
{
"_id" : "hdshard3",
"host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1618479240, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1618479240, 1),
"signature" : {
"hash" : BinData(0,"ou1dzkqck0XTgvqkH/xBlhXC0R4="),
"keyId" : NumberLong("6941260985399246879")
}
}
}
或者
mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "hdshard1", "host" : "hdshard1/172.16.254.136:40001,172.16.254.137:40001,172.16.254.138:40001", "state" : 1 }
{ "_id" : "hdshard2", "host" : "hdshard2/172.16.254.136:40002,172.16.254.137:40002,172.16.254.138:40002", "state" : 1 }
{ "_id" : "hdshard3", "host" : "hdshard3/172.16.254.136:40003,172.16.254.137:40003,172.16.254.138:40003", "state" : 1 }
查看所有数据库分片情况
mongos> use config
switched to db config
mongos> db.databases.find()
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : false, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : false, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }
可以看到db5开启了分片,db6和recommend没有开启分片。
激活数据库分片功能
mongos> sh.enableSharding("db6")
{
"ok" : 1,
"operationTime" : Timestamp(1618480521, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1618480521, 3),
"signature" : {
"hash" : BinData(0,"ReiWhtUTkbTud9SM+7N2+WyhBcs="),
"keyId" : NumberLong("6941260985399246879")
}
}
}
mongos>
mongos> sh.enableSharding("recommend")
{
"ok" : 1,
"operationTime" : Timestamp(1618480562, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1618480562, 4),
"signature" : {
"hash" : BinData(0,"NJG21ltpt/C7h5SH7CKvnT95I+k="),
"keyId" : NumberLong("6941260985399246879")
}
}
}
验证数据库分片情况
mongos> db.databases.find()
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : true, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : true, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }
可以看到db6和recommend库都已活数据库分片。
判断是否为分片集群
mongos> db.runCommand({ isdbgrid : 1})
{
"isdbgrid" : 1,
"hostname" : "mongo7",
"ok" : 1,
"operationTime" : Timestamp(1618486042, 13),
"$clusterTime" : {
"clusterTime" : Timestamp(1618486042, 13),
"signature" : {
"hash" : BinData(0,"3k9SyHGJqe/nbacFkPTxo59smNA="),
"keyId" : NumberLong("6941260985399246879")
}
}
}
列出开启分片的数据库
mongos> use config
switched to db config
mongos> db.databases.find( { "partitioned": true } )
{ "_id" : "db5", "primary" : "hdshard2", "partitioned" : true, "version" : { "uuid" : UUID("bc5d26af-5248-431b-8065-7143fdbffb97"), "lastMod" : 1 } }
{ "_id" : "db6", "primary" : "hdshard3", "partitioned" : true, "version" : { "uuid" : UUID("6a1dd60f-a1fc-47be-9fad-d8ce01841d68"), "lastMod" : 1 } }
{ "_id" : "recommend", "primary" : "hdshard1", "partitioned" : true, "version" : { "uuid" : UUID("cb833b8e-cc4f-4c52-83c3-719aa383bac4"), "lastMod" : 1 } }
查看分片键
mongos> db.collections.find()
{ "_id" : "config.system.sessions", "lastmodEpoch" : ObjectId("60547131a531190b111e42a0"), "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), "dropped" : false, "key" : { "_id" : 1 }, "unique" : false, "uuid" : UUID("f8a8e664-10d2-41fb-84ec-a165579ee86d") }
{ "_id" : "db5.db5", "lastmodEpoch" : ObjectId("6054804d6410651de69524d9"), "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), "dropped" : false, "key" : { "id" : 1 }, "unique" : false, "uuid" : UUID("2122da21-7279-4b88-b60f-73dd3d0276fb") }