- Prerequisites
- Installing MongoDB
- Configuring MongoDB
- Securing MongoDB
- Starting and Managing MongoDB
- Setting Up Users and Authentication
- Configuring Firewall
- Initializing Replica Set
- Restoring Data
- Troubleshooting
- A Ubuntu server (this guide uses Ubuntu 22.04)
- Root or sudo access to the server
- Basic knowledge of terminal commands
-
Update the package list:
sudo apt-get update
-
Install required packages:
sudo apt-get install -y gnupg
-
Import the MongoDB public GPG key:
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
-
Create a list file for MongoDB:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
-
Update the package list again:
sudo apt-get update
-
Install MongoDB:
sudo apt-get install -y mongodb-org
-
Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
-
Here's a detailed
mongod.conf
file with explanations:# mongod.conf # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # Where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # Network interfaces net: port: 27017 bindIp: 0.0.0.0 # Allow connections from any IP # How the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo # Security security: authorization: enabled keyFile: /etc/mongodb/keyfile # For replica set authentication # Replication replication: replSetName: "myReplicaSet"
Explanation of key settings:
storage.dbPath
: Where MongoDB stores its data filessystemLog
: Logging configurationnet.bindIp
:0.0.0.0
allows connections from any IPsecurity.authorization
: Enables access controlsecurity.keyFile
: Used for internal authentication in a replica setreplication.replSetName
: Name of the replica set
-
Save and exit the editor (in nano, press Ctrl+X, then Y, then Enter)
- Create a keyfile for replica set authentication:
sudo mkdir -p /etc/mongodb sudo openssl rand -base64 756 > /etc/mongodb/keyfile sudo chmod 400 /etc/mongodb/keyfile sudo chown mongodb:mongodb /etc/mongodb/keyfile
-
Start MongoDB:
sudo systemctl start mongod
-
Enable MongoDB to start on boot:
sudo systemctl enable mongod
-
Check MongoDB status:
sudo systemctl status mongod
-
Connect to MongoDB:
mongosh
-
Create an admin user:
use admin db.createUser( { user: "adminUser", pwd: "securePassword", // Change this! roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )
-
Create a user for a specific database:
use test db.createUser( { user: "dpac", pwd: "securePassword", // Change this! roles: [ { role: "readWrite", db: "test" } ] } )
-
Allow MongoDB port through UFW:
sudo ufw allow from YOUR_IP_ADDRESS to any port 27017
Replace
YOUR_IP_ADDRESS
with the IP address you want to allow. -
Enable UFW if not already enabled:
sudo ufw enable
-
Check UFW status:
sudo ufw status
-
Connect to MongoDB:
mongosh
-
Initiate the replica set:
rs.initiate({ _id: "myReplicaSet", members: [ { _id: 0, host: "YOUR_SERVER_IP:27017" } ] })
Replace
YOUR_SERVER_IP
with your server's actual IP address.
- Restore data from a backup:
Replace
mongorestore --uri "mongodb://dpac:[email protected]:27017/test" --db test /path/to/backup/test/
securePassword
with the actual password for thedpac
user.
-
If MongoDB fails to start, check the logs:
sudo tail -n 100 /var/log/mongodb/mongod.log
-
If you encounter permission issues:
sudo chown -R mongodb:mongodb /var/lib/mongodb sudo chown -R mongodb:mongodb /var/log/mongodb
-
If you need to remove and reinstall MongoDB:
sudo apt-get purge mongodb-org* sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb
Then follow the installation steps again.
-
If you're having connection issues, ensure the bindIp in your mongod.conf is set correctly and that your firewall rules allow the connection.
Remember to replace placeholder passwords with strong, unique passwords in a production environment. Always follow best practices for security when setting up a database server.
This guide should help you set up a self-hosted MongoDB instance. Always refer to the official MongoDB documentation for the most up-to-date and comprehensive information.
Claude Chat Ref: https://claude.ai/chat/a44de017-e5c6-4383-b1ce-98e99102b045