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

Cannot allocate memory error while computing files - Setup Swap space

ActionView::Template::Error (Cannot allocate memory - nodejs /tmp/execjs20131021-26716-fckzo3.js 2>&1
Nodejs is used to compile javascript at runtime. In RoR applications, this error comes when server is started in production mode which compiles the assets or while running rake assets:precompile.
check for swap space and allocate

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.