On this page本页内容
Users with appropriate privileges can change their own passwords and custom data.具有适当权限的用户可以更改自己的密码和自定义数据。Custom data
stores optional user information.Custom
数据存储可选的用户信息。
To generate a strong password for use in this procedure, you can use the 要生成用于此过程的强密码,可以使用openssl
utility’s rand
command.openssl
实用工具的rand
命令。For example, issue 例如,使用以下选项发出openssl rand
with the following options to create a base64-encoded string of 48 pseudo-random bytes:openssl rand
,以创建48个伪随机字节的base64编码字符串:
To modify your own password and custom data, you must have privileges that grant 要修改您自己的密码和自定义数据,您必须具有在用户数据库上分别授予changeOwnPassword
and changeOwnCustomData
actions respectively on the user’s database.changeOwnPassword
和changeOwnCustomData
操作的权限。
Connect to the 连接到具有管理用户和角色权限的mongod
or mongos
with privileges to manage users and roles, such as a user with userAdminAnyDatabase
role.mongod
或mongos
,例如具有userAdminAnyDatabase
角色的用户。The following procedure uses the 下面的过程使用在启用访问控制中创建的myUserAdmin
created in Enable Access Control.myUserAdmin
。
If you do not specify the password to the 如果没有为-p
command-line option, the mongo
shell prompts for the password.-p
命令行选项指定密码,mongo
shell将提示输入密码。
In the 在admin
database, create
a new role with changeOwnPassword
and changeOwnCustomData
.admin
数据库中,使用changeOwnPassword
和changeOwnCustomData
创建一个新角色。
In the 在test
database, create
a new user with the created "changeOwnPasswordCustomDataRole"
role.test
数据库中,使用创建的changeOwnPasswordCustomDataRole
角色创建一个新用户。For example, the following operation creates a user with both the built-in role 例如,以下操作将创建一个同时具有内置角色readWrite
and the user-created "changeOwnPasswordCustomDataRole"
.readWrite
和用户创建的changeOwnPasswordCustomDataRole
角色的用户。
Tip
Starting in version 4.2 of the 从mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call.mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
To grant an existing user the new role, use 若要授予现有用户新角色,请使用db.grantRolesToUser()
.db.grantRolesToUser()
方法。
Connect to the 以具有适当权限的用户身份连接到mongod
or mongos
as a user with appropriate privileges.mongod
或mongos
。
For example, the following operation connects to MongoDB as 例如,以下操作作为在先决条件部分中创建的user123
created in the Prerequisites section.user123
连接到MongoDB。
If you do not specify the password to the 如果没有为-p
command-line option, the mongo
shell prompts for the password.-p
命令行选项指定密码,mongo
shell将提示输入密码。
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.showPrivileges
选项的usersInfo
命令。
Use the 使用db.updateUser()
method to update the password and custom data.db.updateUser()
更新密码和自定义数据的方法。
For example, the following operation changes the user’s password to 例如,以下操作将用户的密码更改为KNlZmiaNUp0B
and custom data to { title: "Senior Manager" }
:KNlZmiaNUp0B
,将自定义数据更改为{ title: "Senior Manager" }
:
Tip
Starting in version 4.2 of the 从mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call.mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。mongo
shell.
Enter the password when prompted.出现提示时输入密码。