Skip to content

Instantly share code, notes, and snippets.

@daniel-roberts-10gen
Last active October 1, 2015 01:18
Show Gist options
  • Save daniel-roberts-10gen/1893173 to your computer and use it in GitHub Desktop.
Save daniel-roberts-10gen/1893173 to your computer and use it in GitHub Desktop.
MongoDB Sharding Setup for localhost test
//Setup directories
mkdir r1
mkdir r2
mkdir r3
mkdir r4
mkdir r5
mkdir r6
mkdir config
//create replicasets
//First Set (shard 1)
./mongod --port 27017 --dbpath ../data/r1 --replSet dmr
./mongod --port 27018 --dbpath ../data/r2 --replSet dmr
./mongod --port 27019 --dbpath ../data/r3 --replSet dmr
//mongo shell
./mongo --port 27017
cfg = {
"_id" : "dmr",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:27017"
},
{
"_id" : 1,
"host" : "localhost:27018"
},
{
"_id" : 2,
"host" : "localhost:27019"
}
]
}
rs.initiate(cfg)
rs.status();
//insert test data.
db.stuff.insert({data:’xxxxxxxxxxxxxxxxx’});
//Second Set (shard 2)
./mongod --port 27027 --dbpath ../data/r4 --replSet dmr2
./mongod --port 27028 --dbpath ../data/r5 --replSet dmr2
./mongod --port 27029 --dbpath ../data/r6 --replSet dmr2
./mongo --port 27027
cfg = {
"_id" : "dmr2",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:27027"
},
{
"_id" : 1,
"host" : "localhost:27028"
},
{
"_id" : 2,
"host" : "localhost:27029"
}
]
}
rs.initiate(cfg);
rs.status();
//Start the config server
./mongod --configsvr --port 10000 --dbpath ../data/config/
//Start mongos
./mongos --port 20000 --configdb localhost:10000
//from the mongos shell
use admin
sh.addShard('dmr/localhost:27017');
sh.addShard('dmr2/localhost:27027');
//chunksize of 1 for testing only
use config
db.settings.save({"_id" : "chunksize", "value" : 1 })
use test
sh.enableSharding(‘test’);
//Default ObjectId() _id values are generally a bad idea for shard keys.
sh.shardCollection('test.stuff',{'_id':1});
//Make sure you are pointing at the correct database.
use test
//Insert lots of data
for(i=0;i<100000;i++) {
db.stuff.insert({data:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment