How to set up a MongoDB Replica Set

replicasetWe will see how to set up our mongod services in order to deploy a Replica Set and for getting high availability and automatic data replication.

The first thing we need is to have MongoDB installed in our computer. If you do not remember how to do it, you can read these related posts:

Typical configuration

The most common configuration is composed by three nodes. One of them will be the primary and the other two will be secondaries. As we know, the replication is always done from the primary towards the secondaries.

Creating directories

We must create the directory in which we want to store our data. Once this is done we can set up the services. I will use the a0, a1 and a2 directories for my nodes:

First node

We will set up the first node’s service in this way:

Before continuing with the other nodes let’s talk about the options we have used (in the official documentation we can read all of them):

  • replSet  This is the name of the Replica Set our service will belong to.
  • dbpath  The directory where our data will be stored.
  • logpath <path/file> In this log file will be stored all the service activity.
  • port  The port that our machine will use. By default, it will be 27017.
  • fork Our service will run as a daemon.
  • logappend If the log file exists, our information will be added to it without deleting any previous data.
  • smallfiles Useful in development environments where we do not need huge amounts of data.
  • oplogSize  Useful in development environments to limit the size of the file operations log. This file is used in replication.

Remaining nodes

Now that we have understood what we have done, we will set up the two remaining nodes:

It is not necessary to specify any port if our services run in different machines. Thus, all of them will use the 27017 port.

We will check that the three services are running:

Replica Set configuration

At this moment our three services are running in isolation because we have not configured the Replica Set yet. This is what we do now.

We are going to connect ourselves to the first node:

We can check the status of the Replica Set or its configuration by using rs.status() or rs.conf() and we will find that this is not configured yet:

With the following command MongoDB will configure the Replica Set, only with the node we are connected to and with the minimal default configuration:

We can see that our node has been added to the Replica Set:

Now, the prompt is composed by the name of the Replica Set (a), followed by colon and the type of the node. In this case our node is the primary because is the only one in the Replica Set.

We must add the remaining two nodes to our Replica Set:

Now the Replica Set setting is finished:

And, finally, we see that the three nodes are working properly:

We can see that the secondaries are syncing from the primary.  Replication will work automatically with all of our operations.

Reading from a secondary

What must we do to be able to read from a secondary? First of all we must connect to that secondary:

If we try to read now we will notice that we can’t.

We must execute this command first:

Now, we are able to do it:

In a new post we will talk about how to set up a Sharded Cluster.

Leave a comment

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

20 − nineteen =