

mongod.exe --dbpath="C:\mongodb\db\config" --port 11110
mongos.exe --port=11111 --configdb=127.0.0.1:11110
mongod.exe --dbpath="c:\mongodb\db\shard0" --port=22220 mongod.exe --dbpath="c:\mongodb\db\shard1" --port=22221
mongo.exe 127.0.0.1:11111
use admin
db.runCommand({"addshard":"127.0.0.1:22220",allowLocal:true})
db.runCommand({"addshard":"127.0.0.1:22221",allowLocal:true})
mongos> use admin
switched to db admin
mongos> db.runCommand({"enablesharding":"test"})
{ "ok" : 1 }
mongos> db.runCommand({"shardcollection":"test.student","key":{"sid":1}})
{ "collectionsharded" : "test.student", "ok" : 1 }
mongos>
mongos> db.runCommand({"listShards":1})
{
"shards" : [
{
"_id" : "shard0000",
"host" : "127.0.0.1:22220"
},
{
"_id" : "shard0001",
"host" : "127.0.0.1:22221"
}
],
"ok" : 1
}
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("548bd82b9355a9edbf5efa69")
}
shards:
{ "_id" : "shard0000", "host" : "127.0.0.1:22220" }
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
test.student
shard key: { "sid" : 1 }
chunks:
shard0000 1
{ "name" : { "$minKey" : 1 } } -->> { " sid " : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
mongos>
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
mongos> db.runCommand({"removeshard":"127.0.0.1:22221"})
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard0001",
"ok" : 1
}
mongos> db.runCommand({"listShards":1})
{
"shards" : [
{
"_id" : "shard0000",
"host" : "127.0.0.1:22220"
},
{
"_id" : "shard0001",
"draining" : true,
"host" : "127.0.0.1:22221"
}
],
"ok" : 1
}
mongos>
mongos> db.runCommand({"removeshard":"127.0.0.1:22221"})
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard0001",
"ok" : 1
}
mongos> db.runCommand({"listShards":1})
{
"shards" : [
{
"_id" : "shard0000",
"host" : "127.0.0.1:22220"
}
],
"ok" : 1
}
mongos>
mongos> db.runCommand({"removeshard":"127.0.0.1:22220"})
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard0000",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"test"
],
"ok" : 1
}
mongos>
mongos> db.runCommand({"moveprimary":"test","to":"127.0.0.1:22221"})
{ "primary " : "shard0001:127.0.0.1:22221", "ok" : 1 }
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("548bd82b9355a9edbf5efa69")
}
shards:
{ "_id" : "shard0000", "draining" : true, "host" : "127.0.0.1:22220" }
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0001" }
test.student
shard key: { " sid " : 1 }
chunks:
shard0001 1
{ "name" : { "$minKey" : 1 } } -->> { " sid " : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 0)
mongos>
mongos> db.runCommand({"removeshard":"127.0.0.1:22220"})
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard0000",
"ok" : 1
}
mongos> db.runCommand({"listShards":1})
{
"shards" : [
{
"_id" : "shard0001",
"host" : "127.0.0.1:22221"
}
],
"ok" : 1
}
mongos>
mongos> db.runCommand({"addshard":"127.0.0.1:22220",allowLocal:true})
{
"ok" : 0,
"errmsg" : "can‘t add shard 127.0.0.1:22220 because a local database ‘test‘ exists in another shard0001:127.0.0.1:22221"
}
mongos>
C:\mongodb\bin>mongo.exe --port=22220
MongoDB shell version: 2.4.6
connecting to: 127.0.0.1:22220/test
> use test
switched to db test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
>
mongos> db.runCommand({"addshard":"127.0.0.1:22220",allowLocal:true})
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos> db.runCommand({"listShards":1})
{
"shards" : [
{
"_id" : "shard0001",
"host" : "127.0.0.1:22221"
},
{
"_id" : "shard0002",
"host" : "127.0.0.1:22220"
}
],
"ok" : 1
}
mongos>
mongos> use config switched to db config mongos> show collections changelog chunks collections databases lockpings locks mongos settings shards system.indexes tags version mongos>
mongos> db.chunks.find()
{ "_id" : "test.student-sid_MinKey", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("548c02d79355a9edbf5f0193"), "ns" : "test.studen
t", "min" : { "sid" : { "$minKey" : 1 } }, "max" : { "sid" : { "$maxKey" : 1 } }, "shard" : "shard0002" }
mongos> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0002" }
{ "_id" : "amdin", "partitioned" : false, "primary" : "shard0002" }
mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 1 }
mongos> db.shards.find()
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
{ "_id" : "shard0002", "host" : "127.0.0.1:22220" }
mongos>
mongos> use config
switched to db config
mongos> db.settings.save({ _id:"chunksize", value: 1})
mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 1 }
mongos>
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("548bd82b9355a9edbf5efa69")
}
shards:
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
{ "_id" : "shard0002", "host" : "127.0.0.1:22220" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0002" }
test.student
shard key: { "sid" : 1 }
chunks:
shard0002 2
shard0001 8
{ "sid" : { "$minKey" : 1 } } -->> { "sid" : 0 } on : shard0002 Timestamp(2, 1)
{ "sid" : 0 } -->> { "sid" : 6673 } on : shard0002 Timestamp(1, 3)
{ "sid" : 6673 } -->> { "sid" : 20765 } on : shard0001 Timestamp(2, 2)
{ "sid" : 20765 } -->> { "sid" : 34839 } on : shard0001 Timestamp(2, 4)
{ "sid" : 34839 } -->> { "sid" : 48913 } on : shard0001 Timestamp(2, 6)
{ "sid" : 48913 } -->> { "sid" : 63011 } on : shard0001 Timestamp(2, 8)
{ "sid" : 63011 } -->> { "sid" : 74942 } on : shard0001 Timestamp(2, 10)
{ "sid" : 74942 } -->> { "sid" : 86877 } on : shard0001 Timestamp(2, 12)
{ "sid" : 86877 } -->> { "sid" : 98836 } on : shard0001 Timestamp(2, 14)
{ "sid" : 98836 } -->> { "sid" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 15)
{ "_id" : "amdin", "partitioned" : false, "primary" : "shard0002" }
mongos>
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("548bd82b9355a9edbf5efa69")
}
shards:
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
{ "_id" : "shard0002", "host" : "127.0.0.1:22220" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0002" }
test.student
shard key: { "sid" : 1 }
chunks:
shard0002 5
shard0001 5
{ "sid" : { "$minKey" : 1 } } -->> { "sid" : 0 } on : shard0002 Timestamp(2, 1)
{ "sid" : 0 } -->> { "sid" : 6673 } on : shard0002 Timestamp(1, 3)
{ "sid" : 6673 } -->> { "sid" : 20765 } on : shard0002 Timestamp(3, 0)
{ "sid" : 20765 } -->> { "sid" : 34839 } on : shard0002 Timestamp(4, 0)
{ "sid" : 34839 } -->> { "sid" : 48913 } on : shard0002 Timestamp(5, 0)
{ "sid" : 48913 } -->> { "sid" : 63011 } on : shard0001 Timestamp(5, 1)
{ "sid" : 63011 } -->> { "sid" : 74942 } on : shard0001 Timestamp(2, 10)
{ "sid" : 74942 } -->> { "sid" : 86877 } on : shard0001 Timestamp(2, 12)
{ "sid" : 86877 } -->> { "sid" : 98836 } on : shard0001 Timestamp(2, 14)
{ "sid" : 98836 } -->> { "sid" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 15)
{ "_id" : "amdin", "partitioned" : false, "primary" : "shard0002" }
mongos>
mongos> db.student.getShardDistribution() Shard shard0001 at 127.0.0.1:22221 data : 4.28MiB docs : 51087 chunks : 5 estimated data per chunk : 878KiB estimated docs per chunk : 10217 Shard shard0002 at 127.0.0.1:22220 data : 4.1MiB docs : 48913 chunks : 5 estimated data per chunk : 840KiB estimated docs per chunk : 9782 Totals data : 8.39MiB docs : 100000 chunks : 10 Shard shard0001 contains 51.08% data, 51.08% docs in cluster, avg obj size on shard : 88B Shard shard0002 contains 48.91% data, 48.91% docs in cluster, avg obj size on shard : 88B mongos>
use school
for(var i=0;i<100000;i++){
var randAge = parseInt(5*Math.random()) + 20;
var gender = (randAge%2)?"Male":"Female";
db.student.insert({"sid":i, "name":"Will"+i, "gender": gender, "age": randAge});
}
mongos> use admin
switched to db admin
mongos> db.runCommand({"addshard":"127.0.0.1:22222",allowLocal:true})
{ "shardAdded" : "shard0003", "ok" : 1 }
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("548bd82b9355a9edbf5efa69")
}
shards:
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
{ "_id" : "shard0002", "host" : "127.0.0.1:22220" }
{ "_id" : "shard0003", "host" : "127.0.0.1:22222" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0002" }
test.student
shard key: { "sid" : 1 }
chunks:
shard0003 3
shard0002 4
shard0001 3
{ "sid" : { "$minKey" : 1 } } -->> { "sid" : 0 } on : shard0003 Timestamp(7, 0)
{ "sid" : 0 } -->> { "sid" : 6673 } on : shard0002 Timestamp(7, 1)
{ "sid" : 6673 } -->> { "sid" : 20765 } on : shard0002 Timestamp(3, 0)
{ "sid" : 20765 } -->> { "sid" : 34839 } on : shard0002 Timestamp(4, 0)
{ "sid" : 34839 } -->> { "sid" : 48913 } on : shard0002 Timestamp(5, 0)
{ "sid" : 48913 } -->> { "sid" : 63011 } on : shard0003 Timestamp(6, 0)
{ "sid" : 63011 } -->> { "sid" : 74942 } on : shard0003 Timestamp(8, 0)
{ "sid" : 74942 } -->> { "sid" : 86877 } on : shard0001 Timestamp(8, 1)
{ "sid" : 86877 } -->> { "sid" : 98836 } on : shard0001 Timestamp(2, 14)
{ "sid" : 98836 } -->> { "sid" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 15)
{ "_id" : "amdin", "partitioned" : false, "primary" : "shard0002" }
{ "_id" : "school", "partitioned" : false, "primary" : "shard0003" }
mongos>
mongos> db.runCommand({"enablesharding":"school"})
{ "ok" : 1 }
mongos> db.runCommand({"shardcollection":"school.student","key":{"sid":1}})
{
"proposedKey" : {
"sid" : 1
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "school.student",
"name" : "_id_"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
}
mongos> use school
switched to db school
mongos> db.student.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "school.student",
"name" : "_id_"
}
]
mongos> db.student.ensureIndex({"sid":1})
mongos> use admin
switched to db admin
mongos> db.runCommand({"shardcollection":"school.student","key":{"sid":1}})
{ "collectionsharded" : "school.student", "ok" : 1 }
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("548bd82b9355a9edbf5efa69")
}
shards:
{ "_id" : "shard0001", "host" : "127.0.0.1:22221" }
{ "_id" : "shard0002", "host" : "127.0.0.1:22220" }
{ "_id" : "shard0003", "host" : "127.0.0.1:22222" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0002" }
test.student
shard key: { "sid" : 1 }
chunks:
shard0003 3
shard0002 4
shard0001 3
{ "sid" : { "$minKey" : 1 } } -->> { "sid" : 0 } on : shard0003 Timestamp(7, 0)
{ "sid" : 0 } -->> { "sid" : 6673 } on : shard0002 Timestamp(7, 1)
{ "sid" : 6673 } -->> { "sid" : 20765 } on : shard0002 Timestamp(3, 0)
{ "sid" : 20765 } -->> { "sid" : 34839 } on : shard0002 Timestamp(4, 0)
{ "sid" : 34839 } -->> { "sid" : 48913 } on : shard0002 Timestamp(5, 0)
{ "sid" : 48913 } -->> { "sid" : 63011 } on : shard0003 Timestamp(6, 0)
{ "sid" : 63011 } -->> { "sid" : 74942 } on : shard0003 Timestamp(8, 0)
{ "sid" : 74942 } -->> { "sid" : 86877 } on : shard0001 Timestamp(8, 1)
{ "sid" : 86877 } -->> { "sid" : 98836 } on : shard0001 Timestamp(2, 14)
{ "sid" : 98836 } -->> { "sid" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 15)
{ "_id" : "amdin", "partitioned" : false, "primary" : "shard0002" }
{ "_id" : "school", "partitioned" : true, "primary" : "shard0003" }
school.student
shard key: { "sid" : 1 }
chunks:
shard0001 6
shard0002 5
shard0003 6
{ "sid" : { "$minKey" : 1 } } -->> { "sid" : 5957 } on : shard0001 Timestamp(2, 0)
{ "sid" : 5957 } -->> { "sid" : 11915 } on : shard0002 Timestamp(3, 0)
{ "sid" : 11915 } -->> { "sid" : 17873 } on : shard0001 Timestamp(4, 0)
{ "sid" : 17873 } -->> { "sid" : 23831 } on : shard0002 Timestamp(5, 0)
{ "sid" : 23831 } -->> { "sid" : 29789 } on : shard0001 Timestamp(6, 0)
{ "sid" : 29789 } -->> { "sid" : 35747 } on : shard0002 Timestamp(7, 0)
{ "sid" : 35747 } -->> { "sid" : 41705 } on : shard0001 Timestamp(8, 0)
{ "sid" : 41705 } -->> { "sid" : 47663 } on : shard0002 Timestamp(9, 0)
{ "sid" : 47663 } -->> { "sid" : 53621 } on : shard0001 Timestamp(10, 0)
{ "sid" : 53621 } -->> { "sid" : 59579 } on : shard0002 Timestamp(11, 0)
{ "sid" : 59579 } -->> { "sid" : 65537 } on : shard0001 Timestamp(12, 0)
{ "sid" : 65537 } -->> { "sid" : 71495 } on : shard0003 Timestamp(12, 1)
{ "sid" : 71495 } -->> { "sid" : 77453 } on : shard0003 Timestamp(1, 12)
{ "sid" : 77453 } -->> { "sid" : 83411 } on : shard0003 Timestamp(1, 13)
{ "sid" : 83411 } -->> { "sid" : 89369 } on : shard0003 Timestamp(1, 14)
{ "sid" : 89369 } -->> { "sid" : 95327 } on : shard0003 Timestamp(1, 15)
{ "sid" : 95327 } -->> { "sid" : { "$maxKey" : 1 } } on : shard0003 Timestamp(1, 16)
mongos>
参考来源:
MongoDB分片存储的集群架构实现
http://www.lai18.com/content/317635.html
2MongoDB入门教程(包含安装、常用命令、相关概念、使用技巧、常见操作等)
8MongoDB入门教程之细说MongoDB数据库的增删查改操作
10MongoDB入门教程之Windows下的MongoDB数据库安装图解
11MongoDB查询字段没有创建索引导致的连接超时异常解案例分享
30MongoDB教程之聚合(count、distinct和group)
39MongoDB系列教程(六):java操作mongodb实例
40MongoDB系列教程(七):MongoDb数据结构详解
41MongoDB系列教程(五):mongo语法和mysql语法对比学习
原文地址:http://blog.csdn.net/u011252402/article/details/47061019