MongoDB 8.0
MongoDB is a general-purpose, document-oriented NoSQL database that stores data as flexible JSON-like documents. It is well suited for applications that need rich queries, horizontal scaling, or schemas that evolve over time.
Software included
Section titled “Software included”| Component | Version |
|---|---|
| MongoDB Community | 8.0.x |
| Ubuntu | 24.04 LTS |
Environment variables
Section titled “Environment variables”You can optionally set these when deploying MongoDB from the marketplace. Leave any field blank to have a secure random value generated automatically.
| Variable | Description |
|---|---|
MONGO_INITDB_ROOT_USERNAME | MongoDB root username |
MONGO_INITDB_ROOT_PASSWORD | MongoDB root password |
Getting started
Section titled “Getting started”1. Connect to your VM
Section titled “1. Connect to your VM”ssh ubuntu@<your-vm-ip>2. Wait for first-boot configuration
Section titled “2. Wait for first-boot configuration”On the first boot, a setup script runs automatically. It creates the superuser defined by
MONGO_INITDB_ROOT_USERNAME, which defaults to admin, using the configured or generated password,
and enables authentication. This takes under 60 seconds.
Track progress:
journalctl -u mongodb-first-boot.service -fWait for the service to exit, then disconnect and reconnect. On login, the MOTD reports the MongoDB
version with a READY status once setup is complete.
3. Retrieve credentials
Section titled “3. Retrieve credentials”sudo cat /etc/mongodb/credentials.txtThis file contains the admin username, password, and connection instructions. It is only readable by
root. The following command reads the configured username from the file. mongosh then prompts for
the password so it does not appear in the process list.
4. Connect to MongoDB
Section titled “4. Connect to MongoDB”MONGO_USER=$(sudo awk '/^Username:/{print $NF}' /etc/mongodb/credentials.txt)mongosh -u "$MONGO_USER" --authenticationDatabase adminExpected output:
test>Managing MongoDB
Section titled “Managing MongoDB”# Check service statussystemctl status mongod
# Restartsudo systemctl restart mongod
# View logssudo journalctl -u mongod -fConfiguration file: /etc/mongod.conf
Security
Section titled “Security”Port 27017 is not open externally by default. UFW is enabled and allows SSH (port 22) only.
To allow access from a specific IP:
sudo ufw allow from <trusted-ip> to any port 27017To connect without opening the firewall (recommended), use an SSH tunnel:
# Run this on your local machinessh -L 27017:localhost:27017 ubuntu@<your-vm-ip>
# Then connect locally using the username from /etc/mongodb/credentials.txtmongosh -u <username> --authenticationDatabase admin