Overview
拥有相应特权的用户可更改自己的密码和自定义数据。Custom data 存储着可选的用户信息。
Considerations
要生成在此过程中使用的强密码,您可以使用 openssl 实用工具的 rand 命令。例如,使用以下选项发出 openssl rand 来创建 48 个伪随机字节长且采用 Base64 编码的字符串:
openssl rand -base64 48
先决条件
要修改自己的密码和自定义数据,您必须具有分别授予在用户数据库上执行 changeOwnPassword 和 changeOwnCustomData 动作的特权。
以具有管理用户和角色特权的用户身份进行连接。
连接到具有管理用户和角色特权的mongod或mongos ,例如具有userAdminAnyDatabase角色的用户。 以下过程将使用在对自管理部署启用访问控制中创建的myUserAdmin 。
mongosh --port 27017 -u myUserAdmin -p --authenticationDatabase 'admin'
If you do not specify the password to the -p command-line option, mongosh prompts for the password.
创建具有相应特权的角色。
在 admin 数据库中,create 具有 changeOwnPassword 和 changeOwnCustomData 的新角色。
use admin db.createRole( { role: "changeOwnPasswordCustomDataRole", privileges: [ { resource: { db: "", collection: ""}, actions: [ "changeOwnPassword", "changeOwnCustomData" ] } ], roles: [] } )
添加具有此角色的用户。
在 test 数据库中,create 具有创建的 "changeOwnPasswordCustomDataRole" 角色的新用户。例如,以下操作创建的用户同时具有内置角色 readWrite 和用户创建的 "changeOwnPasswordCustomDataRole"。
提示
您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。
use test db.createUser( { user:"user123", pwd: passwordPrompt(), // or cleartext password roles:[ "readWrite", { role:"changeOwnPasswordCustomDataRole", db:"admin" } ] } )
要向现有用户授予新角色,请使用 db.grantRolesToUser()。
步骤
使用适当的特权进行连接。
以具有相应特权的用户身份连接到 mongod 或 mongos。
例如,以下操作以在先决条件部分中创建的 user123 的身份连接到 MongoDB。
mongosh --port 27017 -u user123 --authenticationDatabase 'test' -p
If you do not specify the password to the -p command-line option, mongosh prompts for the password.
To check that you have the privileges specified in the Prerequisites section as well as to see user information, use the usersInfo command with the showPrivileges option.
更改您的密码和自定义数据。
使用 db.updateUser() 方法更新密码和自定义数据。
例如,以下操作将用户密码更改为 KNlZmiaNUp0B 并将自定义数据更改为 { title: "Senior Manager" }:
提示
您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。
use test db.updateUser( "user123", { pwd: passwordPrompt(), // or cleartext password customData: { title: "Senior Manager" } } )
根据提示输入密码。