> mongo --nodb
# mkdir -p /data/db/test1
> cluster = new ShardingTest({"shards" : 3, "chunksize" : 1}) //30000、30001、30002 mongod,mongos 30999
# netstat -tnplu | grep mong
tcp 0 0 0.0.0.0:30000 0.0.0.0:* LISTEN 28000/mongod
tcp 0 0 0.0.0.0:30001 0.0.0.0:* LISTEN 28016/mongod
tcp 0 0 0.0.0.0:30002 0.0.0.0:* LISTEN 28030/mongod
tcp 0 0 0.0.0.0:30999 0.0.0.0:* LISTEN 28048/mongos
tcp 0 0 0.0.0.0:31000 0.0.0.0:* LISTEN 28000/mongod
tcp 0 0 0.0.0.0:31001 0.0.0.0:* LISTEN 28016/mongod
tcp 0 0 0.0.0.0:31002 0.0.0.0:* LISTEN 28030/mongod
tcp 0 0 0.0.0.0:31999 0.0.0.0:* LISTEN 28048/mongos
# ps -ef | grep mong
root 27869 25530 0 22:14 pts/3 00:00:00 ./mongo --nodb
root 28000 27869 0 22:17 pts/3 00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongod --port 30000 --dbpath /data/db/test0 --setParameter enableTestCommands=1
root 28016 27869 0 22:17 pts/3 00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongod --port 30001 --dbpath /data/db/test1 --setParameter enableTestCommands=1
root 28030 27869 0 22:17 pts/3 00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongod --port 30002 --dbpath /data/db/test2 --setParameter enableTestCommands=1
root 28048 27869 0 22:17 pts/3 00:00:00 /usr/local/mongodb-linux-x86_64-2.4.5/bin/mongos --port 30999 --configdb localhost:30000 --chunkSize 1 --setParameter enableTestCommands=1
> db = (new Mongo("localhost:30999")).getDB("test")
m30999| Sat Jul 27 22:24:25.744 [mongosMain] connection accepted from 127.0.0.1:39375 #2 (2 connections now open)
test
mongos>for (var i=0; i<100000; i++) {
... db.users.insert({"username" : "user"+i, "created_at" : new Date()});
... }
mongos> db.users.count()
100000
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("51f3d68a3b74fc9fc09a0043")
}
shards:
{ "_id" : "shard0000", "host" : "localhost:30000" }
{ "_id" : "shard0001", "host" : "localhost:30001" }
{ "_id" : "shard0002", "host" : "localhost:30002" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }
mongos> sh.enableSharding("test")
m30999| Sat Jul 27 22:28:31.460 [conn2] enabling sharding on: test
{ "ok" : 1 }
mongos> db.users.ensureIndex({"username" : 1})
m30001| Sat Jul 27 22:29:01.272 [conn3] build index test.users { username: 1.0 }
m30001| Sat Jul 27 22:29:01.554 [conn3] build index done. scanned 100000 total records. 0.282 secs
m30001| Sat Jul 27 22:29:01.554 [conn3] insert test.system.indexes ninserted:1 keyUpdates:0 locks(micros) w:282639 282ms
mongos> sh.shardCollection("test.users", {"username" : 1})
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("51f3d68a3b74fc9fc09a0043")
}
shards:
{ "_id" : "shard0000", "host" : "localhost:30000" }
{ "_id" : "shard0001", "host" : "localhost:30001" }
{ "_id" : "shard0002", "host" : "localhost:30002" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0001" }
test.users
shard key: { "username" : 1 }
chunks:
shard0000 5
shard0002 4
shard0001 5
{ "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shard0000 Timestamp(2, 0)
{ "username" : "user16643" } -->> { "username" : "user2329" } on : shard0002 Timestamp(3, 0)
{ "username" : "user2329" } -->> { "username" : "user29937" } on : shard0000 Timestamp(4, 0)
{ "username" : "user29937" } -->> { "username" : "user36583" } on : shard0002 Timestamp(5, 0)
{ "username" : "user36583" } -->> { "username" : "user43229" } on : shard0000 Timestamp(6, 0)
{ "username" : "user43229" } -->> { "username" : "user49877" } on : shard0002 Timestamp(7, 0)
{ "username" : "user49877" } -->> { "username" : "user56522" } on : shard0000 Timestamp(8, 0)
{ "username" : "user56522" } -->> { "username" : "user63169" } on : shard0002 Timestamp(9, 0)
{ "username" : "user63169" } -->> { "username" : "user69816" } on : shard0000 Timestamp(10, 0)
{ "username" : "user69816" } -->> { "username" : "user76462" } on : shard0001 Timestamp(10, 1)
{ "username" : "user76462" } -->> { "username" : "user83108" } on : shard0001 Timestamp(1, 10)
{ "username" : "user83108" } -->> { "username" : "user89756" } on : shard0001 Timestamp(1, 11)
{ "username" : "user89756" } -->> { "username" : "user96401" } on : shard0001 Timestamp(1, 12)
{ "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shard0001 Timestamp(1, 13)
mongos> db.users.find({username: "user12345"}).toArray()
[
{
"_id" : ObjectId("51f3d84f0273316ee2e7f700"),
"username" : "user12345",
"created_at" : ISODate("2013-07-27T14:25:19.769Z")
}
]
mongos> db.users.find({username: "user12345"}).explain()
{
"clusteredType" : "ParallelSort",
"shards" : {
"localhost:30000" : [
{
"cursor" : "BtreeCursor username_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"username" : [
[
"user12345",
"user12345"
]
]
},
"server" : "nd0302012029:30000"
}
]
},
"cursor" : "BtreeCursor username_1",
"n" : 1,
"nChunkSkips" : 0,
"nYields" : 0,
"nscanned" : 1,
"nscannedAllPlans" : 1,
"nscannedObjects" : 1,
"nscannedObjectsAllPlans" : 1,
"millisShardTotal" : 0,
"millisShardAvg" : 0,
"numQueries" : 1,
"numShards" : 1,
"indexBounds" : {
"username" : [
[
"user12345",
"user12345"
]
]
},
"millis" : 1
}
mongos> db.users.find().explain()
{
"clusteredType" : "ParallelSort",
"shards" : {
"localhost:30000" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 36924,
"nscannedObjects" : 36924,
"nscanned" : 36924,
"nscannedObjectsAllPlans" : 36924,
"nscannedAllPlans" : 36924,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 71,
"indexBounds" : {
},
"server" : "nd0302012029:30000"
}
],
"localhost:30001" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 33536,
"nscannedObjects" : 33536,
"nscanned" : 33536,
"nscannedObjectsAllPlans" : 33536,
"nscannedAllPlans" : 33536,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 55,
"indexBounds" : {
},
"server" : "nd0302012029:30001"
}
],
"localhost:30002" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 29540,
"nscannedObjects" : 29540,
"nscanned" : 29540,
"nscannedObjectsAllPlans" : 29540,
"nscannedAllPlans" : 29540,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 63,
"indexBounds" : {
},
"server" : "nd0302012029:30002"
}
]
},
"cursor" : "BasicCursor",
"n" : 100000,
"nChunkSkips" : 0,
"nYields" : 0,
"nscanned" : 100000,
"nscannedAllPlans" : 100000,
"nscannedObjects" : 100000,
"nscannedObjectsAllPlans" : 100000,
"millisShardTotal" : 189,
"millisShardAvg" : 63,
"numQueries" : 3,
"numShards" : 3,
"millis" : 72
}
mongos> cluster.stop()
# netstat -tnplu | grep mong
# ps -ef | grep mong
root 27869 25530 0 22:14 pts/3 00:00:03 ./mongo --nodb
注意: #是在系统下, >是在mongo下