On this page本页内容
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.
username |
string | The name of the user whose password you wish to change. |
password |
string | The user’s password. The value can be either:
Tip Starting in version 4.2 of the |
writeConcern |
document | writeConcern document takes the same fields as the getLastError command. |
To modify the password of another user on a database, you must have the changeAnyPassword
action on that database.
Warning
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 mongod and mongos for TLS/SSL.
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.
The following operation changes the password of the user named accountUser
in the products
database to SOh3TbYhx8ypJPxmt1oOfL
:
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. However, you can still specify the password directly as you would with earlier versions of the mongo
shell.
When prompted in the mongo
shell for the password, enter the new password.
You can also pass the new password directly to db.changeUserPassword()
: