Skip to content

Instantly share code, notes, and snippets.

@shivampip
Created November 6, 2019 16:31
Show Gist options
  • Save shivampip/afa8b1dbb997c99619c593d75330e246 to your computer and use it in GitHub Desktop.
Save shivampip/afa8b1dbb997c99619c593d75330e246 to your computer and use it in GitHub Desktop.
MongoDB shell admin password reset
  • open mongod.conf
cd /etc/mongod.conf
  • Comment security
#security:
#  authroization: "enabled"
  • Restart mongodb
sudo service mongod stop
sudo service mongod start
  • Run mongo
mongo
  • Run these cmds
use admin
db.createUser({user:"admin",pwd:"password",roles:[{role:"root",db:"admin"}]});
  • Enable auth again what you had commented in /etc/mongod.conf file (remove comments)

  • Restart mongod service again

  • DONE

@hongyi-zhao
Copy link

hongyi-zhao commented Jun 20, 2024

Hi hanksu-deloitte:

I think that the steps to reset the root password for a 3-node MongoDB replica set can be done as follows:

Stop the MongoDB service on all nodes:

sudo service mongod stop

Start one of the MongoDB instances without authentication:

mongod --dbpath /var/lib/mongodb --port 27017 --bind_ip localhost --replSet yourReplicaSetName --fork --logpath /var/log/mongodb/mongod.log

Connect to the started MongoDB instance using mongosh:
mongosh --port 27017

In the MongoDB shell, execute the following commands to reset the root user's password:

use admin
db.updateUser("root", {pwd: "newpassword"});

Shut down the MongoDB instance:

mongosh admin --eval "db.shutdownServer()"

Restore the security settings in /etc/mongod.conf:

sudo nano /etc/mongod.conf
# Uncomment the security section
# security:
#   authorization: "enabled"

Restart the MongoDB service on all nodes:

sudo service mongod start

Check the replica set status to ensure all nodes are running correctly:

mongosh --username root --password newpassword --authenticationDatabase admin --eval "rs.status()"

By following these steps, you should be able to successfully reset the root password for your 3-node MongoDB replica set. Make sure to understand the potential risks and impacts of performing these operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment