How to Configure ReplicaSet in MongoDB on One Node

Connect with

How to Configure ReplicaSet in MongoDB on One Node
Very essential to know about, How to Configure ReplicaSet in MongoDB on One Node? Configuring of replica set on MongoDB is very easy, if you know the basics of MongoDB and replication. My environment for this test Ubuntu 14, MongoDB 3.

Step 1. Install mongoDB

Install mongoDB on you Linux Ubuntu or CentOS of you choise of your version.

Step 2. Create Folder data directory

Create Folder data directory where you want to store the mongoDB data

m202@m202-ubuntu1404:~$ mkdir -p /data/db/rs202/rs1 /data/db/rs202/rs2 /data/db/rs202/rs3
m202@m202-ubuntu1404:~$ ls /data/db/rs202/
rs1  rs2  rs3

Step 3. Start the mongod process

Open a terminal and start the MongoDB i.e. mongod process with different required parameters. The provided parameters just to traces and to maintain simplicity.
Commands to type on the terminal or you can create a .sh file and run that .sh file in order to spawn three processes of mongod , one for Primary (port 27017), second for secondary ( port 27018), and third for Arbiter (port: 27019).

 
mongod --replSet m202 --logpath "rs1.log" --dbpath /data/db/rs202/rs1 --port 27017 --smallfiles --oplogSize 100 --fork
mongod --replSet m202 --logpath "rs2.log" --dbpath /data/db/rs202/rs2 --port 27018 --smallfiles --oplogSize 100 --fork
mongod --replSet m202 --logpath "rs3.log" --dbpath /data/db/rs202/rs3 --port 27019 --smallfiles --oplogSize 100 --fork

Output by typing one by one on terminal as follows:

m202@m202-ubuntu1404:~$ mongod --replSet m202 --logpath "rs1.log" --dbpath /data/db/rs202/rs1 --port 27017 --smallfiles --oplogSize 100 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4598
child process started successfully, parent exiting
m202@m202-ubuntu1404:~$ mongod --replSet m202 --logpath "rs2.log" --dbpath /data/db/rs202/rs2 --port 27018 --smallfiles --oplogSize 100 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4614
child process started successfully, parent exiting
m202@m202-ubuntu1404:~$ mongod --replSet m202 --logpath "rs3.log" --dbpath /data/db/rs202/rs3 --port 27019 --smallfiles --oplogSize 100 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4630
child process started successfully, parent exiting
m202@m202-ubuntu1404:~$

Step 4. Connect to primary and Config Replicaset

Connect to primary mongoDB server by using provided port for primary mongo server and follow following step to configuration of replication.

Replica set Config as:

config = { _id: "m202", members:[
{ _id : 0, host : "localhost:27017"},
{ _id : 1, host : "localhost:27018"}]
};
rs.initiate(config);
rs.status();

Step 5. Add Orbital

rs.addArb(“localhost:27019”); Terminal Output of step 4 and 5 as follows:
m202@m202-ubuntu1404:~$ mongo --port 27017
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27017/test
Server has startup warnings: 
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] 
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] 
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-26T14:29:26.191+0000 I CONTROL  [initandlisten] 
> config = { _id: "m202", members:[
... { _id : 0, host : "localhost:27017"},
... { _id : 1, host : "localhost:27018"}]
... };
{
	"_id" : "m202",
	"members" : [
		{
			"_id" : 0,
			"host" : "localhost:27017"
		},
		{
			"_id" : 1,
			"host" : "localhost:27018"
		}
	]
}
> rs.initiate(config);
{ "ok" : 1 }
m202:OTHER> rs.status();
{
	"set" : "m202",
	"date" : ISODate("2016-01-26T14:32:15.100Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "localhost:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 171,
			"optime" : Timestamp(1453818729, 1),
			"optimeDate" : ISODate("2016-01-26T14:32:09Z"),
			"electionTime" : Timestamp(1453818733, 1),
			"electionDate" : ISODate("2016-01-26T14:32:13Z"),
			"configVersion" : 1,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "localhost:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 5,
			"optime" : Timestamp(1453818729, 1),
			"optimeDate" : ISODate("2016-01-26T14:32:09Z"),
			"lastHeartbeat" : ISODate("2016-01-26T14:32:13.339Z"),
			"lastHeartbeatRecv" : ISODate("2016-01-26T14:32:13.358Z"),
			"pingMs" : 0,
			"configVersion" : 1
		}
	],
	"ok" : 1
}
m202:PRIMARY> rs.addArb("localhost:27019");
{ "ok" : 1 }
m202:PRIMARY> use testDB;
switched to db testDB
m202:PRIMARY> db.testColl.insert({"a":"anushka"});
WriteResult({ "nInserted" : 1 })
m202:PRIMARY> db.testColl.insert({"a":"tanisha"});
WriteResult({ "nInserted" : 1 })
m202:PRIMARY> ^C
bye
m202@m202-ubuntu1404:~$ mongo --port 27018
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27018/test
Server has startup warnings: 
2016-01-26T14:29:42.665+0000 I CONTROL  [initandlisten] 
2016-01-26T14:29:42.665+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-01-26T14:29:42.665+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-26T14:29:42.665+0000 I CONTROL  [initandlisten] 
2016-01-26T14:29:42.666+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-01-26T14:29:42.666+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-01-26T14:29:42.666+0000 I CONTROL  [initandlisten] 
m202:SECONDARY> rs.getMongo().setSlaveOK();
2016-01-26T14:36:11.336+0000 E QUERY    TypeError: Object function () { return "try rs.help()"; } has no method 'getMongo'
    at (shell):1:4
m202:SECONDARY> db.getMongo().setSlaveOk();
m202:SECONDARY> use testDB
switched to db testDB
m202:SECONDARY> show collections
system.indexes
testColl
m202:SECONDARY> db.testColl.find();
{ "_id" : ObjectId("56a783c4483f3dc7768f95a5"), "a" : "anushka" }
{ "_id" : ObjectId("56a783cb483f3dc7768f95a6"), "a" : "tanisha" }
m202:SECONDARY> ^C
bye

In the above you can see, added data on primary and it replicated to seconday , it means our replication is done. In terminal output , ^C on terminal means , Holding ctrl key and pressed C , it means “ctrl + C” action.

Step 6. Connect to Secondary and Check replication data

Terminal Command

m202@m202-ubuntu1404:~$ mongo --port 27018
m202:SECONDARY> db.getMongo().setSlaveOk();
m202:SECONDARY> use testDB
switched to db testDB
m202:SECONDARY> show collections
system.indexes
testColl
m202:SECONDARY> db.testColl.find();
{ "_id" : ObjectId("56a783c4483f3dc7768f95a5"), "a" : "anushka" }
{ "_id" : ObjectId("56a783cb483f3dc7768f95a6"), "a" : "tanisha" }

References

  • MongoDB

Connect with

1 thought on “How to Configure ReplicaSet in MongoDB on One Node”

  1. Pingback: sweta

Leave a Comment

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