Wednesday, December 5, 2018

MongoDB Replica set Configuration

cd /u02/mongodb/data

[mongodb@sainath mongodb]$ cd data
[mongodb@sainath data]$ mkdir dbrep_r1
[mongodb@sainath data]$ mkdir dbrep_r2
[mongodb@sainath data]$ mkdir dbrep_r3
[mongodb@sainath data]$ ls -lrtt
total 12
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 28 00:00 dbrep_1
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 28 00:00 dbrep_2
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 28 00:00 dbrep_3
[mongodb@sainath data]$

[mongodb@sainath var]$ mkdir dbrep_r1
[mongodb@sainath var]$ mkdir dbrep_r2
[mongodb@sainath var]$ mkdir dbrep_r3
[mongodb@sainath var]$

/u02/mongodb/var/mongodb/mongod.pid

port : 
37017
37018
37019

vi /u02/mongodb/config/dbrep_r1/dbrep_r1.conf
# mongod.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /u02/mongodb/var/dbrep_r1/mongod.log

# Where and how to store data.
storage:
  dbPath: /u02/mongodb/data/dbrep_r1
  journal:
    enabled: true
  directoryPerDB: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /u02/mongodb/var/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 37017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
replication:
   replSetName: dbrep_r1
  
vi /u02/mongodb/config/dbrep_r2/dbrep_r2.conf
# mongod.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /u02/mongodb/var/dbrep_r2/mongod.log

# Where and how to store data.
storage:
  dbPath: /u02/mongodb/data/dbrep_r2
  journal:
    enabled: true
  directoryPerDB: true
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /u02/mongodb/var/mongodb/mongod.pid  # location of pidfile
# network interfaces
net:
  port: 37018
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
replication:
   replSetName: dbrep_r1
  
  
  
vi /u02/mongodb/config/dbrep_r3/dbrep_r3.conf
# mongod.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /u02/mongodb/var/dbrep_r3/mongod.log

# Where and how to store data.
storage:
  dbPath: /u02/mongodb/data/dbrep_r3
  journal:
    enabled: true
  directoryPerDB: true
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /u02/mongodb/var/mongodb/mongod.pid  # location of pidfile
# network interfaces
net:
  port: 37019
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
replication:
   replSetName: dbrep_r1
   
mongod --config /u02/mongodb/config/dbrep_r1/dbrep_r1.conf   
mongod --config /u02/mongodb/config/dbrep_r2/dbrep_r2.conf   
mongod --config /u02/mongodb/config/dbrep_r3/dbrep_r3.conf   
   



[mongodb@sainath dbrep_r1]$ ps -ef|grep  mongo|grep config
mongodb  12224     1  3 00:41 ?        00:00:14 mongod --config /u02/mongodb/config/dbrep_r1/dbrep_r1.conf
mongodb  13117     1  2 00:47 ?        00:00:02 mongod --config /u02/mongodb/config/dbrep_r2/dbrep_r2.conf
mongodb  13389     1  2 00:48 ?        00:00:00 mongod --config /u02/mongodb/config/dbrep_r3/dbrep_r3.conf
mongodb  13477  4268  0 00:48 pts/1    00:00:00 grep config
[mongodb@sainath dbrep_r1]$

   
mongo --port 37017
mongo --port 37018
mongo --port 37019
   

rs.add( { host: "127.0.0.1:37018", priority: 0 } )
rs.add( { host: "127.0.0.1:37019", priority: 0 } )   


or

rs.add("127.0.0.1:37018");
rs.add("127.0.0.1:37019");

Changing the Priority
======================

cfg=rs.conf()
cfg.members[0].priority=3
cfg.members[1].priority=4
cfg.members[2].priority=3
rs.reconfig(cfg)   
   

Sunday, November 25, 2018


MongoDB Operators

Fields Description

$currentDate à   Sets the value of a field to current date, either as a Date or a Timestamp.
$inc à  Increments the value of the field by the specified amount.
$min à   Only updates the field if the specified value is less than the existing field value.
$max à Only updates the field if the specified value is greater than the existing field value.
$mul à Multiplies the value of the field by the specified amount.
$rename à Renames a field.
$set à Sets the value of a field in a document.
$setOnInsert à Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$unset à Removes the specified field from a document.

Array Operators

$    Acts as a placeholder to update the first element that matches the query condition in an update.
$addToSet à  Adds elements to an array only if they do not already exist in the set.
$pop à Removes the first or last item of an array.
$pull à removes all array elements that match a specified query.
$pushAll à   Deprecated. Adds several items to an array.
$push à Adds an item to an array.
$pullAll à      Removes all matching values from an array.

Modifiers

$each à Modifies the $push and $addToSet operators to append multiple items for array updates.
$position à  Modifies the $push operator to specify the position in the array to add elements.
$slice à Modifies the $push operator to limit the size of updated arrays.
$sort à Modifies the $push operator to reorder documents stored in an array.

Partial Update

$set Parameter
> db.sai.find()
{ "_id" : ObjectId("59f034e02559518339882775"), "a" :
{ "_id" : ObjectId("59f035092559518339882776"), "a" :
{ "_id" : 2, "a" : 42, "b" : "said", "dafa" : 232, "s

a=db.sai
a.insert({_id:1},{"name":"sai"})

$inc
> db.sai.insert({_id:3},{inc_sai:100})
WriteResult({ "nInserted" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3 }
> db.sai.insert({_id:3},{$inc{inc_sai:100})

> db.sai.update({_id:3},{$inc:{inc_sai:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3, "inc_sai" : 101 }
> db.sai.update({_id:3},{$inc:{inc_sai:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3, "inc_sai" : 102 }
> db.sai.update({_id:3},{$inc:{inc_sai:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sai.find({_id:3})
{ "_id" : 3, "inc_sai" : 107 }

$push It will allows the duplicate values         

> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.find({_id:1})
{ "_id" : 1, "new" : 1, "arr" : [ "hi" ] }
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$push: { arr :"hi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> a.find({_id:1})
{ "_id" : 1, "new" : 1, "arr" : [ "hi", "hi", "Ihi", "hi", "hi" ] }

$addToSet It prevents duplicate items from being added to the array

> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> a.update( { _id:1 } , {$addToSet: { arr :"bye"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

a.find({_id:1})
{ "_id" : 1, "new" : 1, "arr" : [ "hi", "hi", "hi", "hi", "hi", "bye" ] }



Saturday, November 17, 2018

MongoDB Replcation Setup

MongoDB Replica-Set Configuration
=========================================
we are configuring the Mongo replicaset on Linux server only for practice purpose

Primary+Seconday+Secondary

[mongodb@sainath data]$ pwd
/u02/mongo/data

Creating Directory's for the data path

[mongodb@sainath data]$ mkdir sai_r1
[mongodb@sainath data]$ mkdir sai_r2
[mongodb@sainath data]$ mkdir sai_r3

[mongodb@sainath data]$ ls -ltr

drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r1
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r2
drwxrwxr-x. 2 mongodb mongodb 4096 Nov 16 22:20 sai_r3



ReplicaSet Name mentioned here is R

Port Numbers We have used are 27027 , 27028 , 27029

Startng Mongod service with the  below commands in different sessions
========================================================================
mongod --port 27027 --dbpath /u02/mongo/data/sai_r1 --replSet R
mongod --port 27028 --dbpath /u02/mongo/data/sai_r2 --replSet R
mongod --port 27029 --dbpath /u02/mongo/data/sai_r3 --replSet R

Connecting the mongo session

[mongodb@sainath data]$ mongo --port 27027
 MongoDB shell version v3.6.0-rc0
connecting to: mongodb://127.0.0.1:27027/
MongoDB server version: 3.6.0-rc0
Server has startup warnings:
2018-11-16T22:24:28.495+0530 I CONTROL  [initandlisten]

Initiate the replSet by using command rs.initiate()

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "sainath.com:27027",
        "ok" : 0,
        "errmsg" : "No host described in new configuration 1 for replica set R maps to this node",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig"
}


Configurinng adding all the mongod servers to cluster replSet R
===============================================================
> config = { "_id": "R",
... "members" : [
... { "_id" : 0, "host" : "localhost:27027" },
... { "_id" : 1, "host" : "localhost:27028" },
... { "_id" : 2, "host" : "localhost:27029" }
... ]}
{
        "_id" : "R",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029"
                }
        ]
}
> rs.initiate(config)
{
        "ok" : 1,
        "operationTime" : Timestamp(1542387435, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387435, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY>   rs.status()
{
        "set" : "R",
        "date" : ISODate("2018-11-16T16:57:37.161Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1542387448, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27027",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 190,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1542387446, 1),
                        "electionDate" : ISODate("2018-11-16T16:57:26Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27028",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T16:57:28Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T16:57:36.115Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T16:57:36.526Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27029",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542387448, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-11-16T16:57:28Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T16:57:28Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T16:57:36.116Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T16:57:36.528Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1542387448, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387448, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY> rs.config()
{
        "_id" : "R",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5beef6eb7e1ec6c6b47764a4")
        }
}



Setting Priorities
======================
cfg=rs.conf()
cfg.members[0].priority=3
cfg.members[1].priority=4
cfg.members[2].priority=3
rs.reconfig(cfg)


R:PRIMARY> cfg=rs.conf()
{
        "_id" : "R",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27027",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27028",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27029",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5beef6eb7e1ec6c6b47764a4")
        }
}
R:PRIMARY> cfg.members[0].priority=3
3
R:PRIMARY> cfg.members[1].priority=4
4
R:PRIMARY> cfg.members[2].priority=3
3
R:PRIMARY> rs.reconfig(cfg)
{
        "ok" : 1,
        "operationTime" : Timestamp(1542387990, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542387990, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
R:PRIMARY>
> rs.status()
{
        "set" : "R",
        "date" : ISODate("2018-11-16T17:11:06.924Z"),
        "myState" : 1,
        "term" : NumberLong(3),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1542388261, 1),
                        "t" : NumberLong(3)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27027",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 999,
                        "optime" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:11:01Z"),
                        "electionTime" : Timestamp(1542388260, 1),
                        "electionDate" : ISODate("2018-11-16T17:11:00Z"),
                        "configVersion" : 4,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27028",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 831,
                        "optime" : {
                                "ts" : Timestamp(1542388249, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542388249, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:10:49Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T17:10:49Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T17:11:06.162Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T17:11:06.347Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 4
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27029",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 831,
                        "optime" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1542388261, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2018-11-16T17:11:01Z"),
                        "optimeDurableDate" : ISODate("2018-11-16T17:11:01Z"),
                        "lastHeartbeat" : ISODate("2018-11-16T17:11:06.162Z"),
                        "lastHeartbeatRecv" : ISODate("2018-11-16T17:11:05.381Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:27027",
                        "configVersion" : 4
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1542388261, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1542388261, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}