Thambuleena
Thambuleena

Reputation: 253

Mongo authentication for specific db

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

Answers (2)

B--rian
B--rian

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:

  1. Start MongoDB without authentication, connect to Mongo shell and create a user administrator within the admin DB.
  2. Disconnect from the Mongo shell and enable authentication in configuration file. If you can't find mongod.conf you are using an deprecated version of MongoDB and should seriously consider updating before moving on.
  3. Sometimes it is necessary to restart the MongoDB service.
  4. Connect to the MongoDB and authenticate as the user administrator with the username and password you set up in step 1.
  5. Create additional users. You do not always want to use the user administrator for everything.
  6. That's all.

Further resources:

Upvotes: 0

Brett
Brett

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

Related Questions