Installing and Configuring MongoDB

1. Installing MongoDB Community Server

PlatformCommandNotes
macOS (Homebrew)brew tap mongodb/brew && brew install mongodb-community@7.0Installs MongoDB 7.0 LTS
Ubuntu/Debianapt-get install -y mongodb-orgRequires adding MongoDB GPG key & repo
RHEL/CentOSyum install -y mongodb-orgConfigure /etc/yum.repos.d/mongodb-org-7.0.repo first
Windowsmsiexec /i mongodb-windows-x86_64-7.0.msiOr use the MSI installer GUI
Dockerdocker run -d -p 27017:27017 --name mongo mongo:7.0Official 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

FeatureCommunityEnterprise
LicenseSSPLCommercial
In-Memory EngineNoYes
LDAP / Kerberos authNoYes
Encryption at RestNoYes
AuditingNoYes
SupportCommunityMongoDB 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

SectionPurposeKey Settings
storageData path & enginedbPath, engine, wiredTiger
systemLogLoggingdestination, path, logAppend
netNetwork bindingsport, bindIp, tls
securityAuth & keyfileauthorization, keyFile
replicationReplica set configreplSetName, oplogSizeMB
shardingCluster roleclusterRole
processManagementDaemon optionsfork, 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

SettingValueEffect
bindIp127.0.0.1Localhost only (default, safest)
bindIp0.0.0.0All interfaces (requires firewall)
bindIp10.0.0.5,127.0.0.1Specific interfaces (comma-separated)
bindIpAlltrueEquivalent to 0.0.0.0
port27017Default mongod; 27018 (shard), 27019 (configsvr)
maxIncomingConnections65536Max concurrent client connections
Warning: Never expose bindIp: 0.0.0.0 without security.authorization: enabled and TLS.

5. Setting Up Storage Options

OptionDefaultDescription
storage.dbPath/data/dbData directory
storage.enginewiredTigerStorage engine (inMemory in Enterprise)
storage.directoryPerDBfalseSeparate subdir per database
wiredTiger.engineConfig.cacheSizeGB50% RAM-1GBInternal cache size
wiredTiger.collectionConfig.blockCompressorsnappynone, snappy, zlib, zstd
wiredTiger.indexConfig.prefixCompressiontruePrefix-compress indexes

6. Configuring Logging Options

SettingValuesPurpose
systemLog.destinationfile | syslogLog target
systemLog.pathfile pathLog file location
systemLog.logAppendtrueAppend vs overwrite on restart
systemLog.logRotaterename | reopenRotation strategy
systemLog.verbosity0-5Global verbosity
systemLog.component.*.verbosity0-5Per-component (query, replication, storage)
systemLog.quiettrueSuppress connection accept events

7. Setting Up Security Options

SettingValuesPurpose
security.authorizationenabled | disabledRequire auth for all clients
security.keyFilefile pathInternal auth for replica/sharded members
security.clusterAuthModekeyFile | x509Inter-node auth mechanism
security.javascriptEnabledfalseDisable server-side JS ($where, mapReduce)
setParameter.authenticationMechanismsSCRAM-SHA-256Allowed mechanisms

8. Using mongod Command-Line Options

FlagEquivalent YAMLDescription
--config / -f-Path to config file
--dbpathstorage.dbPathData directory
--portnet.portListen port
--bind_ipnet.bindIpBind addresses
--authsecurity.authorization: enabledEnable auth
--replSetreplication.replSetNameReplica set name
--forkprocessManagement.forkRun as daemon
--logpathsystemLog.pathLog file
--shutdown-Clean shutdown

9. Connecting with mongosh Shell

CommandDescription
mongoshConnect to localhost:27017
mongosh "mongodb://host:27017/db"Connection string
mongosh -u user -p --authenticationDatabase adminAuthenticated
mongosh --tls --tlsCAFile ca.pemTLS connection
mongosh --eval "db.stats()"One-shot command
mongosh script.jsRun JS file

10. Setting Up Environment Variables

VariablePurpose
MONGODB_URIConvention for app connection string
MONGO_INITDB_ROOT_USERNAMEDocker root user
MONGO_INITDB_ROOT_PASSWORDDocker root password
MONGO_INITDB_DATABASEInitial DB created in Docker
PATHAppend /usr/local/opt/mongodb-community@7.0/bin

11. Configuring Memory Settings

SettingRecommendationNotes
wiredTiger cacheSizeGB50% of (RAM - 1GB)Default; leave RAM for OS file cache
ulimit -n≥ 64000File descriptors
ulimit -u≥ 64000Max processes/threads
vm.swappiness1Avoid swapping
Transparent Huge PagesdisabledCauses latency spikes
NUMAnumactl --interleave=all mongodAvoid single-node memory binding

12. Running MongoDB as a Service

OSCommand
systemd (Linux)systemctl enable --now mongod
systemd statussystemctl status mongod
macOS (brew)brew services start mongodb-community@7.0
Windowsnet start MongoDB
Logs (journald)journalctl -u mongod -f