Mongodb User


Show user list

Access mongo using mongo shell and switch database

use mydb

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


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

Start mongo shell with user

mongo dbname -u user -p password

if password is wrong, cannot access correctly