mongodb 2 ways to change user password

  • 2020-05-06 11:58:06
  • OfStack

1, error, directly update the table


> db.system.users.update({"_id" : ObjectId("529e67553992b24438d5e315")},{"user":"tank2","readOnly" : false,"pwd":"123"}) 
> db.system.users.find(); 
{ "_id" : ObjectId("529e5f8474b4c660718a70f3"), "user" : "tank1", "readOnly" : false, "pwd" : "35dd47abff098f5b4f0b567db8edeac5" } 
{ "_id" : ObjectId("529e67553992b24438d5e315"), "user" : "tank2", "readOnly" : false, "pwd" : "123" } // That's not right  

2, the right way to use db


> db.addUser('tank2','111') 
{ 
  "_id" : ObjectId("529e6f1c8d95afd190add450"), 
  "user" : "tank2", 
  "readOnly" : false, 
  "pwd" : "6b4334d2c97c526e6a11b2f9ce1996e0" 
} 

Some people ask, isn't this the way to add users? Yes, this is the way to add users, but if the username is the same and the password is different, the password will be updated.

3, the correct way to use db.changeUserPassword


> db.changeUserPassword('tank2','test'); 

 


Related articles: