How to Configure MongoDB HA Replica Set on AWS EC2

mongothumbnail How to Configure MongoDB HA Replica Set on AWS EC2It has been always tedious task for choosing right configuration for MongoDB on AWS EC2. Choosing right configuration in this environment is always a challenging and it takes lots of time to make your system Production Ready.

You can use following configuration and steps to install MongoDB in EC2 environment for creating Production Ready HA replica set.

All it needs is two machines that will be used as PRIMARY(Master) and SECONDARY (Slave) node and one ARBITER machine for the replica set. However it might get changed based on your application requirement and you can opt for higher number of nodes based on your need. ARBITER is only required in case of even number replica set. If you want to maintain replica set with one PRIMARY and two SECONDARY, ARBITER is not required.

Hardware Requirement

  1. Two 64 bit EC2 instances of medium/large or higher configuration based on your app requirement for PRIMARY and SECONDARY node. There is a data storage limitation of using 32 bit machine and can only support up-to 2.5 GB of storage.
  2. A small 32 bit EC2 machine for MongoDB ARBITER.
  3. It is recommended to have machines in different availability zone to make it High available in-case shutdown of one availability zone.
  4. Use Ext4 EBS volume to support I/O suspend and write-cache flushing for multi-disk consistent snapshots.

Installation steps

  1. Create and Launch an EC2 instance of required configuration as stated above for PRIMARY, SECONDARY and ARBITER nodes.
  2. Create an EBS volume of required size to be used for MongoDB storage for both nodes.
  3. Connect to EC2 instances on PRIMARY and SECONDARY node via SSH
  4. Make a Ext4 file system on both node via
    sudo mkfs -t ext4 /dev/<Creat _EBS_Volume>
  5. Create directory /data/db or any other of your own choice and mount it to attached volume using
    sudo mount -a /dev/ <Creat_EBS_Volume> /data/db
  6. Edit your /etc/fstab to enumerate it on start up of instance using
    sudo echo ‘/dev/sdf /data/db auto noatime,noexec,nodiratime 0 0>> /etc/fstab
  7. Download and Install MongoDB on all instances.
  8. Start the PRIMARY node with following command in MongoDB directory using
    mongod --rest --replSet myHASet (Where myHASet is the name of Replica set, you can choose any name of your choice)
  9. Go to Mongo treminal in MongoDB directory.
  10. Initialize the set using command rs.initiate() on mongo terminal
  11. Check the status of Replicat set after initialization using rs.status() command.
  12. If initialization is success you will see OK in the output something like this
    "set" : "sample",
    "myState" : 1,
    "members" : [
    "name" : "<PRIMARY_HOSTNAME>:27017",
    "self" : true
    "ok" : 1
    	<li>You can also check the status on http://<PRIMARY_NODE>:27017/_replSet</li>
    	<li>Your Primary node is ready to use now. You can insert/update document on this node.</li>
    	<li>Now start SECONDARY node with same command as on primary <pre lang="java" line="1">mongod --rest --replSet myHASet
  13. Now tell the PRIMARY node to add SECONDARY node in replica set. Go to mongo console on PRIMARY node and add this using
  14. If addition is successful you will see the response
    { "ok" : 1 }
  15. Once your SECONDARY node is attached to replica set you can check the status on http://:27017/_replSet
  16. Now start the ARBITER node using
    mongod --rest --replSet myset --oplogSize 8
  17. Add the ARBITER node in replica set using command
    rs.add( { _id:2, host:<ARBITER_HOSTNAME>;”, arbiterOnly:true } )
  18. Once ARBITER is added successfully, you are done with the configuration and your replica set is ready to use.
  19. Got o http://:27017/_replSet and you should be able to see the status of each node as seen in below scree shot.
  20. ReplicaSet1 300x113 How to Configure MongoDB HA Replica Set on AWS EC2
  21. To test the replica, take down the primary node, and see if SECONDARY is able to pick up and will become PRIMARY node.
  22. You can fire the command db.isMaster() to check the status if SECONDARY node has turned up as Master node.
  23. You can additionally use horizontal sharding using shard cluster for scaling large volume of app data. Configuring shard cluster is not in the scope of this article.

Connecting Replica Set from JAVA API

  • After you have setup the replica set successfully, you can connect with using JAVA driver from your client application.
  • You can use following code snippet for making connection to replica set
List addrs = new ArrayList();
addrs.add( new ServerAddress( "<PRIMARY_HOST>", "<MONGO_PORT>" ) );
addrs.add( new ServerAddress("< SECONDARY_HOST>" , "<MONGO_PORT>"));
Mongo m = new Mongo(addrs);
DB db = m.getDB("<NAME_OF_DB>");
  • MongoDB driver is smart enough to connect to PRIMARY node only, in-case if PRIMARY node is down, it will automatically switch to another node for communication.

Here is an honest attempt to guide you to setup MongoDB on AWS EC2. Though this is an open forum and you all are open to post your comments if I have missed anything.

Also, if you don’t want to get into setting up the infrastructure and administration for MongoDB, you can directly use our App42 NoSQL Cloud Storage Service. This service can be accessed using our REST API or using native platform SDKs available in different languages like iOS, Android,J2ME, JAVA, PHP, Ruby, Windows Phone and C#.

more on pricing How to Configure MongoDB HA Replica Set on AWS EC2get started How to Configure MongoDB HA Replica Set on AWS EC2

twitter How to Configure MongoDB HA Replica Set on AWS EC2facebook How to Configure MongoDB HA Replica Set on AWS EC2google How to Configure MongoDB HA Replica Set on AWS EC2linkedin How to Configure MongoDB HA Replica Set on AWS EC2pinterest How to Configure MongoDB HA Replica Set on AWS EC2reddit How to Configure MongoDB HA Replica Set on AWS EC2stumbleupon How to Configure MongoDB HA Replica Set on AWS EC2tumblr How to Configure MongoDB HA Replica Set on AWS EC2

Leave a Reply

Your email address will not be published. Required fields are marked *

seven − 6 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>