How can I enter Mongo as a superuser or reset users?
If you have locked yourself out then you need to do the following:
- Stop your MongoDB instance
- Remove the --auth and/or --keyfile options from your MongoDB config to disable authentication
- Start the instance without authentication
- Edit the users as needed
- Restart the instance with authentication enabled
I had a similar issue when I created a user without adding a superuser first. The following steps helped solve the problem:
- Open the MongoDB configuration file using
sudo
(sudo vi mongodb.conf
).
The file can be found in/etc/
folder. - Comment
"auth = true"
. - Stop the MongoDB service (
sudo service mongod stop
) - Start the MongoDB service (
sudo service mongod start
) Then create "root" superuser using the below command:
use admin db.createUser({user:"admin",pwd:"password",roles:[{role:"root",db:"admin"}]});
For reference https://docs.mongodb.com/manual/reference/built-in-roles/#superuser-roles
Go back and uncomment
"auth=true"
. Stop and Start/Restart the mongodb service.
If you use a replica set with a keyfile
Security between members of the replica set using Internal Authentication
Keyfile is used for auth in replication.
security:
keyFile: <path-to-keyfile>
replication:
replSetName: <replicaSetName>
You can use this command to login as a admin to mongod:
mongo -u __system -p "$(tr -d '\011-\015\040' < path-to-keyfile)" --authenticationDatabase local
Afterword you are able to create or modify your admin user.
Internal Role
__system
MongoDB assigns this role to user objects that represent cluster members, such as replica set members and mongos instances. The role entitles its holder to take any action against any object in the database.Do not assign this role to user objects representing applications or human administrators, other than in exceptional circumstances.