HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

What's the procedure to set up username/password on mongodb?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thewhatmongodbusernameprocedurepasswordset

Problem

I have tried setting up username / password .But none of the methods seems to work.

1. In mongod.conf ----> remove **#auth=true** to **auth =true**

2.Started the mongod server --> sudo service mongodb restart

3. Created the user in --> use admin -->db.createUSer()
Followed these procedure still not able to figure out how it works its not working.

mongod --auth


But then I try to connect its opening with out any username password error.

I have checked many documentations and answers but was still not able to figure out.

Solution

Create an admin to create users:

> use admin
> ( rs.initiate() )
> db.createUser({user:"siteUserAdmin",pwd:"password",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})


Login as userAdmin:

$ mongo -u siteUserAdmin -p password --authenticationDatabase admin


Create an admin for the cluster:

> use admin
> db.createUser({user:"replSetManager",pwd:"password",roles:[{role:"clusterManager",db:"admin"}]})
> db.auth(replSetManager,password)
> rs.status()  -> ok


Create a user that can access all databases but not admin:

> use admin
> var w = { user:"will", roles:["readWriteAnyDatabase"], pwd:"abc"}
> db.createUser(w)


Create a user only for the test database:

> use test
> var a = { user:"joe", pwd:"asdf", roles:["readWrite"]}
> db.createUser(a)


Many roles can be combined and you can create super admins. Information on all the built-in-roles: http://docs.mongodb.org/manual/reference/built-in-roles/

(You can build your own roles also if needed: http://docs.mongodb.org/manual/tutorial/manage-users-and-roles/#create-user-defined-role)

Code Snippets

> use admin
> ( rs.initiate() )
> db.createUser({user:"siteUserAdmin",pwd:"password",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
$ mongo -u siteUserAdmin -p password --authenticationDatabase admin
> use admin
> db.createUser({user:"replSetManager",pwd:"password",roles:[{role:"clusterManager",db:"admin"}]})
> db.auth(replSetManager,password)
> rs.status()  -> ok
> use admin
> var w = { user:"will", roles:["readWriteAnyDatabase"], pwd:"abc"}
> db.createUser(w)
> use test
> var a = { user:"joe", pwd:"asdf", roles:["readWrite"]}
> db.createUser(a)

Context

StackExchange Database Administrators Q#112386, answer score: 3

Revisions (0)

No revisions yet.