π§© MongoDB Wiki
π Index
- π Overview
- π§± Core Concepts
- π§© Architecture Components
- βοΈ Installation
- βοΈ Configuration
- π§ Basic Commands
- π§© Replica Set Setup
- πͺ£ Backup & Restore
- πͺΆ MongoDB in Graylog Context
- π§° Useful Admin Commands
- π‘οΈ Security Recommendations
- π Performance Tuning
- π§Ύ Logs and Monitoring
- π References
π Overview
MongoDB is an open-source NoSQL database designed for speed, scalability, and flexibility. It uses BSON (Binary JSON) to store structured and unstructured data.
Use Cases: Logging, analytics, IoT, and cloud-native applications.
π§± Core Concepts
- Database β Container for collections.
- Collection β Similar to a table in SQL.
- Document β JSON-like record (keyβvalue pairs).
- Field β Keyβvalue inside a document.
- BSON β Efficient binary format used internally.
π§© Architecture Components
- mongod β Core database daemon.
- mongos β Query router for sharding.
- mongosh β CLI shell for MongoDB.
- Replica Set β Provides redundancy and failover.
- Shard β Enables horizontal scaling.
βοΈ Installation
Ubuntu (VM or Container)
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongod
βοΈ Configuration
File: /etc/mongod.conf
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
replication:
replSetName: rs0
Restart MongoDB service:
sudo systemctl restart mongod
π§ Basic Commands
show dbsβ List all databasesuse testdbβ Switch/create DBshow collectionsβ List collectionsdb.createCollection("users")β Create collectiondb.users.insertOne({...})β Insert documentdb.users.find()β Query alldb.users.updateOne()β Update documentdb.users.deleteOne()β Delete documentdb.dropDatabase()β Drop DB
π§© Replica Set Setup
1οΈβ£ Initiate on Primary Node
mongo
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27017" },
{ _id: 2, host: "mongo3:27017" }
]
})
2οΈβ£ Check Status
rs.status()
rs.isMaster()
πͺ£ Backup & Restore
- Backup:
mongodump --db testdb --out /backup/ - Restore:
mongorestore --db testdb /backup/testdb/
πͺΆ MongoDB in Graylog Context
- MongoDB β Stores Graylog configuration and user data.
- OpenSearch β Stores log messages.
- Graylog Server β Processes and queries data.
Tip: host MongoDB on a separate VM to isolate from OpenSearch for better performance and I/O.
π§° Useful Admin Commands
db.serverStatus()β System statsdb.stats()β Database statsdb.collection.stats()β Collection statsdb.currentOp()β Show running operationsdb.killOp(opid)β Kill a running operation
π‘οΈ Security Recommendations
- Enable Authentication:
security: authorization: enabled - Create admin user:
use admin db.createUser({ user: "admin", pwd: "SecurePass", roles: ["root"] }) - Use TLS, restrict bind IPs, and rotate credentials regularly.
π Performance Tuning
- Indexing: Index frequently queried fields.
- Storage: Prefer SSD or NVMe.
- RAM: Ensure RAM >= working set size.
- WiredTiger Cache: Default ~50% of RAM.
- Connection Pooling: Tune client pool sizes.
π§Ύ Logs and Monitoring
/var/log/mongodb/mongod.logβ Main log filemongostatβ Realtime DB statsmongotopβ Collection-level usagedb.getProfilingStatus()β Query profiling info
π References
- https://www.mongodb.com/docs/
- https://www.mongodb.com/docs/manual/administration/install-on-linux/
- https://go2docs.graylog.org/