MongoDB user management


Little recently touched MongoDB, so here we start to learn MongoDB, user management module related content.

Enter Shell for MongoDB

Enter the following command here to enter shell of MongoDB

mongo

Switching databases

use admin

Use the command above to switch the database to the admin database.

Create a super administrator user

Create a user with super administrator privileges

db.createUser(
 { user: "admin",
  customData : {description:"superuser"},
  pwd: "admin",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
 }
)

Each of these fields is explained below

user field: Name of the new user. The password for the pwd field user cusomData field, any content, can be the user name of the national introduction. The roles field specifies the user’s role, which is used for an empty array. In the roles field, you can specify the relevant role. The name of the db database used to manage the database.

Create a superuser

Create a superuser here to manage the permissions of all users

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

Create 1 business database administrator user

This user is specifically responsible for the addition, deletion, and modification of one or more databases.

> db.createUser({
  user:"user001",
  pwd:"123456",
  customData:{
    name:'jim',
    email:'[email protected]',
    age:18,
  },
  roles:[
    {role:"readWrite",db:"db001"},
    {role:"readWrite",db:"db002"},
    'read'//  You have read-only access to other databases, right db001 , db002 Is read and write permission
  ]
})

Among them

Database user roles, read, readWrite. Database administration roles: dbAdmin, dbOwner, userAdmin Cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManage. Backup and restore roles: backup, restore. All database roles: readAnyDatabase. readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase. Superuser role: root Internal role _system

This completes the creation of one of the most basic database administration roles.

View and create users

Use the following commands to view and create users

show users

This command can be used to view and create users.

Change the password

use admin
db.changeUserPassword("username", "xxx")

Using this command, you can change the password.

Change passwords and user information

db.runCommand(
  {
    updateUser:"username",
    pwd:"xxx",
    customData:{title:"xxx"}
  }
)

Delete database user

use admin
db.dropUser('user001')

Create additional database administrators

//  Login administrator user
use admin
db.auth('admin','admin')
//  Switch to the db001 The database
use db001
// ...  � proprietary user to delete the database

Core, enabling permission validation

After setting permissions, you need to enter the following command to enable permission validation.

use admin

0

All changes require a rewrite to boot mongodb

All changes need to be rewritten to boot mongodb before they can take effect

use admin

1

The above is the MongoDB user management details, more information about MongoDB user management information please pay attention to other articles on this site!