定义
db.changeUserPassword(username, password)更新用户密码。在定义用户的数据库中运行方法,即用户使用
created创建的数据库。重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.有关数据库命令,请参阅
updateUser命令。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
Parameter类型说明username字符串
希望更改其密码的用户的名称。
password字符串
用户的密码。该值可为以下任一项:
以明文字符串表示的用户密码,或
passwordPrompt()提示输入用户密码。您可以将
passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的mongoshell 一样直接指定密码。
writeConcern文档
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
必需的访问权限
要修改数据库其他用户的密码,必须对该数据库执行 changePassword 操作。
行为
警告
By default, db.changeUserPassword() sends all specified data to the MongoDB instance in cleartext, even if using 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 MongoDB Instances for TLS/SSL Encryption.
MongoDB 不以明文形式存储密码。仅当未启用 TLS 传输加密时,密码才会在客户端与服务器之间的传输过程中容易受到攻击。
例子
以下操作将 products 数据库中名为 accountUser 的用户的密码更改为 SOh3TbYhx8ypJPxmt1oOfL:
提示
您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。
use products db.changeUserPassword("accountUser", passwordPrompt())
When prompted in mongosh for the password, enter the new password.
You can also pass the new password directly to db.changeUserPassword():
use products db.changeUserPassword("accountUser", "SOh3TbYhx8ypJPxmt1oOfL")