Installing and Configuring MongoDB
| Platform | Command | Notes |
| macOS (Homebrew) | brew tap mongodb/brew && brew install mongodb-community@7.0 | Installs MongoDB 7.0 LTS |
| Ubuntu/Debian | apt-get install -y mongodb-org | Requires adding MongoDB GPG key & repo |
| RHEL/CentOS | yum install -y mongodb-org | Configure /etc/yum.repos.d/mongodb-org-7.0.repo first |
| Windows | msiexec /i mongodb-windows-x86_64-7.0.msi | Or use the MSI installer GUI |
| Docker | docker run -d -p 27017:27017 --name mongo mongo:7.0 | Official image, mount /data/db for persistence |
Example: macOS install and start
brew tap mongodb/brew
brew install mongodb-community@7.0
brew services start mongodb-community@7.0
mongosh --eval "db.runCommand({ ping: 1 })"
2. Installing MongoDB Enterprise Edition
| Feature | Community | Enterprise |
| License | SSPL | Commercial |
| In-Memory Engine | No | Yes |
| LDAP / Kerberos auth | No | Yes |
| Encryption at Rest | No | Yes |
| Auditing | No | Yes |
| Support | Community | MongoDB Inc. |
# Ubuntu
wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-enterprise.gpg
echo "deb [signed-by=/usr/share/keyrings/mongodb-enterprise.gpg] https://repo.mongodb.com/apt/ubuntu jammy/mongodb-enterprise/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise-7.0.list
sudo apt-get update && sudo apt-get install -y mongodb-enterprise
3. Setting Up mongod Configuration File
| Section | Purpose | Key Settings |
| storage | Data path & engine | dbPath, engine, wiredTiger |
| systemLog | Logging | destination, path, logAppend |
| net | Network bindings | port, bindIp, tls |
| security | Auth & keyfile | authorization, keyFile |
| replication | Replica set config | replSetName, oplogSizeMB |
| sharding | Cluster role | clusterRole |
| processManagement | Daemon options | fork, pidFilePath |
Example: /etc/mongod.conf (YAML)
storage:
dbPath: /var/lib/mongo
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 4
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
net:
port: 27017
bindIp: 127.0.0.1,10.0.0.5
security:
authorization: enabled
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
4. Configuring Network Bindings
| Setting | Value | Effect |
| bindIp | 127.0.0.1 | Localhost only (default, safest) |
| bindIp | 0.0.0.0 | All interfaces (requires firewall) |
| bindIp | 10.0.0.5,127.0.0.1 | Specific interfaces (comma-separated) |
| bindIpAll | true | Equivalent to 0.0.0.0 |
| port | 27017 | Default mongod; 27018 (shard), 27019 (configsvr) |
| maxIncomingConnections | 65536 | Max concurrent client connections |
Warning: Never expose bindIp: 0.0.0.0 without security.authorization: enabled and TLS.
5. Setting Up Storage Options
| Option | Default | Description |
| storage.dbPath | /data/db | Data directory |
| storage.engine | wiredTiger | Storage engine (inMemory in Enterprise) |
| storage.directoryPerDB | false | Separate subdir per database |
| wiredTiger.engineConfig.cacheSizeGB | 50% RAM-1GB | Internal cache size |
| wiredTiger.collectionConfig.blockCompressor | snappy | none, snappy, zlib, zstd |
| wiredTiger.indexConfig.prefixCompression | true | Prefix-compress indexes |
6. Configuring Logging Options
| Setting | Values | Purpose |
| systemLog.destination | file | syslog | Log target |
| systemLog.path | file path | Log file location |
| systemLog.logAppend | true | Append vs overwrite on restart |
| systemLog.logRotate | rename | reopen | Rotation strategy |
| systemLog.verbosity | 0-5 | Global verbosity |
| systemLog.component.*.verbosity | 0-5 | Per-component (query, replication, storage) |
| systemLog.quiet | true | Suppress connection accept events |
7. Setting Up Security Options
| Setting | Values | Purpose |
| security.authorization | enabled | disabled | Require auth for all clients |
| security.keyFile | file path | Internal auth for replica/sharded members |
| security.clusterAuthMode | keyFile | x509 | Inter-node auth mechanism |
| security.javascriptEnabled | false | Disable server-side JS ($where, mapReduce) |
| setParameter.authenticationMechanisms | SCRAM-SHA-256 | Allowed mechanisms |
8. Using mongod Command-Line Options
| Flag | Equivalent YAML | Description |
| --config / -f | - | Path to config file |
| --dbpath | storage.dbPath | Data directory |
| --port | net.port | Listen port |
| --bind_ip | net.bindIp | Bind addresses |
| --auth | security.authorization: enabled | Enable auth |
| --replSet | replication.replSetName | Replica set name |
| --fork | processManagement.fork | Run as daemon |
| --logpath | systemLog.path | Log file |
| --shutdown | - | Clean shutdown |
9. Connecting with mongosh Shell
| Command | Description |
mongosh | Connect to localhost:27017 |
mongosh "mongodb://host:27017/db" | Connection string |
mongosh -u user -p --authenticationDatabase admin | Authenticated |
mongosh --tls --tlsCAFile ca.pem | TLS connection |
mongosh --eval "db.stats()" | One-shot command |
mongosh script.js | Run JS file |
10. Setting Up Environment Variables
| Variable | Purpose |
MONGODB_URI | Convention for app connection string |
MONGO_INITDB_ROOT_USERNAME | Docker root user |
MONGO_INITDB_ROOT_PASSWORD | Docker root password |
MONGO_INITDB_DATABASE | Initial DB created in Docker |
PATH | Append /usr/local/opt/mongodb-community@7.0/bin |
11. Configuring Memory Settings
| Setting | Recommendation | Notes |
| wiredTiger cacheSizeGB | 50% of (RAM - 1GB) | Default; leave RAM for OS file cache |
| ulimit -n | ≥ 64000 | File descriptors |
| ulimit -u | ≥ 64000 | Max processes/threads |
| vm.swappiness | 1 | Avoid swapping |
| Transparent Huge Pages | disabled | Causes latency spikes |
| NUMA | numactl --interleave=all mongod | Avoid single-node memory binding |
12. Running MongoDB as a Service
| OS | Command |
| systemd (Linux) | systemctl enable --now mongod |
| systemd status | systemctl status mongod |
| macOS (brew) | brew services start mongodb-community@7.0 |
| Windows | net start MongoDB |
| Logs (journald) | journalctl -u mongod -f |