mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128 

This starts each instance as a member of a replica set named rs0 , each running on a distinct port, and specifies the path to your data directory with the --dbpath setting. If you are already using the suggested ports, select different ports. 

The --smallfiles and --oplogSize settings reduce the disk space that each mongod instance uses. This is ideal for testing and development deployments as it prevents over- loading your machine. For more information on these and other configuration options, see http://docs.mongodb.org/manual/reference/configuration-options . 

3. Connect to one of your mongod instances through the mongo shell. You will need to indicate which instance by specifying its port number. For the sake of simplicity and clarity, you may want to choose the first one, as in the following command; 

mongo --port 27017 

4. In the mongo shell, use rs.initiate() to initiate the replica set. You can create a replica set configuration object in the mongo shell environment, as in the following example: 

rsconf = { _id: "rs0", members: [ { _id: 0, host: "<hostname>:27017" } ] } 

replacing <hostname> with your system’s hostname, and then pass the rsconf file to rs.initiate() as follows: 

rs.initiate( rsconf ) 

5. Display the current replica configuration (page 652) by issuing the following command: 

rs.conf() 

The replica set configuration object resembles the following 

{ "_id" : "rs0", "version" : 4, "members" : [ { "_id" : 1, "host" : "localhost:27017" } ] } 

6. In the mongo shell connected to the primary , add the second and third mongod instances to the replica set using the rs.add() method. Replace <hostname> with your system’s hostname in the following examples: 

rs.add("<hostname>:27018") rs.add("<hostname>:27019") 

When complete, you should have a fully functional replica set. The new replica set will elect a primary . 

