Configure Authentication, Users and Roles in MongoDB

Configure Authentication, Users and Roles in MongoDB

Instructor-svgAl-Mamun Sarkar
Sep 14 , 2020

In this lesson, I show you how you can create authentication, users, roles in the MongoDB database.

 

Go to the database(here admin) and add a user:

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

 

Shutdown MongoDB and start new mongod with --auth:

db.adminCommand( { shutdown: 1 } )
mongod --auth --port 27017 --dbpath /var/lib/mongodb

 

Change Configurartion file if you start mongod by config:

security:
    authorization: enabled

 

Run mongo shell with database and authentication:

mongo --port 27017  --authenticationDatabase "admin" -u "myUserAdmin" -p

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram