Thursday, 3 August 2023

MongoDB User Creation Details

Now today give you a small demo for creating user, gives permission and getting user info.

# Create user
Syntax:
db.createuser(
{user : “mylogin” , pwd : “mylogin”,
roles : []
});

db.createuser(
{user : “mylogin” , pwd : “mylogin”,
roles : [{role : “userAdminAnyDatabase”, db : “admin”}]});     <—– Full admin level Permission on admin user

db.createuser(
{user : “mylogin” , pwd : “mylogin”,
roles : [
{ role : “read”, db : “admin”},                   <—– Read Permission on Local admin DB
{role : “readWrite” , db: “local”}               <—– Read/Write Permission on Local DB
] }
);

# Change User Password
use mylogin
db.changeUserPassword(“accountUser”, “test#123”)

# Roles
db.grantRolesToUser({ “<username>”,”roles” : [ { “role” : “assetsReader”, “db” : “assets” }]});  <—- Grant role to User

db.revokeRoleFromUser({});                    <———– Revoke role from User

Few RolesuserAadminAnydatabase,read, readWrite

 

# Userinfo getting
use admin
db.system.users.find();

db.getUsers();             <———- It’s also showing the details of all databases user

db.runCommand( { usersInfo: 1 } );                 <———— view all users of databases

db.runCommand(
{
usersInfo: { user: “Aman”, db: “home” },
showPrivileges: true,
showCredentials : true })

db.runCommand( { usersInfo: [ { user: “Aman”, db: “home” }, { user: “Tom”, db: “myApp” } ],
showPrivileges: true
} )

 

For More Detail about

User Creation: Click Here

User Roles: Click Here

User Privileges: Click Here

Manage User and Roles: Click Here

No comments: