来看看分片集合在单独分片副本集中的存在形式
首先需要找到该库已经被分配到了哪个分片之上(由于该库之前并没有数据,所以创建分片键的时候,会自动插入索引数据,自动按照默认配置路由到其中一个分片键集群之中)
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("58fe17e90b3df66581ff6b09")
}
shards:
{ "_id" : "shard-a", "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017", "state" : 1 }
{ "_id" : "shard-b", "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017", "state" : 1 }
active mongoses:
"3.4.4" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Balancer lock taken at Mon Apr 24 2017 23:21:13 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" : "test2_db", "primary" : "shard-a", "partitioned" : true }
test2_db.users
shard key: { "username" : 1, "_id" : 1 }
unique: false
balancing: true
chunks:
shard-a 1
{ "username" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "username" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard-a Timestamp(1, 0)
# 最后这行 databases 看到了,该库的该数据块(chunks) 被分配到了 shard-a 副本集中,那么我们接下来就可以直接到 shard-a 中查看该库中users集合的文档信息。
# 登录到 shard-a 副本集中进行查看
shard-a:PRIMARY> show dbs
admin 0.000GB
local 0.000GB
test2_db 0.000GB
shard-a:PRIMARY> use test2_db
switched to db test2_db
shard-a:PRIMARY> db.users.find() # 该集合暂时没有文档
shard-a:PRIMARY> db.users.getIndexes() # 查看该集合的索引配置信息
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test2_db.users"
},
{
"v" : 2,
"key" : {
"username" : 1,
"_id" : 1
},
"name" : "username_1__id_1",
"ns" : "test2_db.users"
} # 查看到了两个索引,第一个索引 _id 为系统默认添加的索引,第二个索引就是创建分片键的时候自动创建的组合键索引
]
写入数据到分片集群
# 首先创建一个数据对象,用来填充文档大小
mongos> data = new Array(2049).join("abcd ")
mongos> data.length
10240
# data 大小为 1MB
mongos> for (var i=0; i < 100; i++){
... db.getSiblingDB("test2_db").users.insert({
... username: "Join" + i,
... age: i % 13 + 20,
... data: data }
... )
... }
WriteResult({ "nInserted" : 1 })
# 批量插入 100 条文档,每个文档约为 1MB 大小。
# 接下来看看有了这么多文档过后,会怎么分片。
mongos> db.getSiblingDB("config").chunks.count()
3
# 插入这么多数据以后,就会发现多了几个数据块。我们可以通过检查集合中的数据库的数量来验证这个猜想
mongos> db.getSiblingDB("config").chunks.find().pretty()
{
"_id" : "test2_db.users-username_MinKey_id_MinKey",
"lastmod" : Timestamp(2, 1),
"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
"ns" : "test2_db.users",
"min" : {
"username" : { "$minKey" : 1 },
"_id" : { "$minKey" : 1 }
},
"max" : {
"username" : "Join1",
"_id" : ObjectId("58fe293756525c8a54e2a5af")
},
"shard" : "shard-a"
}
{
"_id" : "test2_db.users-username_\"Join1\"_id_ObjectId('58fe293756525c8a54e2a5af')",
"lastmod" : Timestamp(1, 2),
"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
"ns" : "test2_db.users",
"min" : {
"username" : "Join1",
"_id" : ObjectId("58fe293756525c8a54e2a5af")
},
"max" : {
"username" : "Join2",
"_id" : ObjectId("58fe293756525c8a54e2a5b0")
},
"shard" : "shard-a"
}
{
"_id" : "test2_db.users-username_\"Join2\"_id_ObjectId('58fe293756525c8a54e2a5b0')",
"lastmod" : Timestamp(2, 0),
"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
"ns" : "test2_db.users",
"min" : {
"username" : "Join2",
"_id" : ObjectId("58fe293756525c8a54e2a5b0")
},
"max" : {
"username" : { "$maxKey" : 1 },
"_id" : { "$maxKey" : 1 }
},
"shard" : "shard-b"
}
# 查看每个数据块的详细分片信息,发现有两个块被存储在 shard-a 副本集中,还有一个数据块被存储在 shard-b 副本集中
# 我们也可以通过 sh.status() 来更直观的看到相关信息。
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("58fe17e90b3df66581ff6b09")
}
shards:
{ "_id" : "shard-a", "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017", "state" : 1 }
{ "_id" : "shard-b", "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017", "state" : 1 }
active mongoses:
"3.4.4" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Balancer lock taken at Mon Apr 24 2017 23:21:13 GMT+0800 (CST) by ConfigServer:Balancer
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
1 : Success
databases:
{ "_id" : "test2_db", "primary" : "shard-a", "partitioned" : true }
test2_db.users
shard key: { "username" : 1, "_id" : 1 }
unique: false
balancing: true
chunks:
shard-a 2
shard-b 1
{ "username" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "username" : "Join1", "_id" : ObjectId("58fe293756525c8a54e2a5af") } on : shard-a Timestamp(2, 1)
{ "username" : "Join1", "_id" : ObjectId("58fe293756525c8a54e2a5af") } -->> { "username" : "Join2", "_id" : ObjectId("58fe293756525c8a54e2a5b0") } on : shard-a Timestamp(1, 2)
{ "username" : "Join2", "_id" : ObjectId("58fe293756525c8a54e2a5b0") } -->> { "username" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard-b Timestamp(2, 0)
# 这个方法会打印所有的数据库信息,并且包含范围信息。
表象背后,MongoDB 底层依赖 2 个机制来保持集群的平衡:分割与迁移。