Skip to main content

MongoDB read replica setup with Ec2 instances

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


Popular posts from this blog

upload images to AWS::S3 in ruby using aws sdk gem

Using gem aws-sdk for a ROR application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application. The s3 keeps a limitation on the number of buckets creation whereas there is no limitation for content inside a bucket. This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public, so that the images uploaded are directly accessible. The input is takes is the image complete path where it is present, folder in which it should be uploaded and user_id for whom it should be uploaded. def save_screenshot_to_s3(image_location, folder_name,user_id) service = => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY) bucket_name = "app-images" if(service.buckets.include?(bucket_name)) bucket = service.buckets[bucket_name] else bucket = service.buckets.create(bucket_name) …

Fresh server start after instance degraded

So the issue was, amazon issued an warning regarding the server to be terminated, before i could think of making an backup the instance stopped.

The troubleshoot operations like reboot, stop and start didn't worked.
Simple Steps to start a fresh instance with the previous content on server.