{
"_id": ObjectId("4d6e9b89b600c2c196442c21")
"filename": "spreadsheet-1",
"updated_at": ISODate("2011-03-02T19:22:54.845Z"),
"username": "banks",
"data": "raw document data"
}
mkdir conf1 mkdir conf2
mongod --configsvr --replSet conf --dbpath conf1 --port 4001
mongod --configsvr --replSet conf --dbpath conf2 --port 4002
mongo -port 4001
rs.initiate()
rs.conf()
rs.add("localhost:4002")
rs.status()
exit
mongo --port 4002 rs.slaveOk() exit
mkdir data1-1 mkdir data1-2
mongod --shardsvr --replSet data1 --dbpath data1-1 --port 4003
mongod --shardsvr --replSet data1 --dbpath data1-2 --port 4004
mongo -port 4003
rs.initiate()
rs.conf()
rs.add("localhost:4004")
rs.status()
exit
mongo --port 4004 rs.slaveOk() exit
mkdir data2-1 mkdir data2-2
mongod --shardsvr --replSet data2 --dbpath data2-1 --port 4005
mongod --shardsvr --replSet data2 --dbpath data2-2 --port 4006
mongo -port 4005
rs.initiate()
rs.conf()
rs.add("localhost:4006")
rs.status()
exit
mongo --port 4006 rs.slaveOk() exit
mongos --configdb conf/localhost:4001,localhost:4002 --port 4000
mongo --port 4000
sh.addShard("data1/localhost:4003")
sh.addShard("data2/localhost:4005")
db.getSiblingDB("config").shards.find()
sh.enableSharding("test")
db.getSiblingDB("config").databases.find()
use test
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"000"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"001"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"002"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"003"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"004"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"005"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"006"})
db.testcol.insert({"fname":"James", "lname":"Bond", "shard-key":"007"})
db.testcol.createIndex({"shard-key":1})
sh.shardCollection("test.testcol", {"shard-key": 1})
db.test_collection.stats()
... ... ... ...
"shards" : {
"data2" : {
"ns" : "test.testcol",
"size" : 592,
"count" : 8,
"avgObjSize" : 74,
"storageSize" : 32768,
... ... ... ...
use test
var bulk = db.test_collection.initializeUnorderedBulkOp();
people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy",
"Greg", "Steve", "Kristina", "Katie", "Jeff"];
for(var i=0; i<1000000; i++){
user_id = i;
name = people[Math.floor(Math.random()*people.length)];
number = Math.floor(Math.random()*10001);
bulk.insert( { "user_id":user_id, "name":name, "number":number });
}
bulk.execute();
db.test_collection.createIndex({user_id:1})
sh.shardCollection("test.test_collection",{"user_id":1})
sh.shardCollection("test.test_collection",{"user_id":1})
... ... ... ...
"shards" : {
"data1" : {
"ns" : "test.test_collection",
"size" : 184478484,
"count" : 2604399,
"avgObjSize" : 70,
"storageSize" : 80326656,
... ... ... ...
... ... ... ...
"data2" : {
"ns" : "test.test_collection",
"size" : 135758751,
"count" : 1916602,
"avgObjSize" : 70,
"storageSize" : 67858432,
... ... ... ...
sh.status()
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
data1 1
{ "_id" : { "$minKey" : 1 } } -->>
{ "_id" : { "$maxKey" : 1 } } on : data1 Timestamp(1, 0)
... ... ... ...
test.test_collection
shard key: { "user_id" : 1 }
unique: false
balancing: true
chunks:
data1 10
data2 9
{ "user_id" : { "$minKey" : 1 } } -->> { "user_id" : 0 } on : data2 Timestamp(8, 0)
{ "user_id" : 0 } -->> { "user_id" : 166667 } on : data2 Timestamp(9, 0)
{ "user_id" : 166667 } -->> { "user_id" : 289501 } on : data1 Timestamp(9, 1)
{ "user_id" : 289501 } -->> { "user_id" : 414501 } on : data1 Timestamp(7, 5)
{ "user_id" : 414501 } -->> { "user_id" : 479349 } on : data1 Timestamp(7, 6)
{ "user_id" : 479349 } -->> { "user_id" : 604349 } on : data1 Timestamp(3, 0)
{ "user_id" : 604349 } -->> { "user_id" : 789699 } on : data1 Timestamp(5, 0)
{ "user_id" : 789699 } -->> { "user_id" : 958699 } on : data2 Timestamp(7, 1)
{ "user_id" : 958699 } -->> { "user_id" : 1167398 } on : data2 Timestamp(3, 2)
{ "user_id" : 1167398 } -->> { "user_id" : 1417399 } on : data2 Timestamp(3, 3)
{ "user_id" : 1417399 } -->> { "user_id" : 1667400 } on : data2 Timestamp(3, 4)
{ "user_id" : 1667400 } -->> { "user_id" : 1982999 } on : data2 Timestamp(3, 5)
{ "user_id" : 1982999 } -->> { "user_id" : 2232999 } on : data1 Timestamp(5, 2)
{ "user_id" : 2232999 } -->> { "user_id" : 2483000 } on : data1 Timestamp(5, 3)
{ "user_id" : 2483000 } -->> { "user_id" : 2733001 } on : data1 Timestamp(5, 4)
{ "user_id" : 2733001 } -->> { "user_id" : 3007999 } on : data1 Timestamp(5, 5)
{ "user_id" : 3007999 } -->> { "user_id" : 3257999 } on : data2 Timestamp(6, 2)
{ "user_id" : 3257999 } -->> { "user_id" : 3520999 } on : data2 Timestamp(6, 3)
{ "user_id" : 3520999 } -->> { "user_id" : { "$maxKey" : 1 } } on : data1 Timestamp(7, 0)
test.testcol
shard key: { "shard-key" : 1 }
unique: false
balancing: true
chunks:
data2 1
{ "shard-key" : { "$minKey" : 1 } } -->>
{ "shard-key" : { "$maxKey" : 1 } } on : data2 Timestamp(1, 0)