4

I used db.addUser(...) to create a user at some point in the past. How do I now change that user's password?

I logged in with a user with userAdmin role. What command do I use to change another user's password?

Edit 2

I need this answered for the v2.4 style addUser and privilege documents

Edit

It has been suggested that I use the 2.2 addUser syntax to change the password. This does not work:

db.addUser({user: "test", pwd: "oldPassword", roles: ["readWrite"]})
db.addUser("test", "newPassword")

gives

uncaught exception: couldn't add user: system.users entry must not have both 'roles' and 'readOnly' fields
Gabriel
  • 1,679
  • 3
  • 16
  • 37

3 Answers3

8
db.changeUserPassword("test", "newPassword")

https://groups.google.com/d/msg/mongodb-user/KkXbDCsCfOs/rk2_h-oSbAwJ https://jira.mongodb.org/browse/DOCS-1515

Finally found it!

Gabriel
  • 1,679
  • 3
  • 16
  • 37
1

To change a password, just run the addUser command again.

db.addUser("coolguy",  "newxH@x0rPasswd", true);
chrsblck
  • 3,948
  • 2
  • 17
  • 20
  • This is the old style addUser. I am using the new style user docs. http://docs.mongodb.org/manual/reference/privilege-documents/ – Gabriel May 17 '13 at 16:11
  • db.addUser({user: "super", pwd: "123456"}) GIVES 'roles' field must be provided src/mongo/shell/db.js:155 – Gabriel May 17 '13 at 16:14
  • db.addUser({user: "super", pwd: "123456", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]}) GIVES uncaught exception: User already exists with that username/userSource combination – Gabriel May 17 '13 at 16:15
  • db.addUser("super", "123456") GIVES uncaught exception: couldn't add user: system.users entry must not have both 'roles' and 'readOnly' fields – Gabriel May 17 '13 at 16:17
1

this might help.

Becareful about the argument passed. That is for readOnly option.

EDIT : Steps I followed in : Added a new user

> db.addUser("admin","firstpwd") 
{
    "user" : "admin",
    "readOnly" : false,
    "pwd" : "40a84fcba954c8924d277f23b0f880b1",
    "_id" : ObjectId("51966ec8c7ad876ba0319438")
}

exit

> db.auth("admin","firstpwd")
1

Changing the password

> db.addUser("admin","secondpwd")
{
    "_id" : ObjectId("51966ec8c7ad876ba0319438"),
    "user" : "admin",
    "readOnly" : false,
    "pwd" : "82f4e416844349418281a3eca1cf6082"
}

exit

db.auth("admin","secondpwd") 1

MongoDB shell version: 2.4.3

Community
  • 1
  • 1
Srivatsa N
  • 2,291
  • 4
  • 21
  • 36
  • I saw that but the answer is speaking to the asker's confusion about the --auth command line option. I just want to change a user's password. – Gabriel May 17 '13 at 16:13
  • just use the db.addUser() again with the new password – Srivatsa N May 17 '13 at 16:50
  • I tried this but it gives "uncaught exception: User already exists with that username/userSource combination" – Gabriel May 17 '13 at 17:17
  • Here is the output MongoDB shell version: 2.4.3 > db.addUser("coolguy", "hereIsoldpwd") { "_id" : ObjectId("51966b49ef4f47402f6be04c"), "user" : "coolguy", "readOnly" : false, "pwd" : "4f9a89c83b9493f5666762ed0149f258" } I exit from DB , logged in again, > db.auth("coolguy", "hereIsoldpwd") 1 > db.addUser("coolguy", "hereisNewPwd") { "_id" : ObjectId("51966b49ef4f47402f6be04c"), "user" : "coolguy", "readOnly" : false, "pwd" : "4f9a89c83b9493f5666762ed0149f258" } It worked pretty well for me – Srivatsa N May 17 '13 at 17:47
  • I see you are using the old style user privilege docs. I am using the 2.4 style docs with roles. See http://docs.mongodb.org/manual/reference/privilege-documents/ and http://docs.mongodb.org/manual/tutorial/add-user-to-database/ – Gabriel May 17 '13 at 17:55