MongoDB: How to reset root password on Mac

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

As with any database system, security is of paramount importance in MongoDB. One critical aspect of database security is the management of user passwords. This guide will walk you through the process of resetting the root (administrator) password for MongoDB on a Mac. This process is handy in case you’ve forgotten the password or have taken over an existing MongoDB installation with an unknown password.

Step-by-Step Instructions

Step 1: Stop the MongoDB Service

To begin the password reset process, you need to stop the running MongoDB service. You can do this by running the following command in the terminal:

brew services stop mongodb-community@

This will stop the MongoDB service if it was started using Homebrew, which is a common method of installation on Mac.

Step 2: Start MongoDB Without Access Control

Next, you’ll need to restart the MongoDB server without access control to gain unrestricted access to the databases. Do this by running:

mongod --port 27017 --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork --auth

Ensure that you replace ‘/usr/local/var/mongodb’ with the actual path to your database storage and ‘/usr/local/var/log/mongodb/mongo.log’ with the path to your log file if it’s located elsewhere. The ‘–fork’ option runs the server process in the background.

Step 3: Connect to MongoDB

With MongoDB running without access control, you can now connect to the database without a password:

mongo --port 27017

This connects to the server using the default MongoDB port (27017).

Step 4: Switch to the admin Database

In the Mongo shell, switch to the admin database, as this is where user credentials are stored:

use admin

Step 5: Set the new Root Password

Now, you can set a new password for the root user by using the following commands:

db.createUser({
 user: 'root',
 pwd: 'newpassword',
 roles: ['root']
})

Make sure to replace ‘newpassword’ with your desired secure password.

Step 6: Restart MongoDB with Access Control

After setting the new password, you need to enable access control again. First, stop the MongoDB server process that’s running without access control:

use admin
db.shutdownServer()

Then start the MongoDB service normally (with access control enabled) using Homebrew:

brew services start mongodb-community@

Additional Security Measures

Beyond changing your root password, consider implementing additional security measures, such as enabling SSL connections, setting up IP whitelisting, and using firewalls to protect your MongoDB instance.

Troubleshooting

If you encounter issues during this process, ensure that:

  • The paths to the MongoDB database and log file are correct.
  • You have the necessary permissions to stop and start the MongoDB service.
  • No other instances of MongoDB are running on conflicting ports.

Conclusion

Resetting the root password on MongoDB is crucial if you’ve forgotten it or if you’re securing your database after taking over an existing installation. By following the steps outlined in this tutorial, you can reset your root password and ensure your MongoDB installation on Mac is secured.