Wednesday, 31 August 2016

How to create superuser in MongoDB?

I am using mongoDB version 3.2

mongoDB has defined basic in built roles such as -
1.read
2.readWrite
3.userAdmin
4.dbOwner
5.dbAdmin
6.clusterAdmin
7.readWriteAnyDatabse
8.readAnyDatabase
8.userAdminAnyDatabase 
9.restore 
10.root 
etc.

For "superuser" I mean user must have right to read,write.admin option to any database in mongod instance server.

No 10 "role option "root" mentioned above is superuser.

root = readWriteAnyDatabase, + dbAdminAnyDatabase + userAdminAnyDatabase+clusterAdmin  + restore

Create user as below ->

db.createUser({user:"superAdmin",pwd:"super123",roles:["root"]});

It has db scope limited to admin db.
So connect to admin db and then authenticate first.
Then you can access any db you want.

How to use Authentication in MongoDB

I am using mongoDB version 3.2
By default authentication in mongoDB is disabled.

In order to enable authentication we need below prerequisites.
 Prerequisites:
1. Create role (or use In built roles)
2. Create user

Here ,I am going to create read access to admin db using in built role "read"

How to create User?
1. run mongod instance
  mongod
2.connect it using mongo shell
   mongo
3.connect to admin db
use admin
4.Now create user in this database.
db.createUser({user:"super",pwd:"super123",roles:["root"]});

 user- username
 pwd -password
 roles- read
db -admin (The scope of this access is limited to admin db by default because we are using "admin" db)
Now user has been created successfully.

How to authenticate?
5. close mongod instance and mongo shell and reconnect to enable authentication.
mongod --auth
6.connect to admin db
use admin 
7. now if you try to access this db you will be shown "Not Authenticated" message.
So, you need to get authenticated using user name and password.
    db.auth("super","super123")

Now you can access admin db for "read" operations.

How to create oplog for MongoDB without using replica set

I am using MongoDb version 3.2

Normally replica set is used to generate oplog in MongoDB.

I have a requirement wherein I need every mongoDB instance running on different servers as Master or Primary.

Therefore I got the below solution ->
In version 3.2 each mongoDB instance is connected as Master(Primary) if we do not use replica set. But  this will not generate oplog in local database and hence no Oplog Tail is available.

We need at least 1 master user for oplog generation.(In Replica set 1 user as Master is mandatory.)

1. Run mongod instance as master.
 mongod --master
2.Now run mongo node and connect to local database.
use local;

You will find oplog.$main collection as oplog connection.



Tuesday, 30 August 2016

What is Java Memory Footprint and How to calculate it using JDK tools?

Java Memory Footprint -
It refers to the amount to memory(RAM) a Java Program uses or references while running.
           In Java, Memory Footprint is mainly runtime environment in the form of JVM ,which is loaded indirectly when a Java application is launched.


Now to calculate it?
JDK provides 2 tools to calculate it.
1. jconsole.exe
2. jvisualvm.exe

We can also use Task Manager to find approx memory used in Memory column.

 Prerequisite -  Run your java application.(Eg.Simple Java Program with main())

 How to use jconsole?
1. Run jconsole.exe and you will get jconsole window popup.
2. Click on connect button
3. Then choose your Java Application.

We can have view of -
1. How much memory is consumed? - Heap ,Non-Heap
2. How many threads are running ?
3. How many classes are loaded?
and other memory related info.


** Note-
By default how many JVM core thread runs when we run a Java Program?
1. Attach Listener - It starts when first attach request occurs to JVM.
2. Signal Dispatcher -It starts when OS pass signal to JVM for appropriate Handler.
3. Finalizer - This Thread calls Finalize() method.
4.Reference Handler - This is high priority thread for pending references.
5.Main - This thread  calls main() method.

Friday, 26 August 2016

How to create mongoDB oplog for single Mongo Shell node/instance using replica set

I am using MongoDB version 3.2.

MongoDB oplog  is mainly introduced for replication set or cluster of nodes.
**Oplog is created in local database in mongoDB server whenever we initialise a replica set.

Since replication is online process ( internet is needed ) I want to maintain central backup server offline for my project.So I do not need  replica set.(so I do not want to use it).
Hence I am exploring an option to create oplog without initializing  replica set.

But I could not generate oplog just by opening a mongoDB shell (node).
Oplog is not found under local db.


My solution  is -->
1. run mongod as
mongod --replSet rs0

2.open mongoDB shell(node) and initialize it.
 rs.initialise();

That's it !!! Do not add any other members to replica set.
(It is equivalent to single mongoDB node)

3.Now add record to your created db for project
and check oplog.


 Please comment if anyone has an alternative solution to this.
May we can open multiple mongoDB nodes/instances on same server and make every node as primary?
But How to make each node primary without being part of replica set?








Thursday, 25 August 2016

How to delete replica set in MongoDB

I am using mongoDB version 3.2.
I created a replica set rs0.

I  do not know how to get rid of this.(I mean delete).


In order to use mongod as stand alone server instead replica set carry out following steps.
  1. Restart mongod process without --replSet  argument.
  2. Drop the local database
    use local;
    db.dropDatabase();
     
    It will help. 
    

How to create Replica Set in MongoDB

I am using mongoDB version 3.2.
I am assuming you have installed mongoDB on your machine.

Steps:
1. Open command prompt and navigate to "/bin" folder of mongoDB where mongod.exe files are stored.
mongod --replSet  abc  (Here abc is replica Set name) 
 Now you have created replica Set.

2. Now open mongo shell (node) in another window.
Again go  to "/bin" and run mongo.exe  

 3. Now you have already connected to test db.
   Initiate the replica set.
     rs.initiate();

 Now you can see replica set initialized on your shell.
Eg.<abc:PRIMARY>

or <abc:SECONDARY>
 etc.

Accessing Oplog in MongoDB

Oplog  stands for  Operation Log.
It is not a log file but a capped collection (collection with fixed size) stored in MongoDb.
Precisely it is stored in local database on MongoDb server.

What is the use?
It is introduced to support replication (duplication of data) in MongoDb.
It mainly monitors CRUD operations in MongoDb and writes to local db in oplog.rs collection


I am using mongoDB Version 3.2

There are 2 ways to access oplog in mongoDB -
1.With replica Set (using cluster of hosts)
2.Without replica Set (using each mongoDB instance as Master)

1st way =>
Prerequisite -> connect to replica set
mongod --replSet rs0

How to query it in mongo shell ?
1. Use local database.
    use local;
2. documents in oplog.rs  will give you latest oplog data.
   db.oplog.rs.find();


 2nd Way =>
Prerequisite -> connect as Master user
(Though each connected node in version 3.2 is master by default but oplog does not generate.Hence this step is mandatory.)
mongod --master
1. Use local database
use local;
2.docs in oplog.$main will give you latest oplog data.
db.oplog.$main.find();


You will get list of all latest oplog records here.