Resetting admin password for a container running MongoDB

For unknown reasons I cannot connect as admin to my self-hosted MongoDB running in a docker container. This container is a standard 6.0 container launched as part of a group with docker compose. The following is the relevant part of docker-compose.yml:

    image: "mongo:6.0"
    command: ["--auth"]
    restart: unless-stopped
      - .env
      - "${MPORT}:${MPORT}"
      - mongo_mema_data:/data/db
      - memanet

and the .env file contains (passwords are fictitious):


I tried the following from the same directory where .env and docker-compose.yml are:
docker compose restart mongo_mema
and would have expected to be able to use the following connection URL:
from within the running container (or the host since 27017 is exposed on the host too) but the authentication fails.
This container is running a database in use so I need to minimize downtimes.
What error am I doing?
Thanks a lot.

You already know the procedure per your other thread you just need to execute it correctly.

In this instance I would stop the container and the start a new one on the command line using the --volumes-from argument to start a mongod without --auth

docker run --name resetMongoPW --rm -d --volumes-from projectName_mongo_mema mongo:6.0

docker exec resetMongoPW mongosh --quiet --eval --quiet 'db.getSiblingDB("admin").changeUserPassword("admin","password")'

These are only for the initial db, they won’t update after the initialisation.

1 Like

Thanks Chris I appreciate it. Very godd to know those envs are only for the initial setup, I had overlooked that.

Never used the --volumes-from option. Shall study a bit. From your example not sure what object you’re referring to with the string projectName_mongo_mema . The volume I use for persistance as per the docker-compose.yml file is mongo_mema_data

docker volume ls gives:

local     mema_services_mongo_mema_data
local     mema_services_mongodb_data
local     mongodb_data

Take care.