Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to change the password in MongoDB for existing user?
To change the password in MongoDB for an existing user, use the changeUserPassword() method. This operation requires administrative privileges and must be performed on the database where the user was created.
Syntax
db.changeUserPassword("username", "newPassword");
Step 1: Switch to Admin Database
First, switch to the admin database where user management operations are performed ?
use admin
switched to db admin
Step 2: Display Existing Users
Check the current users in the database ?
db.getUsers();
[
{
"_id": "admin.John",
"user": "John",
"db": "admin",
"roles": [
{
"role": "userAdminAnyDatabase",
"db": "admin"
}
],
"mechanisms": [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
]
Step 3: Change User Password
Update the password for the existing user "John" ?
db.changeUserPassword("John", "newSecurePassword123");
The password has been successfully changed. The user can now authenticate with the new password.
Key Points
- You must have userAdmin or userAdminAnyDatabase role to change passwords.
- The operation must be performed on the database where the user was originally created.
- Password changes take effect immediately.
Conclusion
Use changeUserPassword() to update existing user passwords in MongoDB. Ensure you have proper administrative privileges and perform the operation on the correct database where the user exists.
Advertisements
