Reputation: 253
Can you please help me how to access a DB using mongo with authentication?
use admin
db.addUser( { user: "root", pwd: "root", roles: ["readWrite"] } )
db.auth('root', 'root')
I am using Ubuntu. Is anything I did wrong? How to access my DB with authentication?
Upvotes: 3
Views: 3323
Reputation: 5890
I am assuming you are actually asking two questions: (1) How to setup MongoDB authentication and (2) how to use it.
Although it is a couple of years later, I cannot help to link the official tutorial. Here the management summary:
admin
DB.mongod.conf
you are using an deprecated version of MongoDB and should seriously consider updating before moving on.Further resources:
Upvotes: 0
Reputation: 3885
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.
Upvotes: 1