Skip to main content

mongodb authentication setting


The administrator of the database is the right person for the task of granting, or denying, permissions to users for doing operations with database resources. By using roles, we can specify what action can be done with a resource. Therefore, a role is a privilege granted to a user to do specific tasks with specific resources. Before you enable access control, you should create a user that can then create users and assign roles to them once access control is enabled.This user-admin will then be used to create and maintain other users and roles, so needs to be assigned a suitable role to enable it to do so.


I assume you already have admin user set up within you system if not refer hare before proceeding.

The role userAdminAnyDatabase in MongoDB gives ability to create users and assign roles to them, but by itself it doesn’t allow the user to do anything else. The superuser role in MongoDB is the root.

In the this post i will show how to create, show and delete a user in MongoDB. I will also show how to create a user with root (superuser) privileges on the all databases in MongoDB.


 Shell into mongodb with the admin user


mongo -u admin -p --authenticationDatabase admin --host 192.168.0.250

MongoDB shell version v4.0.10

Enter password:



> use admin
switched to db admin
> show users -- Show users in the current database:
{
        "_id" : "admin.admin",
        "userId" : UUID("6e316f61-9cf3-42f5-9998-dcd5da0559f4"),
        "user" : "admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}


Create user called farmtest, with read access to database farm, read and write access to farm-dev


> use farm-dev -- Switch to the database in which you would like to create a common user      
switched to db farm-dev
> db.createUser(
... {
... user: "farmtest",
... pwd: "pass123",
... roles: [
... { role: "read", db: "farm" },  --- different permissions on different databases.
... { role: "readWrite", db: "farm-dev" }
... ]
... }
... );
Successfully added user: {
        "user" : "farmtest",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "farm"
                },
                {
                        "role" : "readWrite",
                        "db" : "farm-dev"
                }
        ]
}


connect to remote MongoDB server from the command line using mongo shell with the farmtest user.

 mongo -u farmtest -p --authenticationDatabase farm-dev --host 192.168.0.250
MongoDB shell version v4.0.10
Enter password:
connecting to: mongodb://192.168.0.250:27017/?authSource=farm-dev&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("564c14a4-52ac-407c-98a3-670a9fc95697") }
MongoDB server version: 4.0.10
>

Note: if you want to use MongoDB client, we must use a connection string like this:

module.exports = {mongoURI: 'mongodb://farmtest:pass123@192.168.0.250/farm-dev'}

Create mongo-root user: 

 use admin
switched to db admin
> db.createUser(
... {
... user: "mgroot",
... pwd: "Passw0rd",
... roles: [ { role: "root", db: "admin" } ]
... }
... )
Successfully added user: {
        "user" : "mgroot",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}



 Delete a user from the current database:


> db.dropUser("user1")

Popular posts from this blog

How to Install MongoDB on Debian 9 and enable remort access and authentication

MongoDB is a free and open-source NoSQL document database with the scalability and flexibility with the querying and indexing. This post will assist you with the installation process of MongoDB, manage its service, enable remote access and securing MongoDB. Note: Platform Support MongoDB only provides packages for 64-bit builds of Debian 8 and 9. See Supported Platforms for more information.The mongodb-org package is officially maintained and supported by MongoDB Inc. and kept up-to-date with the most recent MongoDB releases. This installation procedure uses the mongodb-org package a.k.a the official MongoDB repo. Step 1 — Installing MongoD make sure the curl command is installed so that we can use it to add the MongoDB signing key if you don't have curl installed, install it by running the following command:- apt install curl Import the public key used by the package management system. curl https://www.mongodb.org/static/pgp/server-4.0.asc | apt-key add - ...

django react app setting up the backend

On the previous article I demonstrated how we can use the generic views along with ModelSerializer classes to rapidly develop our REST APIs. Knowledge that you will need  in your career as full stack / backend developer, however think of this article as an extension to the previous one, equipped with what we already know about REST API we will step our game up and discuss about ViewSet, ModelViewset we will dig deep into the concepts of Routers which allow us to manage our api routes in a simple and sophisticated manner as well as helping to speed up building APIs even further. There for on part II of this article i'll work you through on how React application can consume this RESTful API. There for at the end of the day we will have a full stack web app, in short we strat our development at the backend then later on we move at the frontend... so are you excited and ready to take the challange? lets do this then..... you can get source code for the bakend on github Preparat...

How To Install the Anaconda Python Distribution on Debian 9 And Running a public notebook server

Anaconda Distribution is an open-source package manager, environment manager and distribution of Python and R programming languages. With a collection of 1,000+ open source packages with free community support. Designed for data science and machine learning workflows, you can use it whether you are on Windows, macOS or Linux. The Anaconda distribution ships with the conda command-line package management utility. You can learn more about Anaconda and conda by reading the official Anaconda Documentation . Jupyter is a browser-based interpreter that allows you to interactively work with Python and R. Anaconda provides Jupyter as well. You can think of Jupyter as a digital notebook that gives you an ability to execute commands, take notes and draw charts.It’s primarily used by Data Scientists. But I find that very useful tool if you are learning Python or R. It’s basically the same as working on a shell but much better. The Jupyter notebook web application is based on a...