MongoDB read replica setup with Ec2 instances

2:09 AM 0 Comments

MongoDB setup  with ec2 instances, odd no of ec2 instances should be used which helps in polling when the instances are down. Backup and restore will be easily managed when ebs storage volume is used and mongodb is setup on a independent instance.

Let's say we decide to go with 1 primary and 2 secondary instance as read replicas.
--Install mongodb on all the instances or install on one instance and launch similar instances using aws console.
--Attach ebs volumes for db,log and journal files in each of the instance. Change the path to these folders in /etc/mongodb.conf. Make sure mongod has read and write permission on these folders.

Now restart mongodb server on each of the instance to make sure changes done are working fine.
$sudo /etc/init.d/mongodb restart

Replication Steps:
Copy this line at the bottom of  mongodb.conf file,  replSet = rs0, and specify the same replica set name on same instance.

Now, start each mongod instance

        mongod --config /etc/mongodb.conf

Open mongo shell in any of the instance by hitting $mongo in the terminal and execute the commands below.

$ rs.initiate()   -- Initiates the replica set
$ rs.conf()        -- Displays the current replica set configuration

Add the other members to the replica,
rs.add("ec2-address:27017")  --secondary

For more basic steps, Refer Deploy a Replica Set- MongoDB 

When Mongodb setup on ec2 instances setup on primary and secondary with read replicas.

If any of the replica stops working,

cfg = rs.conf()

"_id" : "rs0",
"version" : 2,
"members" : [
"_id" : 0,
"host" : "ip-10-128-149-4:27017"
"_id" : 1,
"host" : ""

rs0:SECONDARY> cfg.members=[cfg.members[0]]
[ { "_id" : 0, "host" : "ip-10-128-149-4:27017" } ]
rs0:SECONDARY> rs.reconfig(cfg, {force : true})
{ "ok" : 1 }
rs0:SECONDARY> rs.status()

"set" : "rs0",
"date" : ISODate("2013-11-13T21:04:24Z"),
"myState" : 1,
"members" : [
"_id" : 0,
"name" : "ip-10-128-149-4:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 15772,
"optime" : Timestamp(1384376658, 1),
"optimeDate" : ISODate("2013-11-13T21:04:18Z"),
"self" : true
"ok" : 1
rs0:PRIMARY> rs.add("")
{ "ok" : 1 }
rs0:PRIMARY> rs.status

Bijendra Singh

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.