Showing posts with label Replication. Show all posts
Showing posts with label Replication. Show all posts

Tuesday, June 30, 2015

Replication in MongoDB

High throughput
Increase availability
Maintain copies of data like one in DR , one for reporting
Replica set members: primary , secondary






Machine generated alternative text:
Computer LocalDisk(C:)fr data
Organize — Include in library — Share with Burn New folder
. Name Date modified Type Size
Favorites
. db 6/29/2015 9:13 AM File folder
Libraries . rsl 6/30/2015 4:33 PM File folder
>  Documents . rs2 6/30/2015 4:33 PM File folder
> J Music . rs3 6/30/2015 4:33 PM File folder
‘: Computer



C:\Program Files\MongoDB 2.6 Standard\bin>mongod --dbpath C:\data\rs1 --port 27017 --replset rs101 --smallfiles -
oplogsize 16


C:\Program Files\MongoDB 2.6 Standard\bin>mongod --dbpath C:\data\rs2 --port 27018 --replSet rs101 --smallfiles -
oplogsize 16


C:\>mongo --port 27017
> var myconf ={_id:"rs101", members:[{_id:0,host:"localhost:27017"}]}
> myconf
{
        "_id" : "rs101",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27017"
                }
        ]
}
>


> rs.initiate(myconf)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
rs101:OTHER>
rs101:PRIMARY>

rs101:PRIMARY> rs.status()
{
        "set" : "rs101",
        "date" : ISODate("2015-06-30T11:19:19Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 598,
                        "optime" : Timestamp(1435663092, 1),
                        "optimeDate" : ISODate("2015-06-30T11:18:12Z"),
                        "electionTime" : Timestamp(1435663092, 2),
                        "electionDate" : ISODate("2015-06-30T11:18:12Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}

rs101:PRIMARY> rs.add("localhost:27018")
{ "ok" : 1 }
rs101:PRIMARY>


rs101:PRIMARY> rs.status()
{
        "set" : "rs101",
        "date" : ISODate("2015-06-30T11:22:00Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 759,
                        "optime" : Timestamp(1435663208, 1),
                        "optimeDate" : ISODate("2015-06-30T11:20:08Z"),
                        "electionTime" : Timestamp(1435663092, 2),
                        "electionDate" : ISODate("2015-06-30T11:18:12Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 112,
                        "optime" : Timestamp(1435663208, 1),
                        "optimeDate" : ISODate("2015-06-30T11:20:08Z"),
                        "lastHeartbeat" : ISODate("2015-06-30T11:22:00Z"),
                        "lastHeartbeatRecv" : ISODate("2015-06-30T11:21:59Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:27017"
                }
        ],
        "ok" : 1
}
rs101:PRIMARY>





C:\>mongo --port 27018
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27018/test
Server has startup warnings:
2015-06-30T16:42:50.272+0530 [initandlisten]
2015-06-30T16:42:50.272+0530 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2015-06-30T16:42:50.272+0530 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less
with --journal).
2015-06-30T16:42:50.272+0530 [initandlisten] **       Note that journaling defaults to off for 32 bit and is curr
ntly off.
2015-06-30T16:42:50.272+0530 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
2015-06-30T16:42:50.288+0530 [initandlisten]
rs101:SECONDARY>
rs101:PRIMARY> show dbs
admin  (empty)
local  0.063GB
rs101:PRIMARY> use mydb
switched to db mydb
rs101:PRIMARY> db.createCollection("rstable")
{ "ok" : 1 }
rs101:PRIMARY>

rs101:SECONDARY> show dbs
admin  (empty)
local  0.063GB
mydb   0.031GB
rs101:SECONDARY> show collections
2015-06-30T16:57:15.761+0530 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shel
/query.js:131
rs101:SECONDARY>




Mongo doesn’t allow read /write from secondary by default