22

I have production environment where my mongoDB is up and running and DBAs are asking us to change the password which we use for authentication. One way to do this is run the addUser command again with a new password as described in change password

> db.auth("app_user", "somepassword")
db.addUser("app_user", "new password")

This is as good as a adding a new user.

I understand that I have to restart mongod with the --auth option once I add a new user as described in but as this is a production env and I can't restart my server. Is there any other option ? or if my approach is wrong how to change the password in mongoDB

Community
  • 1
  • 1
Srivatsa N
  • 2,291
  • 4
  • 21
  • 36
  • You only would need to restart if you were going from no auth to auth - you have no need to restart. – Asya Kamsky Apr 30 '13 at 15:13
  • Apparently you also need to restart when adding/updating users to a non auth mongod (yes its still possible to authenticate users on specific database) – Benoit Nov 25 '16 at 15:13

5 Answers5

25

MongoDB > 3.X

db.updateUser("root", {pwd: "NewRootAdmin" }) 

Reference: https://docs.mongodb.com/manual/reference/method/db.updateUser/

db80
  • 4,157
  • 1
  • 38
  • 38
24

For v2.4

db.changeUserPassword("app_user", "new password")

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

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
Gabriel
  • 1,679
  • 3
  • 16
  • 37
1

You linked to a question asking about adding authentication to MongoDB which involves starting 'mongod' with option --auth. Since you are already running with --auth the restart is not necessary in your scenario.

Just change the user password and you'll be set to go.

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
  • 1
    Thanks Asya, but is it the only way to change the password ? "adduser" is somewhat misleading. – Srivatsa N May 02 '13 at 03:31
  • 1
    I agree it's not the best command name but addUser will create user if they don't exist or change their password if they do exist. – Asya Kamsky May 02 '13 at 16:02
  • As of 2.4 this doesn't seem to work anymore `JavaScript execution failed: User already exists with that username/userSource combination` – UpTheCreek May 07 '13 at 06:32
  • @Asya using the 2.2 syntax gives "uncaught exception: couldn't add user: system.users entry must not have both 'roles' and 'readOnly' fields" – Gabriel May 17 '13 at 17:27
1

Starting of Mongodb 4.0, you have to use db.updateUser(), setting passwordDigestor key as "server", when updating the user password:

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
1

If you have an old password and you want to change the password then user mongo --username <USERNAME> and then use db.changeUserPassword("<USERNAME>", passwordPrompt()) .

Krunal
  • 938
  • 2
  • 10
  • 33