MongoDB – How to Retrieve Admin Password on AWS EC2

mongodb

3rd party developers have installed MongoDB on AWS EC2 instance and later handed over code to me.

Now I don't know what's the password for admin user of MongoDB but I do have ssh keys to connect to that EC2 instance.

My query is how to reset password for MongoDB admin user ?

Best Answer

You can't retrieve existing passwords, but you can reset them assuming you have ssh access to the host and appropriate root or sudo permissions to edit the MongoDB configuration and restart the MongoDB service.

The exact steps may vary depending on your O/S and how you manage the MongoDB server, but the general process for resetting a password on a standalone MongoDB server would be:

  1. Take a backup of the configuration file used by your used by your MongoDB service (typically /etc/mongod.conf for Linux installations).
  2. Edit the current configuration file to set:
    • security.authorization to disabled
    • net.bindIp to localhost (you don't want to allow remote connections while access control is disabled)
  3. Restart your MongoDB service using the relevant command for your O/S (typically sudo service mongod restart in Linux).
  4. Login to mongod locally without auth using the mongo shell: mongo localhost/admin.
  5. Reset the password for your admin user: db.changeUserPassword('myAdminUser','New&secur3').
  6. Replace your MongoDB configuration file with the original that you backed up in the first step.
  7. Restart the service: sudo service mongod restart.