I have started using MongoDB with authentication. As per the documentation I used the localhost exception to create an admin user with the single role of userAdminAnyDatabase. That all went fine. When I run the "mongo" client and authorize against the admin database after using that database I get the number 1 indicating that authorization succeeded. However, any command I try to execute results in an "unauthorized" error, even trying to show the collections in the admin database I just authorized against.

Can anyone tell me why this is and how I can fix it? Currently I am running without authentication until I can get this fixed.


The userAdminAnyDatabase role (and the userAdmin role that it is an extension of) simply gives your user the permission to read and write to the system.users collection to administer the users of a database. It doesn't allow the user to read or write to the actual collections of the database. For that, you need to grant the read or readWrite roles.

See User Privilege Roles in MongoDB and Add a User to a Database.


Try to run the next steps to solve authentication issue:

  1. start mongod without authentication enabled and run:
  2. create database
  3. create user with role
  4. create a dummy collection with one document
  5. stop/kill mongod
  6. start mongod with authentication enabled
  7. switch to database created in step 1
  8. authenticate on database with user created in step 3

sample commands:

  1. mongod --port 27017 --dbpath /data/db1
  2. use educationDb;
  3. db.createUser({ user : 'readonly', pwd: 'readonly', roles : ['read']})
  4. db.xyz.insert({"name":"nix.education"})
  5. stop/kill mongod
  6. mongod --auth --port 27017 --dbpath /data/db1
  7. use educationDb;
  8. db.auth('readonly','readonly')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.