Saturday, April 29, 2017

Roles-Privs-create user

Built-in Roles:

read
readWrite
dbAdmin
userAdin
clusterAdmin

readAnyDatabase
readWriteAnyDatabase
dbAdminAnyDatabase
userAdminAnyDatabase

User prompt changing:

prompt = function() {
    user = db.runCommand({connectionStatus:1}).authInfo.authenticatedUsers[0]
    host = db.getMongo().toString().split(" ")[2]
    curDB = db.getName()
    if (user) {
       uname = user.user
    }
    else {
       uname = "local"
    }
    return uname + "@" + host + ":" + curDB + "> "
}


Creating roles:

db.createRole({ role: "appReadRole", privileges: [ { resource: { db: "test", collection: "" }, actions:

[ "find" ] } ], roles: [] })


Listing all the roles:

db.getRoles(
    {
      rolesInfo: 1,
      showPrivileges:false,
      showBuiltinRoles: false
    }
)

Creating user with roles:

var a={user:"mani", pwd:"mani", roles:[{role:"read",db:"test"}]}
db
db.createUser(a)


db.createUser({ user: "finance", pwd: "password", roles: [ { role: "appReadRole", db: "test" } ] })

mongo Mani-PC:27002/test -u finance -p password

Changing password:

db.changeUserPassword("finance", "welcome123")

Current user details:

db.runCommand({connectionStatus : 1})

use admin
db.system.users.find().pretty()
db.system.users.remove({user:"userA"})


db.grantRolesToUser(
  "report",
  [{ "role" : "readWriteAnyDatabase", "db" : "admin" }]
)


db.revokeRolesFromUser(
  "report",
  [{ "role" : "readWriteAnyDatabase", "db" : "admin" }]
)

db.getUser("report")


use products
db.grantPrivilegesToRole(
  "inventoryCntrl01",
  [
    {
      resource: { db: "products", collection: "" },
      actions: [ "insert" ]
    },
    {
      resource: { db: "products", collection: "system.js" },
      actions: [ "find" ]
    }
  ],
  { w: "majority" }
)


===============================================================
Edit .mongorc.js in your home directory for changing the prompt:

function prompt() {
    var username = "anon";
    var user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0];
    var host = db.getMongo().toString().split(" ")[2];
    var current_db = db.getName();

    if (!!user) {
        username = user.user;
    }

    return username + "@" + host + ":" + current_db + "> ";
}
==============================================================

Wednesday, April 26, 2017

Mongo Prompt with DB name and current user

Past the below code in the mongo shell


prompt = function() {
    user = db.runCommand({connectionStatus:1}).authInfo.authenticatedUsers[0]
    host = db.getMongo().toString().split(" ")[2]
    curDB = db.getName()
    if (user) {
       uname = user.user
    }
    else {
       uname = "local"
    }
    return uname + "@" + host + ":" + curDB + "> "
}

Tuesday, April 25, 2017

Roles and authentication

Common Roles:
============
read
readWrite
dbAdmin
userAdin
clusterAdmin

readAnyDatabase
readWriteAnyDatabase
dbAdminAnyDatabase
userAdminAnyDatabase



var a={user:"abcd", pwd:"efgh", roles:["readWrite]}

db.createUser(a)

mongo hostname/dbname -u abcd -p efgh



Monday, February 13, 2017

Wconcern - Replication - Best practice

Best practice

Write concern -> wmajority setting is important
Wtimeout value should be appropriate to ensure taking Write ackg within timeout interval
Connection pool max size should be appropriate

Wednesday, February 1, 2017

Replication failover




FAILOVER
=========

ps  -Aef|grep mongod
Ps -Aef|grep mongod|grep <pid>    ---- belongs to primary instance port id
kill  -9   <pid>
rs.slaveOk() for making the secondary replica set to allow user queries...this has to be executed on secondary dbs only

After all the failover testings are done, bringing back the failed node to online:
mongod --replSet abc  --dbpath 1 --port 27001 --oplogSize 50 --logpath log.1 --logappend --fork

Read Preference :
rs.slaveOk() for making the secondary replica set to allow user queries...this has to be executed on secondary dbs only
primary  - Default
primary preferred - Try primary if not reachable then read from secondary
secondary - Read from scondary, offload read from primary
secondary preferred - Scondary first then primary
nearest - Nearest member first







Wednesday, January 25, 2017

The profiler

The profiler============

db.commandHelp("profile")

Levels
0= OFF
1=selective (slow)
2=ON

Setting profile:

Setting profile for Level 2 =>db.setProfilingLevel(2)

Setting profile for logging transaction >3Msecs   => db.setProfilingLevel(1,3)


Checking profile:
db.getProfilingStatus()

show collections  => "system.profile" will be in the collection list

List entries in the profile log=>db.system.profile.find().pretty()

Count number of entries in the profile => db.system.profile.find().count()

display last entry from profile => db.system.profile.find({},sort({$natural:-1}.limit(1).pretty()

display last entry from profile with type of operation query or update or??
=> db.system.profile.find({},{op:1}.sort({$natural:-1}.limit(10).pretty()

CHECKING Profile size=>
db.system.namesapces.find()  - default 1MB in RAM, it's a circular queue

db.system.profile.stats()

Optimizer plan EXPLAIN

Explain:



***********query planner******************

db.example.explain().find({a:17}).sort({b:-1})

- Stage tells "collscan" or "Idxscan"

- Winning plan tells which plan is choosen by this execution

db.example.explain().remove({a:17,b:12})

- Taking explain plan for removing indexes


*************executionStats*****************

Examing explain plan

exp=db.example.explain("executionStats")
Explainable(test.example)

exp.find({a:17,b:12})

This throw output with query planner section and exectuion stats which gives details how
itration time, number search, records returned, time to execute etc


*************allplansexecution*****************

expall=db.example.explain("allPlansExecution")

exp.find({a:17,b:12})



Examples:

db.sensor_readings.createIndex({active:1,tstamp:1})

db.products.createIndex({for:1})

db.products.find({for:"ac3"})

db.products.explain().find({for:"ac3"})

exp=db.products.explain("executionStats")

exp.find({for:"ac3"})