You can add a user to a specific db by first changing to that db use dbname
and then adding a user for that specific db db.addUser('username','password')
. Once you have done this you can connect to that db using mongo dbname -u username -p
, or by connecting to mongo and then changing to that db and then doing db.auth('username','password')
.
The admin database is a special case as priveleges granted to users in this db are granted for any db. An example from the MongoDB docs is:
The userAdmin is a database specific privilege, and only grants a user the ability to administer users on a single database. However, for the admin database, userAdmin allows a user the ability to gain userAdminAnyDatabase, and so for the admin database only these roles are effectively the same.