Mongo management user related operation summary

Time:2020-1-15

List all users

Copy codeThe code is as follows:
mongo
use admin
db.system.users.find()

Disable authentication mode

Copy codeThe code is as follows:
/etc/mongod.conf
noauth = true
auth = false

Create user

The user is database based, and the user created on the admin database belongs to the administrator user.

Manager user creation

Users with all database management rights

Copy codeThe code is as follows:
 use admin
 db.createUser({
User: “user name”
PWD: “password”
    roles : [
        {
            role : “userAdminAnyDatabase”
            db   : “admin”
        }
    ]
 })

Users with specific database management rights

Copy codeThe code is as follows:
Use “database name”
 db.createUser({
User: “user name”,
PWD: “password”,
    roles : [
        {
            role : “userAdmin”,
DB: database name
        }
    ]
 })

General user

Copy codeThe code is as follows:
DB “database name”
 db.createUser({
User: “user name”,
PWD: “password”,
    roles: [
        {
            role : “read”,  # or “readWrite”
DB: “database name”,
        }
    ]
 })

User verification

Copy codeThe code is as follows:
Db.auth (“user name”, “password”)
Verification of Mongo shell startup
$Mongo “database name” – U “user name” – P “password”

delete user

Copy codeThe code is as follows:
Use “database name”
DB. System. Users. Remove ({user: “user name to delete”})

Example: create a database user with read-write permission for the data

Create a database user with read and write permissions to the database

Copy codeThe code is as follows:
use dbname ;
db.createUser({user: “dbuser”, pwd: “dbuseradmin”, roles:[{role: “readWrite”, db: “dbname”}] })

Database user login

Copy codeThe code is as follows:
mongo dbname -u dbuser -p dbduseradmin