Mongodb User

Contents

Show user list

Access mongo using mongo shell and switch database

use mydb
db.system.users.find()

db is database object

Add admin user

Switch admin db and use following

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

username is “superadmin”, password is “password”

Add general user

Access specific database and create general user

use mydb
db.createUser({user: "guser", pwd:"password", roles:[{role:"readWrite", db:'mydb'}]})

guser is user name

Check

db.auth("user", "password")

Start mongo shell with user

mongo dbname -u user -p password

if password is wrong, cannot access correctly