Sunday 28 August 2016

Replication in mongoDB step by step.



                                               Replication in mongoDB step by step.

Operating system details:-

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

mongoDB version details:-

MongoDB shell version: 2.6.12

Node details(servers)

node1: mongodb1.example.com

node2: mongodb2.example.com

node3: mongodb3.example.com

note:- i configured DNS server

1) create directory for dbpath (location of files)

Node1:

-bash-3.2$ pwd
/var/lib/mongo

-bash-3.2$ mkdir 1

-bash-3.2$ ls -ltr
total 82020
-rw------- 1 mongod mongod 16777216 Aug 11 05:34 local.ns
-rw------- 1 mongod mongod 67108864 Aug 11 05:34 local.0
-rwxr-xr-x 1 mongod mongod        5 Aug 29 03:29 mongod.lock
drwxr-xr-x 2 mongod mongod     4096 Aug 29 03:29 journal
drwxr-xr-x 2 mongod mongod     4096 Aug 29 04:04 1

Node2:

-bash-3.2$ pwd
/var/lib/mongo
-bash-3.2$ mkdir 2
-bash-3.2$ ls -ltr
total 82020
-rw------- 1 mongod mongod 16777216 Aug 11 05:34 local.ns
-rw------- 1 mongod mongod 67108864 Aug 11 05:34 local.0
-rwxr-xr-x 1 mongod mongod        5 Aug 29 03:29 mongod.lock
drwxr-xr-x 2 mongod mongod     4096 Aug 29 03:29 journal
drwxr-xr-x 2 mongod mongod     4096 Aug 29 04:05 2

Node3:

-bash-3.2$ mkdir 3
-bash-3.2$ pwd
/var/lib/mongo
-bash-3.2$ ls -ltr
total 82020
-rw------- 1 mongod mongod 16777216 Aug 11 05:34 local.ns
-rw------- 1 mongod mongod 67108864 Aug 11 05:34 local.0
-rwxr-xr-x 1 mongod mongod        5 Aug 29 03:28 mongod.lock
drwxr-xr-x 2 mongod mongod     4096 Aug 29 03:28 journal
drwxr-xr-x 2 mongod mongod     4096 Aug 29 04:06 3
-bash-3.2$

2)  Create replication set and start mongod process in each server

Node1:

-bash-3.2$ mongod --port 27001 --replSet abc --dbpath /var/lib/mongo/1 --logpath /var/lib/mongo/log.1 --logappend --oplogSize 50 -smallfiles --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4146
child process started successfully, parent exiting

Node2:

-bash-3.2$ mongod --port 27002 --replSet abc --dbpath /var/lib/mongo/2 --logpath /var/lib/mongo/log.2 --logappend --oplogSize 50 -smallfiles --fork
about to fork child process, waiting until server is ready for connections.
forked process: 8573
child process started successfully, parent exiting

Node3:

-bash-3.2$ mongod --port 27003 --replSet abc --dbpath /var/lib/mongo/3 --logpath /var/lib/mongo/log.3 --logappend --oplogSize 50 -smallfiles --fork
about to fork child process, waiting until server is ready for connections.
forked process: 8499
child process started successfully, parent exiting


3) Connect to mongoDB on primary Server for initiat replication set

-bash-3.2$ mongo --port 27001
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27001/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
                               
> rs.initiate()                    -------initiat replication set
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "mongodb1.example.com:27001",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> rs.status()
{
        "set" : "abc",
        "date" : ISODate("2016-08-28T22:44:30Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongodb1.example.com:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 326,
                        "optime" : Timestamp(1472424262, 1),
                        "optimeDate" : ISODate("2016-08-28T22:44:22Z"),
                        "electionTime" : Timestamp(1472424262, 2),
                        "electionDate" : ISODate("2016-08-28T22:44:22Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
abc:PRIMARY>
abc:PRIMARY>

4)  Add secondary servers to replication set.

abc:PRIMARY> rs.add("mongodb2.example.com:27002")
{ "ok" : 1 }
abc:PRIMARY> rs.add("mongodb3.example.com:27003"
... )
{ "ok" : 1 }

abc:PRIMARY> rs.initiate()
{
        "info" : "try querying local.system.replset to see current configuration",
        "ok" : 0,
        "errmsg" : "already initialized"
}
abc:PRIMARY> rs.status()
{
        "set" : "abc",
        "date" : ISODate("2016-08-28T22:47:58Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongodb1.example.com:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 534,
                        "optime" : Timestamp(1472424443, 1),
                        "optimeDate" : ISODate("2016-08-28T22:47:23Z"),
                        "electionTime" : Timestamp(1472424262, 2),
                        "electionDate" : ISODate("2016-08-28T22:44:22Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "mongodb2.example.com:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 44,
                        "optime" : Timestamp(1472424443, 1),
                        "optimeDate" : ISODate("2016-08-28T22:47:23Z"),
                        "lastHeartbeat" : ISODate("2016-08-28T22:47:57Z"),
                        "lastHeartbeatRecv" : ISODate("2016-08-28T22:47:57Z"),
                        "pingMs" : 14,
                        "syncingTo" : "mongodb1.example.com:27001"
                },
                {
                        "_id" : 2,
                        "name" : "mongodb3.example.com:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 35,
                        "optime" : Timestamp(1472424443, 1),
                        "optimeDate" : ISODate("2016-08-28T22:47:23Z"),
                        "lastHeartbeat" : ISODate("2016-08-28T22:47:57Z"),
                        "lastHeartbeatRecv" : ISODate("2016-08-28T22:47:57Z"),
                        "pingMs" : 48,
                        "syncingTo" : "mongodb1.example.com:27001"
                }
        ],
        "ok" : 1
}
abc:PRIMARY>

5)  connect to secondary server

Node2:

-bash-3.2$ mongo --port 27002
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27002/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
abc:SECONDARY>

Node3:

-bash-3.2$ mongo --port 27003
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27003/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
abc:SECONDARY>

use rs.slaveOk() command for apply the replication on secodary servers
example:
abc:SECONDARY>rs.slaveOk()

6) Test replication is working

abc:PRIMARY> show dbs
admin  (empty)
local  0.094GB
abc:PRIMARY> db.products.insert({ "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 })
WriteResult({ "nInserted" : 1 })
abc:PRIMARY> db.products.find()
{ "_id" : ObjectId("57c36b55d9bbb775d4557902"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }

Node2:

abc:SECONDARY> show dbs
admin  (empty)
local  0.094GB
abc:SECONDARY> db.products.find()
{ "_id" : ObjectId("57c36b55d9bbb775d4557902"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }
abc:SECONDARY>

Node3:

abc:SECONDARY> show dbs
admin  (empty)
local  0.094GB
abc:SECONDARY> db.products.find()
{ "_id" : ObjectId("57c36b55d9bbb775d4557902"), "emp_name" : "shiv", "emp_id" : 320, "city" : "nashik", "salary" : 4000 }



 Reference :-http://www.tutorialspoint.com

No comments:

Post a Comment