For demo purpose we are going to configure three servers primary, secondary and arbiter servers and we are going to do this in local computer for testing purpose only in real world you need separate servers or you can use virtual machine to use seprate virtual machines for each servers,open command line and paste the following code.

cd \dbdiary\
md \dbdiary\db1
md \dbdiary\db2
md \dbdiary\db3

As you can see we have created 3 sub-directory to our main data file directory which we store data file and configuration of our database for primary, secondary and arbiter, to start each instance we need to issue bellow commands:

java; highlight: [2,5]; title: ; notranslate" title="">

@REM Primary
Start “a” mongod –dbpath ./db1 –port 3000 replSet “demo”
@REM Secondary
Start “a” mongod –dbpath ./db1 –port 4000 replSet “demo”
@REM Arbiter
Start “a” mongod –dbpath ./db1 –port 5000 replSet “demo”

You will see that I’m setting their port to be different from each instance as I’m running this all in one machine I need each of them to listen to their own separate port, the option replSet will tell all those instances that they are going to be participating in a replica set called “Demo”.

Replica Set Configuration Replica Set Configuration

As of now we have set our instance to participate in replica set “Demo” but we have not set our configuration for replica set so at the moment they are running but does not know anything about how many servers they need to participate with and where they are running either locally or in cloud, since mongo’s internal language is JavaScript we are going to create the configuration in JavaScript which is straight forward, bellow is the configuration file for the instances along with their roles in replica set.

var demoConfig={
    "_id" : "dbdiary_demo",
      "members" : [
                  {
                     "_id" : 0,
                     "host" : "localhost:3000"
		     "Priority" = 10;
                  },
                  {
                     "_id" : 1,
                     "host" : "localhost:4000"
                  },
                  {
                     "_id" : 2,
                     "host" : "localhost:5000"
		     "arbiterOnly" = true
                  }
     ]
}

As you can see we have configured a configuration file named demoConfig and also we have set each server to listen to which port and their roles as primary or arbiter in the primary section we have given priority which means if we set the highest priority between those servers that will be set as primary server and in this configuration we have set it to 10 and others are null which means that server is going to be our primary server, and also one of the server in configuration is to to act as arbiter.

This is the simplest configuration file which will work for almost all kinds of purpose but you have the power to change and customize it according to your need, there is many other option you can use in configuration to allow the mongo works perfect for your needs.

Configuration of replicaSet in mongodb Configuration of replicaSet in mongodb

As you can see in above screenshot we run our configuration file for the replica set and you can see the prompt for mongodb is changed from null to the server it’s currently connected to and that tell us about our configuration is done successfully, at the moment our servers are ready to get any write command to write it in document.

Hope you find this use full in the next blog post I will be showing you how to configure this please visit tomorrow for that post, and do let me know if you want me to write about any specific subject.