Load balancer or replica set of mongo db

Description:
Mongo db load balance
For more under standing click below link 
https://drive.google.com/open?id=0B7xaspetzd9-UE80RmdCbHp5X2c


Code 1:
?
Or Download mongodb-win32-x86_64-3.0.7.zip file
1.        Unzip a particular location like : D:\Software\MongoDb
2.        Inside D drive create a folder like data
3.        Again create another three folder like db,db1,db2
Below command 
d:>mkdir \data\rs1 \data\rs2 \data\rs3
D:\Software\MongoDb\mongodb\bin>mongod --replSet kartik --logpath \data\rs1\1.log --dbpath \data\rs1 --port 27017 --smallfiles --oplogSize 64

4.        Now start the mongo service by below command below:
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

a.        D:\Software\MongoDb\mongodb\bin>mongod --replSet kartik --logpath \data\rs1\1.log --dbpath \data\rs1 --port 27017 --smallfiles --oplogSize 64

b.        D:\Software\MongoDb\mongodb\bin>mongod --replSet kartik --logpath \data\rs2\2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64

c.         D:\Software\MongoDb\mongodb\bin>mongod --replSet kartik --logpath \data\rs3\3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64

5.        Now start the mongo server below command:
                                                                         i.       mongo.exe or mongo --host "host name" --port 27017 like mongo --host "192.168.208.181" --port 27017
6.        D:\Software\MongoDb\mongodb\bin>mongo.exe   or goto line no 199
7.        MongoDB shell version: 3.0.7
8.        connecting to: test
9.        > rs.initiate()
10.     {
11.             "info2" : "no configuration explicitly specified -- making one",
12.             "me" : "IN-L1155:27017",
13.             "ok" : 1
14.     }
15.     kartik:SECONDARY> rs.add("192.168.65.143:27017")
16.     {
17.             "ok" : 0,
18.             "errmsg" : "Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: IN-L1155:27017; the following nodes did not respond affirmatively: 192.168.65.143:27017 failed with Failed attempt to connect to 192.168.65.143:27017; couldn't connect to server 192.168.65.143:27017 (192.168.65.143), connection attempt failed",
19.             "code" : 74
20.     }
21.     kartik:PRIMARY> rs.add("192.168.65.143:27017")
22.     {
23.             "ok" : 0,
24.             "errmsg" : "Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: IN-L1155:27017; the following nodes did not respond affirmatively: 192.168.65.143:27017 failed with Failed attempt to connect to 192.168.65.143:27017; couldn't connect to server 192.168.65.143:27017 (192.168.65.143), connection attempt failed",
25.             "code" : 74
26.     }
27.      
28.     kartik:PRIMARY> rs.add("192.168.208.181:27018")
29.     { "ok" : 1 }
30.     kartik:PRIMARY> rs.config()
31.     {
32.             "_id" : "rs0",
33.             "version" : 2,
34.             "members" : [
35.                     {
36.                             "_id" : 0,
37.                             "host" : "IN-L1155:27017",
38.                             "arbiterOnly" : false,
39.                             "buildIndexes" : true,
40.                             "hidden" : false,
41.                             "priority" : 1,
42.                             "tags" : {
43.      
44.                             },
45.                             "slaveDelay" : 0,
46.                             "votes" : 1
47.                     },
48.                     {
49.                             "_id" : 1,
50.                             "host" : "192.168.208.181:27018",
51.                             "arbiterOnly" : false,
52.                             "buildIndexes" : true,
53.                             "hidden" : false,
54.                             "priority" : 1,
55.                             "tags" : {
56.      
57.                             },
58.                             "slaveDelay" : 0,
59.                             "votes" : 1
60.                     }
61.             ],
62.             "settings" : {
63.                     "chainingAllowed" : true,
64.                     "heartbeatTimeoutSecs" : 10,
65.                     "getLastErrorModes" : {
66.      
67.                     },
68.                     "getLastErrorDefaults" : {
69.                             "w" : 1,
70.                             "wtimeout" : 0
71.                     }
72.             }
73.     }
74.     kartik:PRIMARY> rs.add("192.168.208.181:27019")
75.     { "ok" : 1 }
76.     kartik:PRIMARY> rs.config()
77.     {
78.             "_id" : "rs0",
79.             "version" : 3,
80.             "members" : [
81.                     {
82.                             "_id" : 0,
83.                             "host" : "IN-L1155:27017",
84.                             "arbiterOnly" : false,
85.                             "buildIndexes" : true,
86.                             "hidden" : false,
87.                             "priority" : 1,
88.                             "tags" : {
89.      
90.                             },
91.                             "slaveDelay" : 0,
92.                             "votes" : 1
93.                     },
94.                     {
95.                             "_id" : 1,
96.                             "host" : "192.168.208.181:27018",
97.                             "arbiterOnly" : false,
98.                             "buildIndexes" : true,
99.                             "hidden" : false,
100.                           "priority" : 1,
101.                           "tags" : {
102.    
103.                           },
104.                           "slaveDelay" : 0,
105.                           "votes" : 1
106.                   },
107.                   {
108.                           "_id" : 2,
109.                           "host" : "192.168.208.181:27019",
110.                           "arbiterOnly" : false,
111.                           "buildIndexes" : true,
112.                           "hidden" : false,
113.                           "priority" : 1,
114.                           "tags" : {
115.    
116.                           },
117.                           "slaveDelay" : 0,
118.                           "votes" : 1
119.                   }
120.           ],
121.           "settings" : {
122.                   "chainingAllowed" : true,
123.                   "heartbeatTimeoutSecs" : 10,
124.                   "getLastErrorModes" : {
125.    
126.                   },
127.                   "getLastErrorDefaults" : {
128.                           "w" : 1,
129.                           "wtimeout" : 0
130.                   }
131.           }
132.   }
133.   kartik:PRIMARY> rs.status()
134.   {
135.           "set" : "rs0",
136.           "date" : ISODate("2016-10-13T12:29:48.413Z"),
137.           "myState" : 1,
138.           "members" : [
139.                   {
140.                           "_id" : 0,
141.                           "name" : "IN-L1155:27017",
142.                           "health" : 1,
143.                           "state" : 1,
144.                           "stateStr" : "PRIMARY",
145.                           "uptime" : 3268,
146.                           "optime" : Timestamp(1476361776, 1),
147.                           "optimeDate" : ISODate("2016-10-13T12:29:36Z"),
148.                           "electionTime" : Timestamp(1476358572, 2),
149.                           "electionDate" : ISODate("2016-10-13T11:36:12Z"),
150.                           "configVersion" : 3,
151.                           "self" : true
152.                   },
153.                   {
154.                           "_id" : 1,
155.                           "name" : "192.168.208.181:27018",
156.                           "health" : 1,
157.                           "state" : 2,
158.                           "stateStr" : "SECONDARY",
159.                           "uptime" : 136,
160.                           "optime" : Timestamp(1476361776, 1),
161.                           "optimeDate" : ISODate("2016-10-13T12:29:36Z"),
162.                           "lastHeartbeat" : ISODate("2016-10-13T12:29:46.551Z"),
163.                           "lastHeartbeatRecv" : ISODate("2016-10-13T12:29:48.213Z"),
164.                           "pingMs" : 0,
165.                           "syncingTo" : "IN-L1155:27017",
166.                           "configVersion" : 3
167.                   },
168.                   {
169.                           "_id" : 2,
170.                           "name" : "192.168.208.181:27019",
171.                           "health" : 1,
172.                           "state" : 2,
173.                           "stateStr" : "SECONDARY",
174.                           "uptime" : 11,
175.                           "optime" : Timestamp(1476361776, 1),
176.                           "optimeDate" : ISODate("2016-10-13T12:29:36Z"),
177.                           "lastHeartbeat" : ISODate("2016-10-13T12:29:46.554Z"),
178.                           "lastHeartbeatRecv" : ISODate("2016-10-13T12:29:46.570Z"),
179.                           "pingMs" : 0,
180.                           "configVersion" : 3
181.                   }
182.           ],
183.           "ok" : 1
184.   }
185.   kartik:PRIMARY>



186.   D:\Software\MongoDb\mongodb\bin>mongo --host "192.168.208.181" --port 27018
187.   MongoDB shell version: 3.0.7
188.   connecting to: 192.168.208.181:27018/test
189.   kartik:SECONDARY> use docstorage
190.   switched to db docstorage
191.   kartik:SECONDARY> rs.slaveOk()
192.   kartik:SECONDARY> use docstorage
193.   switched to db docstorage
194.   kartik:SECONDARY> show collections
195.   Data
196.   system.indexes
197.   kartik:SECONDARY> db.data.find()
198.   kartik:SECONDARY>

199.   D:\Software\MongoDb\mongodb\bin>mongo --port 27017
200.   MongoDB shell version: 3.0.7
201.   connecting to: 127.0.0.1:27017/test
202.   kartik:PRIMARY> rs.add("IN-L1155:27018")
203.   { "ok" : 1 }
204.   kartik:PRIMARY> rs.add("IN-L1155:27019")
205.   { "ok" : 1 }
206.   kartik:PRIMARY> rs.status()
207.   {
208.           "set" : "kartik",
209.           "date" : ISODate("2016-11-16T09:57:50.873Z"),
210.           "myState" : 1,
211.           "members" : [
212.                   {
213.                           "_id" : 0,
214.                           "name" : "IN-L1155:27017",
215.                           "health" : 1,
216.                           "state" : 1,
217.                           "stateStr" : "PRIMARY",
218.                           "uptime" : 884,
219.                           "optime" : Timestamp(1479290263, 1),
220.                           "optimeDate" : ISODate("2016-11-16T09:57:43Z"),
221.                           "electionTime" : Timestamp(1479289556, 2),
222.                           "electionDate" : ISODate("2016-11-16T09:45:56Z"),
223.                           "configVersion" : 3,
224.                           "self" : true
225.                   },
226.                   {
227.                           "_id" : 1,
228.                           "name" : "IN-L1155:27018",
229.                           "health" : 1,
230.                           "state" : 2,
231.                           "stateStr" : "SECONDARY",
232.                           "uptime" : 16,
233.                           "optime" : Timestamp(1479290263, 1),
234.                           "optimeDate" : ISODate("2016-11-16T09:57:43Z"),
235.                           "lastHeartbeat" : ISODate("2016-11-16T09:57:49.065Z"),
236.                           "lastHeartbeatRecv" : ISODate("2016-11-16T09:57:50.062Z"),
237.                           "pingMs" : 0,
238.                           "syncingTo" : "IN-L1155:27017",
239.                           "configVersion" : 3
240.                   },
241.                   {
242.                           "_id" : 2,
243.                           "name" : "IN-L1155:27019",
244.                           "health" : 1,
245.                           "state" : 2,
246.                           "stateStr" : "SECONDARY",
247.                           "uptime" : 7,
248.                           "optime" : Timestamp(1479290263, 1),
249.                           "optimeDate" : ISODate("2016-11-16T09:57:43Z"),
250.                           "lastHeartbeat" : ISODate("2016-11-16T09:57:49.067Z"),
251.                           "lastHeartbeatRecv" : ISODate("2016-11-16T09:57:49.082Z"),
252.                           "pingMs" : 2,
253.                           "syncingTo" : "IN-L1155:27018",
254.                           "configVersion" : 3
255.                   }
256.           ],
257.           "ok" : 1
258.   }
259.   kartik:PRIMARY> db.assignment.insert({name:"Java",duedate:"25th January"});
260.   WriteResult({ "nInserted" : 1 })
261.   kartik:PRIMARY> db.assignment.find().preety();
262.   2016-11-16T15:32:55.783+0530 E QUERY    TypeError: Object DBQuery: test.assignment -> { } has no method 'preety'
263.       at (shell):1:22
264.   kartik:PRIMARY> db.assignment.find().pretty();
265.   {
266.           "_id" : ObjectId("582c2e925f6adbe1d3d069b2"),
267.           "name" : "Java",
268.           "duedate" : "25th January"
269.   }
270.           kartik:PRIMARY>

271.   D:\Software\MongoDb\mongodb\bin>mongo --port 27018
272.   MongoDB shell version: 3.0.7
273.   connecting to: 127.0.0.1:27018/test
274.   kartik:SECONDARY> db.assignment.find().pretty();
275.   Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
276.   kartik:SECONDARY> rs.slaveOk()
277.   kartik:SECONDARY> db.assignment.find().pretty();
278.   {
279.           "_id" : ObjectId("582c2e925f6adbe1d3d069b2"),
280.           "name" : "Java",
281.           "duedate" : "25th January"
282.   }
283.          kartik:SECONDARY>





Code 2: Load balance of mongo db 
?

Previous
Next Post »