Reset mongodb root password

855 Views Asked by At

I have forgot my mongodb root user password for the shared cluster of 3 nodes. I have gone through stack overflow for the same issue but was unable to replicate due to different configuration. Below is my configuration

  1. mongodb version 4.4.
  2. replication on 3 servers(nodes) using keyfile authentication.
  3. all nodes are running in docker containers.

In case useful, I have other credentials that were created through root user for backup and read write permission but they dont have access to admin database.

Please guide me if you have any solution. thanks

unable to find anything to try

1

There are 1 best solutions below

2
On

The official way of doing this is:

Restart the MongoDB without authorization, i.e. mongod --noauth ... or via configuration file

security:
   authorization: disabled

Then you can logon without password and change credentials of the root user.

Attention: while the MongoDB is running without authorization, every user connects with root privileges, so you better restart the MongoDB in maintenance mode, i.e.

net:
   bindIp: localhost
   port: 55555

#replication:
#   replSetName: shardA

#sharding:
#   clusterRole: shardsvr

setParameter:
   skipShardingConfigurationChecks: true
   disableLogicalSessionCacheRefresh: true

Then you can connect only from localhost using port 55555 (which is not configured by other cluster members nor known by other users)

You need to do this only on the configuration server, because user accounts are stored there, not on the shards or mongos members.

See Perform Maintenance on Replica Set Members

However, there is a much simpler way to achieve the same, use the keyfile for authentication:

mongosh --authenticationDatabase local -u __system -p "$(tr -d '\011-\015\040' < /path/to/keyfile)"