mongodb building a sharded environment

We are going to create a sharded mongodb environment locally on in machine using many non standard ports. 3 shards with each having 3 replicas. 3 configuration servers and 1 mongos server on standard port 27017. The mongos server handle all the incoming requests and then distribute to the apppariate shard server for the requests.

Create the frist shard with 3 mongodb replica nodes and name it shard1.
[code language=”shell”]
mkdir -p /data/shard1/rs1 /data/shard1/rs2 /data/shard1/rs3 /data/log
mongod –replSet shard1 –logpath "/data/log/shard1-replica1.log" –dbpath /data/shard1/rs1 –port 37017 –fork –shardsvr
mongod –replSet shard1 –logpath "/data/log/shard1-replica2.log" –dbpath /data/shard1/rs2 –port 37018 –fork –shardsvr
mongod –replSet shard1 –logpath "/data/log/shard1-replica3.log" –dbpath /data/shard1/rs3 –port 37019 –fork –shardsvr

mongo –port 37017 << ‘EOF’
config = { _id: "shard1", members:[
{ _id : 1, host : "localhost:37017" },
{ _id : 2, host : "localhost:37018" },
{ _id : 3, host : "localhost:37019" }]};
rs.initiate(config);
EOF
[/code]

Create the second shard with 3 mongodb replica nodes and name it shard2.
[code language=”shell”]
mkdir -p /data/shard2/rs1 /data/shard2/rs2 /data/shard2/rs3
mongod –replSet shard2 –logpath "/data/log/shard2-replica1.log" –dbpath /data/shard2/rs1 –port 47017 –fork –shardsvr
mongod –replSet shard2 –logpath "/data/log/shard2-replica2.log" –dbpath /data/shard2/rs2 –port 47018 –fork –shardsvr
mongod –replSet shard2 –logpath "/data/log/shard2-replica3.log" –dbpath /data/shard2/rs3 –port 47019 –fork –shardsvr

mongo –port 47017 << ‘EOF’
config = { _id: "shard2", members:[
{ _id : 1, host : "localhost:47017" },
{ _id : 2, host : "localhost:47018" },
{ _id : 3, host : "localhost:47019" }]};
rs.initiate(config);
EOF
[/code]

Create the third shard with 3 mongodb replica nodes. and name it shard3.
[code language=”shell”]
mkdir -p /data/shard3/rs1 /data/shard3/rs2 /data/shard3/rs3
mongod –replSet shard3 –logpath "/data/log/shard3-replica1.log" –dbpath /data/shard3/rs1 –port 57017 –fork –shardsvr
mongod –replSet shard3 –logpath "/data/log/shard3-replica2.log" –dbpath /data/shard3/rs2 –port 57018 –fork –shardsvr
mongod –replSet shard3 –logpath "/data/log/shard3-replica3.log" –dbpath /data/shard3/rs3 –port 57019 –fork –shardsvr

mongo –port 57017 << ‘EOF’
config = { _id: "shard3", members:[
{ _id : 1, host : "localhost:57017" },
{ _id : 2, host : "localhost:57018" },
{ _id : 3, host : "localhost:57019" }]};
rs.initiate(config);
EOF
[/code]

Create mongodb configuration servers, start mongos on standard port and add the 3 shards to enable sharding.
[code language=”shell”]
# create mongodb configuration servers.
mkdir -p /data/config/config-a /data/config/config-b /data/config/config-c
mongod –logpath "cfg-a.log" –dbpath /data/config/config-a –port 57040 –fork –configsvr
mongod –logpath "cfg-b.log" –dbpath /data/config/config-b –port 57041 –fork –configsvr
mongod –logpath "cfg-c.log" –dbpath /data/config/config-c –port 57042 –fork –configsvr

# start the mongos on a standard port
mongos –logpath "/data/log/mongos-1.log" –configdb localhost:57040,localhost:57041,localhost:57042 –fork
echo "Connnecting to mongos and enabling sharding"

# add shards and enable sharding on the test db
mongo <<‘EOF’
db.adminCommand( { addshard : "shard1/"+"localhost:37017" } );
db.adminCommand( { addshard : "shard2/"+"localhost:47017" } );
db.adminCommand( { addshard : "shard3/"+"localhost:57017" } );
db.adminCommand({enableSharding: "world"})
db.adminCommand({shardCollection: "world.people", key: {pid:1}});
EOF
[/code]

Insert some documents into the database world in people collection.
[code language=”shell”]
mongo <<‘EOF’
var db=db.getSiblingDB("world");
var person;
// 10,000 people
for (i=0; i<10000; i++) {
person = {‘pid’: i, ‘income’: Math.random()*10000000};
db.people.insert(person);
}
EOF
[/code]

Insert some documents into the database world in people colletion using node.js
[code language=”javascript”]
var MongoClient = require(‘mongodb’).MongoClient;
var person;
MongoClient.connect("mongodb://localhost:27017/world", function(err, db) {
if (err) throw err;
for (i=10000; i<20000; i++) {
person = {‘pid’: i, ‘income’: Math.random()*10000000};
db.collection("people").insert(person, function (err, doc) {
if (err) throw err;
console.log(doc);
});
}
});
[/code]

Check the document counts on each shard.
[code language=”shell”]
mongo
use world;
db.people.count();
sh.status();
exit;

mongo –port 37017
use world;
db.people.count();
exit;

mongo –port 47017
use world;
db.people.count();
exit;

mongo –port 57017
use world;
db.people.count();
exit;
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search