This page provides a practical baseline setup for MongoDB on Linux hosts. Covers installation methods for different distributions and considerations for production environments.
Before installing MongoDB, ensure:
First, add the official MongoDB repository:
# Import the public key used by the package management system
wget -qO - https://www.mongodb.org/static/pgp/server-8.0.asc | sudo apt-key add -
# Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
# Update package database
sudo apt update
# Install MongoDB packages
sudo apt install -y mongodb-org
# Create repository file
sudo tee /etc/yum.repos.d/mongodb-org-8.0.repo << EOF
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-8.0.asc
EOF
# Install MongoDB
sudo dnf install -y mongodb-org
sudo apt update
sudo apt install -y mongodb
sudo dnf install -y mongodb-server
⚠️ Warning: Distribution packages may be outdated. Use official MongoDB repositories for production.
# Enable MongoDB to start at boot
sudo systemctl enable mongod
# Start MongoDB service
sudo systemctl start mongod
# Check service status
sudo systemctl status mongod
# Check MongoDB version
mongosh --version
# Connect to MongoDB instance
mongosh
# Test basic connectivity
mongosh --eval "db.adminCommand('ping')"
After first installation, configure basic security:
# Connect to MongoDB
mongosh
# Switch to admin database
use admin
# Create administrative user
db.createUser({
user: "admin",
pwd: passwordPrompt(), // Will prompt for password
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" }
]
})
Review and customize the default configuration in /etc/mongod.conf:
# Basic configuration template
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
# Security settings (enable after creating users)
# security:
# authorization: enabled
sudo systemctl restart mongod
sudo systemctl status mongod
# Check logs
sudo tail -f /var/log/mongodb/mongod.log
# Check if port is already in use
sudo netstat -tlnp | grep :27017
# Check disk space
df -h /var/lib/mongo
# Ensure MongoDB user owns data directory
sudo chown -R mongod:mongod /var/lib/mongo
sudo chown -R mongod:mongod /var/log/mongodb