Database Manual / Reference / mongosh Methods / User Management

db.changeUserPassword() (mongosh method方法)

Definition定义

db.changeUserPassword(username, password)

Updates a user's password. Run the method in the database where the user is defined, i.e. the database you created the user.更新用户的密码。在定义用户的数据库中运行该方法,即您created用户的数据库。

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.本页记录了一种mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。

For the database command, see the updateUser command.有关数据库命令,请参阅updateUser命令。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Parameter参数Type类型Description描述
usernamestring字符串The name of the user whose password you wish to change.您要更改其密码的用户的名称。
passwordstring字符串

The user's password. The value can be either:用户的密码。该值可以是:

  • the user's password in cleartext string, or明文字符串形式的用户密码,或
  • passwordPrompt() to prompt for the user's password.提示输入用户密码。

    You can use the passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.您可以将passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。但是,您仍然可以像使用早期版本的mongo shell一样直接指定密码。

writeConcerndocument文档Optional. 可选。The level of write concern for the operation. 操作的写入关注级别。See Write Concern Specification.请参阅写入关注规范

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

Important

This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Required Access所需访问权限

To modify the password of another user on a database, you must have the changePassword action on that database.要修改数据库上其他用户的密码,您必须对该数据库执行changePassword操作

Behavior行为

Warning

By default, db.changeUserPassword() sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). 默认情况下,db.changeUserPassword()以明文形式将所有指定的数据发送到MongoDB实例,即使使用passwordPrompt()也是如此。Use TLS transport encryption to protect communications between clients and the server, including the password sent by db.changeUserPassword(). For instructions on enabling TLS transport encryption, see Configure mongod and mongos for TLS/SSL on Self-Managed Deployments.使用TLS传输加密来保护客户端和服务器之间的通信,包括db.changeUserPassword()发送的密码。有关启用TLS传输加密的说明,请参阅在自我管理部署上为TLS/SSL配置mongodmongos

MongoDB does not store the password in cleartext. The password is only vulnerable in transit between the client and the server, and only if TLS transport encryption is not enabled.MongoDB不以明文形式存储密码。密码仅在客户端和服务器之间传输时易受攻击,并且只有在未启用TLS传输加密的情况下才易受攻击。

Example示例

The following operation changes the password of the user named accountUser in the products database to SOh3TbYhx8ypJPxmt1oOfL:以下操作将产品数据库中名为accountUser的用户的密码更改为SOh3TbYhx8ypJPxmt1oOfL

Tip

You can use the passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.您可以将passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。但是,您仍然可以像使用早期版本的mongo shell一样直接指定密码。

use products
db.changeUserPassword("accountUser", passwordPrompt())

When prompted in mongosh for the password, enter the new password.当在mongosh中提示输入密码时,请输入新密码。

You can also pass the new password directly to db.changeUserPassword():您还可以将新密码直接传递给db.changeUserPassword()

use products
db.changeUserPassword("accountUser", "SOh3TbYhx8ypJPxmt1oOfL")