Creating a Database and Password

Admitting that the admin user is named admin and that the password for the admin user is password123, the mongo command-line can be reached by executing the following command:

mongo -u admin -p password123

The following instructions in succession can then be used to create a database and then a user named test with the password also test and with ownership rights granted on the database main:

use main
db.createUser({user: "main", pwd: "main", roles: [{role: "dbOwner", db: "main"}]});

Dynamically Updating Replica Set Hostname for MongoDB Docker Containers

Unfortunately, after a replica set has been created with MongoDB with a given hostname, the hostname is irrevocably bound to the replica set such that moving a Docker container to a new network or just changing the hostname will result is MongoDB being unable to access the replica set due to not being able to connect. This results in errors such as:

{"t":{"$date":"2025-10-07T12:33:31.022+00:00"},"s":"I",  "c":"-",        "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"ReadConcernMajorityNotAvailableYet: Read concern majority reads are currently not possible.","nextWakeupMillis":800}}

It is possible to overcome this problem by changing the official Docker container entry point to a custom script that will update the replica set hostname and set the hostname to whatever is passed to the Docker container as a hostname (for example, via --hostname foo, where foo is the desired hostname). The script is as follows:

#!/bin/bash
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2025 - License: MIT            ##
###########################################################################
# This script is meant to replace the entry point of the mongodb docker   #
# container in order to dynamically update a replica set hostname. The    #
# docker container will then be able to just be started with a different  #
# hostname without bothering about manually updating the replica set.     #
#                                                                         #
# For more information, see: https://grimore.org/fuss/mongodb#dynamica... #
# ...lly_updating_replica_set_hostname_for_mongodb_docker_containers      #
###########################################################################
 
###########################################################################
##                            CONFIGURATION                              ##
###########################################################################
REPLICA_SET="overleaf"
 
###########################################################################
##                               INTERNALS                               ##
###########################################################################
mongod --replSet "$REPLICA_SET" --bind_ip_all &
MONGO_PID=$!
 
cat >>/tmp/setHostname.js <<-EOF
cfg = rs.conf()
cfg.members[0].host = "$HOSTNAME:27017"
rs.reconfig(cfg, {force: true})
EOF
 
cat >>/tmp/checkHostname.js <<-EOF
print(rs.conf().members[0].host)
EOF
 
# loop while the hostname is not properly set
while : ; do
  mongosh mongodb://127.0.0.1:27017 -f /tmp/setHostname.js
  CURRENT_HOSTNAME=`mongosh mongodb://127.0.0.1:27017 -f /tmp/checkHostname.js | awk -F':' '{ print $1 }'`
  [ "$CURRENT_HOSTNAME" != "$HOSTNAME" ] || break
  sleep 1
done
 
trap '{ kill -s TERM "$MONGO_PID"; }' HUP INT TERM
wait $MONGO_PID

and note that the replica set name, in this case overleaf, must be passed to the REPLICA_SET variable.

The script has to be saved to some place accessible to Docker, and then a Docker command that uses the script to start mongo would be:

/usr/bin/docker run \
  --rm \
  --name=overleaf-mongo \
  --hostname=overleaf-mongo \
  --interactive \
  --volume /opt/overleaf/toolkit/data/mongo:/data/db \
  --volume /data/configdb \
  --expose=27017 \
  -v /mnt/docker/data/mongodb-overleaf/:/init \
  --runtime=runc \
  --entrypoint bash \
  mongo/mongo:6.0 \
  /init/setHostname.sh

where:

  • /mnt/docker/data/mongodb-overleaf/ is the place where the script setHostname.sh is to be found on the Docker host

fuss/mongodb.txt ยท Last modified: by office

Wizardry and Steamworks

© 2025 Wizardry and Steamworks

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.