#Mongo UseCase:
Installing mongodb on 5 machines with the following deamon configurations:
Host | Mongo Role |
---|---|
router.mongo.cw.com | Router(mongos), Application Server, Arbiter (Shard1), Arbiter (Shard2), Config1, Config2, Config3 |
shard1r1.mongo.cw.com | shard1 replica primary |
shard1r2.mongo.cw.com | shard1 replica secondary |
shard2r1.mongo.cw.com | shard2 replica primary |
shard2r2.mongo.cw.com | shard2 replica secondary |
##Install Mongo 2.4 Download and install mongodb on all machines:
cd /opt
curl -o mongodb-2.4.9.tgz http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz
tar xzf mongodb-2.4.9.tgz
rm mongodb*.tgz
ln -s mongodb* mongodb
##Configure ReplicaSet1 and tell it to be shard 1 ####Configure Replica Primary on shard1r1.mongo.cw.com
mkdir -p /data/shard1 /var/log/mongodb
/opt/mongodb/bin/mongod --replSet shard1 --logpath "/var/log/mongodb/shard1.log" --dbpath /data/shard1 --port 27017 --fork --shardsvr --bind_ip shard1r1.mongo.cw.com
####Configure Replica Secondary on shard1r2.mongo.cw.com
mkdir -p /data/shard1 /var/log/mongodb
/opt/mongodb/bin/mongod --replSet shard1 --logpath "/var/log/mongodb/shard1.log" --dbpath /data/shard1 --port 27017 --fork --shardsvr --bind_ip shard1r2.mongo.cw.com
###Configure Replica Arbiter on router.mongo.cw.com
mkdir -p /data/shard1/arbiter /var/log/mongodb
/opt/mongodb/bin/mongod --replSet shard1 --logpath "/var/log/mongodb/arbiter_shard1.log" --dbpath /data/shard1/arbiter --port 47018 --fork --bind_ip router.mongo.cw.com
####Initialize replica set 1 from mongo shell from replica primary 'shard1r1.mongo.cw.com'
Before, doing so wait until all the replicas came up online
/opt/mongodb/bin/mongo --host shard1r1.mongo.cw.com --port 27017 << 'EOF'
rs.initiate()
rs.add('shard1r2.mongo.cw.com:27017')
rs.addArb('router.mongo.cw.com:47018')
EOF
Check if everything is right using : rs.status()
, in the output you should be able to see 3 nodes, with status "PRIMARY", "SECONDARY" & "ARBITER" respectively
##Configure ReplicaSet2 for Shard 2 ####Configure Replica Primary on shard2r1.mongo.cw.com
mkdir -p /data/shard2 /var/log/mongodb
/opt/mongodb/bin/mongod --replSet shard2 --logpath "/var/log/mongodb/shard2.log" --dbpath /data/shard2 --port 27017 --fork --shardsvr --bind_ip shard2r1.mongo.cw.com
####Configure Replica Secondary on shard2r2.mongo.cw.com
mkdir -p /data/shard2 /var/log/mongodb
/opt/mongodb/bin/mongod --replSet shard2 --logpath "/var/log/mongodb/shard2.log" --dbpath /data/shard2 --port 27017 --fork --shardsvr --bind_ip shard2r2.mongo.cw.com
###Configure Replica Arbiter on router.mongo.cw.com
mkdir -p /data/shard2/arbiter /var/log/mongodb
/opt/mongodb/bin/mongod --replSet shard2 --logpath "/var/log/mongodb/arbiter_shard2.log" --dbpath /data/shard2/arbiter --port 47019 --fork --bind_ip router.mongo.cw.com
####Initialize replica set 2 from mongo shell from replica primary 'shard2r1.mongo.cw.com'
/opt/mongodb/bin/mongo --host shard2r1.mongo.cw.com --port 27017 << 'EOF'
rs.initiate()
rs.add('shard2r2.mongo.cw.com:27017')
rs.addArb('router.mongo.cw.com:47019')
EOF
##Configure ConfigServer(s) on 'router.mongo.cw.com'
mkdir -p /data/config/config-a /data/config/config-b /data/config/config-c
/opt/mongodb/bin/mongod --logpath "/var/log/mongodb/cfg-a.log" --dbpath /data/config/config-a --port 37019 --fork --configsvr --bind_ip router.mongo.cw.com
/opt/mongodb/bin/mongod --logpath "/var/log/mongodb/cfg-b.log" --dbpath /data/config/config-b --port 37020 --fork --configsvr --bind_ip router.mongo.cw.com
/opt/mongodb/bin/mongod --logpath "/var/log/mongodb/cfg-c.log" --dbpath /data/config/config-c --port 37021 --fork --configsvr --bind_ip router.mongo.cw.com
##Configure Mongo Router on 'router.mongo.cw.com'
/opt/mongodb/bin/mongos --configdb router.mongo.cw.com:37019,router.mongo.cw.com:37020,router.mongo.cw.com:37021 --fork --logpath "/var/log/mongodb/router.log"
##Enable sharding on the shards ###Connect to mongos instance on router.mongo.cw.com and enable sharding
/opt/mongodb/bin/mongo --port 27017 <<'EOF'
db.adminCommand( { addshard : "shard1/"+"shard1r1.mongo.cw.com:27017" } );
db.adminCommand( { addshard : "shard2/"+"shard2r1.mongo.cw.com:27017" } );
EOF
###Enable sharding for a database 'logs'
/opt/mongodb/bin/mongo --port 27017 <<'EOF'
db.adminCommand({enableSharding: "logs"})
EOF
###Enable sharding for a collection 'logEvents' using hashed key 'record_id'
/opt/mongodb/bin/mongo --port 27017 <<'EOF'
use logs
db.logEvents.ensureIndex( { "record_id": "hashed"} )
sh.shardCollection("logs.logEvents", { "record_id": "hashed" } )
EOF
##Verify
Using sh.status()
from mongos. Output looks something like this:
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("52884c78701249e73d126128")
}
shards:
{ "_id" : "shard1", "host" : "shard1/ip-10-168-129-36.us-west-1.compute.internal:27017,ip-10-169-41-183:27017" }
{ "_id" : "shard2", "host" : "shard2/ip-10-168-10-49.us-west-1.compute.internal:27017,ip-10-168-98-40:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "logs", "partitioned" : true, "primary" : "shard2" }
logs.logEvents
shard key: { "record_id" : "hashed" }
chunks:
shard2 2
shard1 2
{ "record_id" : { "$minKey" : 1 } } -->> { "record_id" : NumberLong("-4611686018427387902") } on : shard2 Timestamp(2, 2)
{ "record_id" : NumberLong("-4611686018427387902") } -->> { "record_id" : NumberLong(0) } on : shard2 Timestamp(2, 3)
{ "record_id" : NumberLong(0) } -->> { "record_id" : NumberLong("4611686018427387902") } on : shard1 Timestamp(2, 4)
{ "record_id" : NumberLong("4611686018427387902") } -->> { "record_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(2, 5)