您需要使用changeUserPassword()
来更改用户密码。首先让我们创建一个具有某些角色的用户。以下是在MongoDB中创建用户的查询-
> use admin switched to db admin > db.createUser( ... { ... user: "Chris", ... pwd: "chris", ... roles: [ { role: "readWrite", db: "test" } ] ... } ... ); Successfully added user: { "user" : "Chris", "roles" : [ { "role" : "readWrite", "db" : "test" } ] }
让我们从测试数据库中显示用户-
> db.getUser("Chris");
这将产生以下输出-
{ "_id" : "admin.Chris", "user" : "Chris", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
以下是更改MongoDB用户密码的查询。
> db.changeUserPassword("Chris", "123456");
密码已更改。