MongoDB
Overview
MongoDB Community Server is the open-source document database. This SecureAnyCloud image installs and configures it from the official MongoDB package repository. The database listens on the standard MongoDB wire protocol port, and the mongosh shell is preinstalled for local administration and scripting.
What you get:
- MongoDB Community Server, installed from the official MongoDB package repository
- Authentication enabled by default, with no shared or default database credentials in the image
- Two strong, instance-unique passwords generated on first boot by a one-shot service: an admin superuser in the admin database, and an application user (SecureAnyCloud) in the appdb database
- Both passwords written to /root/mongodb-credentials.txt, readable only by the root user
- A separate, independently resizable storage volume at the MongoDB data directory, which holds the database files and journal. It keeps database storage off the operating system disk, so you can grow, snapshot and back it up on its own schedule
- A small nginx healthcheck shim on port 80 that serves a static
OKresponse, so external HTTP probes have a 2xx endpoint to hit - MongoDB client traffic over its TCP wire protocol on port 27017
Prerequisites
Before you deploy this image, you need:
- An Amazon Web Services account where you can launch EC2 instances
- IAM permissions to launch instances, create security groups and subscribe to AWS Marketplace products
- An EC2 key pair in the target Region for SSH access to the instance
- A VPC and subnet in the target Region, with a security group that allows inbound port 22 from your management network
- The AWS CLI version 2 installed locally, if you plan to deploy from the command line
Launch the instance from AWS Marketplace
Sign in to the AWS Management Console, open the EC2 service, and select Launch instance. Under Application and OS Images, choose AWS Marketplace AMIs and search for MongoDB Community Server.
Select the SecureAnyCloud listing and choose Select, then Continue on the subscription summary.
Pick an instance type of m5.large or larger as a balanced default. Size the instance to the working set your collections require. Choose your EC2 key pair under Key pair (login).
Under Network settings, select your VPC and subnet. Create or select a security group that allows inbound port 22 from your management network. If your application servers run in the same VPC and need to reach the database, also open inbound TCP port 27017 from those servers' security group.
Select Launch instance. First-boot initialization takes about a minute after the instance state becomes Running and the status checks pass. During that window, mongodb-firstboot.service rotates the admin and application passwords.
Launch the instance from the AWS CLI
The following command launches an instance from the SecureAnyCloud MongoDB Community Server Marketplace AMI into an existing subnet and security group.
Replace <ami-id> with the AMI ID shown on the Marketplace listing, <key-name> with your EC2 key pair name, and <subnet-id> with your subnet ID. Replace <security-group-id> with a security group that opens port 22 from your management network and, optionally, port 27017 from your application tier.
aws ec2 run-instances \
--image-id <ami-id> \
--instance-type m5.large \
--key-name <key-name> \
--subnet-id <subnet-id> \
--security-group-ids <security-group-id> \
--block-device-mappings '{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":30,"VolumeType":"gp3"}}' \
--metadata-options 'HttpTokens=required,HttpEndpoint=enabled' \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=mongodb-server}]'The image attaches a dedicated 30 GiB database storage volume automatically, mounted at the MongoDB data directory. To start with more database storage, enlarge that volume on the Storage step in the console, or add a second block device mapping on the CLI.
Connect to the instance over SSH
Connect to the instance with SSH as the default login user for the operating system variant you launched. The login user differs by variant:
Operating system variant SSH login user
Ubuntu 24.04 ubuntu
Replace <key-file> with the path to your private key file and <instance-public-ip> with the public IP address or DNS name of the instance.
ssh -i <key-file> ubuntu@<instance-public-ip>Read the per-instance database passwords
On first boot, the mongodb-firstboot.service one-shot generates two passwords unique to your instance. It writes them to /root/mongodb-credentials.txt, which only the root user can read. Display the file with sudo:
sudo cat /root/mongodb-credentials.txtThe file lists two accounts:
MONGO_ADMIN_USER=adminwith rolerootin theadmindatabase, for full cluster administrationMONGO_APP_USER=sacwith rolesreadWriteanddbAdminin theappdbdatabase, for the application tier
Both passwords are unique to this instance. They were created by openssl rand -hex 16 on first boot. Keep them in a secrets manager, and treat the credentials file as read-only on the instance.
Confirm the MongoDB server
Check the installed server and shell versions, then the dedicated data volume.
mongod --version | head -1mongosh --versionConfirm that the database service and the healthcheck shim are running:
systemctl is-active mongod.service nginx.serviceConfirm that mongod is bound to all interfaces on port 27017, and that the database data directory is on its own filesystem:
sudo ss -tln | grep ':27017' findmnt /var/lib/mongodb df -h /var/lib/mongodbOpen a shell as the admin user
Use the admin password from /root/mongodb-credentials.txt to open mongosh against the local node, authenticating against the admin database.
The command below reads the password from the credentials file, so you never type the plaintext on the command line.
ADMIN_PASS=$(sudo grep '^MONGO_ADMIN_PASSWORD=' /root/mongodb-credentials.txt | cut -d= -f2-) mongosh --host 127.0.0.1 --port 27017 -u admin -p "$ADMIN_PASS" --authenticationDatabase admin --eval 'db.adminCommand({ping:1}).ok'The command prints 1 if the admin user authenticated and the deployment responded to ping.
To start an interactive shell session as the admin user, drop the --eval argument:
ADMIN_PASS=$(sudo grep '^MONGO_ADMIN_PASSWORD=' /root/mongodb-credentials.txt | cut -d= -f2-) mongosh --host 127.0.0.1 --port 27017 -u admin -p "$ADMIN_PASS" --authenticationDatabase adminInside the interactive shell, list the databases with show dbs. Leave the shell with exit.
Run an application-user round trip
The application user (SecureAnyCloud) has the readWrite and dbAdmin roles on the appdb database.
The command below reads the application password from the credentials file and runs an end-to-end probe. It inserts a document, finds it again, then drops the test collection.
APP_PASS=$(sudo grep '^MONGO_APP_PASSWORD=' /root/mongodb-credentials.txt | cut -d= -f2-) mongosh --quiet --host 127.0.0.1 --port 27017 -u sac -p "$APP_PASS" --authenticationDatabase appdb appdb --eval ' db.demo.insertOne({hello: "world", ts: new Date()}); printjson(db.demo.findOne({hello: "world"})); db.demo.drop(); 'SecureAnyCloud's build pipeline uses this same round trip to prove that the rotated application password works on every image before the AMI ships.
Connect from an application server
To connect from another EC2 instance in the same VPC, give that instance a security group that allows outbound TCP 27017 to the MongoDB instance. Then add an inbound rule on the MongoDB instance's security group that allows TCP 27017 from the application server's security group.
A standard MongoDB connection string for the application user looks like this. Replace <MONGO_APP_PASSWORD> with the password shown in /root/mongodb-credentials.txt, and <mongodb-private-ip> with the MongoDB instance's private IP address.
mongodb://sac:<MONGO_APP_PASSWORD>@<mongodb-private-ip>:27017/appdb?authSource=appdbMost MongoDB drivers accept this connection string directly. The equivalent mongosh command is:
mongosh "mongodb://sac:<MONGO_APP_PASSWORD>@<mongodb-private-ip>:27017/appdb?authSource=appdb"Check database storage
Database files and the WiredTiger journal are stored under /var/lib/mongodb, a separate filesystem on its own EBS volume. Confirm the dedicated mount and review free space with:
findmnt /var/lib/mongodb df -h /var/lib/mongodbBecause the data directory is on its own volume, you can grow it independently of the operating system disk. Modify the EBS volume in the AWS console or with the CLI. Then extend the filesystem on the instance with sudo resize2fs against the volume's device.
Maintain the instance
Keep the operating system patched with the standard package manager. The official MongoDB APT repository is already configured on the image, so MongoDB Community Server upgrades arrive through the normal package update process.
Review the database log files under /var/log/mongodb/ for slow operations and connection events. Use the mongotop and mongostat tools from the mongodb-database-tools package to monitor server activity. Alternatively, connect a metrics collector to the diagnostic data under /var/lib/mongodb/diagnostic.data.
To take a logical backup of the appdb database, run mongodump authenticated as the application user. Restore with mongorestore. Run mongodump --help and mongorestore --help on the instance for the full set of options.
For a multi-node replica set, add further nodes, each running this same image or any compatible MongoDB build. Reconfigure replication.replSetName in /etc/mongod.conf on every node and restart mongod.service. Then run rs.initiate() followed by rs.add() from the primary's mongosh session.
Support
SecureAnyCloud provides this Amazon Machine Image with 24/7 technical support by email and chat. Contact SecureAnyCloud for help with deployment, replica set configuration, schema design, performance tuning, indexing and database administration.
MongoDB is a trademark of MongoDB, Inc. All other product and company names are trademarks or registered trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
Deploy on AWS
Launch MongoDB Community Server on AWS with 24/7 support from SecureAnyCloud.
View on Marketplace
Need Help?
Our support team is available 24/7.
sales@secureanycloud.com