4 min to read
Changing MongoDB root password
Changing the password for good
Recently I had to change a MongoDB password from a self hosted MongoDB installation in a Kubernetes cluster, only because the password was not that strong (the password for root was password), and thanks to that I had to update it, it was not that easy as I tought, that’s why I am making this small blog on how to update the password for good.
What MongoDB is?
MongoDB is a document database used to build highly available and scalable internet applications. With its flexible schema approach, it’s popular with development teams using agile methodologies. Offering drivers for all major programming languages, MongoDB allows you to immediately start building your application without spending time configuring a database. This was done over MongoDB 5.0.9.
Why Use MongoDB?
MongoDB is built on a scale-out architecture that has become popular with developers of all kinds for developing scalable applications with evolving data schemas.
As a document database, MongoDB makes it easy for developers to store structured or unstructured data. It uses a JSON-like format to store documents. This format directly maps to native objects in most modern programming languages, making it a natural choice for developers, as they don’t need to think about normalizing data. MongoDB can also handle high volume and can scale both vertically or horizontally to accommodate large data loads.
MongoDB was built for people building internet and business applications who need to evolve quickly and scale elegantly. Companies and development teams of all sizes use MongoDB for a wide variety of reasons.
Changing the password
In my case, I was connecting to a MongoDB with multiple replicas so I needed to connect to primary node in order to do the actual write/update of the password, typically this procedure is not allowed on secondary nodes to maintain consistency.
To change the root password or perform other administrative actions, we need to connect to the primary node in the replica set. The primary node is the one that is allowed to perform write operations, such as updating user information.
1.- Identify the Primary Node:
Connect to any of the nodes in the replica set and run the following command to identify the primary node:
Look for the node with the stateStr field set to “PRIMARY.”
2.- Connect to the Primary Node:
Use the mongo shell to connect to the primary node:
mongo <primary-node-host>:<primary-node-port>/admin -u admin -p
3.- Switch to the Admin Database:
Once connected to the primary node, switch to the admin database:
4.- Update the Password:
Then, update the root user’s password using the updateUser command:
Replace new_root_password with the new password you want to set.
5.- Exit the MongoDB Shell:
After successfully changing the password, exit the MongoDB shell:
6.- Restart MongoDB:
kubectl rollout status statefulset/mongodb
That should do it, you will be able to connecto to your self hosted MongoDB instance using your new secure password (I updated the password to “strongpassword123”)