Pages

January 28, 2020

#MongoDB Part 6: Interview Questions And Answers

How to create User and add Role in MongoDB?
Creating Admin User
We can create a user administrator in MongoDB by using the createUser method. e.g:

db.createUser(
{
   user: "myadmin",
   pwd: "password@123",
   roles:[{role: "userAdminAnyDatabase", db:"admin"}]
});


The above query will create a user with name 'myadmin'. Since it needs to be a database administrator in which case we have assigned to the 'userAdminAnyDatabase' role. This role allows the user to have administrative privileges to all databases in MongoDB. The db parameter specifies the admin database which is a special Meta database within MongoDB which holds the information for this user.

Create User for single database
To create a user, who will manage a single database, we can use the same createUser command but we need to use the 'userAdmin' option only.

db.createUser(
{
   user: "hruser",
   pwd: "password@123",
   roles:[{role: "userAdmin", db:"Employee"}]
});

Above query will create 'hruser', in this case since it needs to be a database administrator is assigned to the 'userAdmin' role. This role allows the user to have administrative privileges only to the database specified in the db option. The db parameter specifies the database to which the user should have administrative privileges on.

There is a whole list of role available in MongoDB. e.g, there is a the 'read' role which only allows read only access to databases and then there is the 'readwrite' role which provides read and write access to the database.

How replication works in MongoDB?
Replication means synchronizing the data across multiple servers. It provides redundancy and increase data availability with multiple copies of data on different database server. Replication helps in protecting the database from the loss of a single server.
  • Primary replica set:  MongoDB writes data only to primary or master replica set.
  • Secondary replica set: Secondary or slave nodes can accept only reads. They replicate from the primary.
What is a replica set?
In MongoDB, multiple MongDB Servers are grouped in sets called Replica sets. The Replica set will have a primary server which will accept all the write operation from clients. All other instances added to the set after this will be called the secondary instances which can be used primarily for all read operations.

How does concurrency affect primary replica set?
When the collection changes are written to primary, MongoDB writes the same to a special collection in the local database, called the primary’s oplog. Hence, both the collection’s database and local database are locked.

What is oplog in MongoDB?
oplog stands for operations log. It is a special capped collection which stores an ordered history of all logical operations that modify the data stored in your database.

Adding the first member using rs.initiate()
Let's assume we have 3 servers called A, B, and C, where A is our Primary server and B, C are our secondary servers. These steps which need to be followed for creating the replica set along with the addition of the first member to the set.

1). Ensure that all mongod.exe instances which will be added to the replica set are installed on different servers. This is to ensure that even if one server goes down, the others will be available and hence other instances of MongoDB will be available.
2). Ensure that all mongo.exe instances can connect to each other. From A, issue the below commands:

mongo –host B –port 27017
mongo –host C –port 27017

3). Start the first mongod.exe instance with the replSet option. This option provides a grouping for all servers which will be part of this replica set.

mongo –replSet "Replica1"

where "Replica1" is the name of your replica set.
4). Now that the first server is added to the replica set, the next step is to initiate the replica set by issuing the following command rs.initiate()
5). Verify the replica set by issuing the command rs.conf() to ensure the replica set up properly.

Adding a secondary server using rs.add()
The secondary servers can be added to the replica set by just using the rs.add command. This command takes in the name of the secondary servers and adds the servers to the replication set.

Suppose we have A, B, and C, which are required to be part of your replica set and A, is defined as the primary server in the replica set. To add B and C to the replica set issue the commands:

rs.add("B");
rs.add("C");

We can use the rs.remove command to remove the required server from the replica set. So suppose if you have a replica set with A, B, and C, and you want to remove B from the replica set, issue the command

rs.remove("B");

-K Himaanshu Shuklaa..

No comments:

Post a Comment