Managing MongoDB on a Linux VPS involves installation, configuration, security, and performance optimization.

1. Installing MongoDB on Linux VPS

MongoDB can be installed using package managers like apt (for Debian/Ubuntu) or yum (for CentOS/RHEL).

For Ubuntu/Debian

  1. Update package list:
    sudo apt update 
    sudo apt upgrade -y
  2. Import MongoDB GPG key:
    curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-keyring.gpg

    **Run the following command to add the missing public key:

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B00A0BD1E2C63C11
    or

    **Run this command to manually import the key:

    wget -qO - https://pgp.mongodb.com/server-5.0.asc | sudo tee /etc/apt/trusted.gpg.d/mongodb-server-5.0.

    Check if the key was added successfully:

    gpg --dry-run --quiet --import --import-options import-show /etc/apt/trusted.gpg.d/mongodb-server
  3. Add MongoDB repository:
    Ubuntu:
    echo "deb [signed-by=/usr/share/keyrings/mongodb-server-keyring.gpg] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
    Debian:
    echo "deb [signed-by=/etc/apt/trusted.gpg.d/mongodb-server-5.0.asc] https://repo.mongodb.org/apt/debian bullseye/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  4. Install MongoDB:
    sudo apt update
    sudo apt install -y mongodb-org
  5. Start and Enable MongoDB:
    sudo systemctl start mongod
    sudo systemctl enable mongod

For CentOS/RHEL

  1. Add MongoDB repository:
    sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<EOF
    [mongodb-org-6.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
    EOF

  2. Install MongoDB:
    sudo yum install -y mongodb-org
  3. Start and Enable MongoDB:
    sudo systemctl start mongod
    sudo systemctl enable mongod

2. Securing MongoDB

By default, MongoDB listens on localhost (127.0.0.1). To secure it:

Enable Authentication

  1. Create an admin user:
    mongosh
    use admin
    db.createUser({
    user: "admin",
    pwd: "StrongPassword",
    roles: [{ role: "root", db: "admin" }]
    })
  2. Enable authentication in MongoDB config file:
    sudo nano /etc/mongod.conf

    Find the security section and add:

    security:
    authorization: enabled
  3. Restart MongoDB:
    sudo systemctl restart mongod

Restrict External Access

  1. Modify MongoDB to listen only to localhost:
    sudo nano /etc/mongod.conf

    Change:

    bindIp: 127.0.0.1
  2. Use UFW (Ubuntu) or FirewallD (CentOS) to allow only specific IPs:
    sudo ufw allow from YOUR_IP to any port 27017

3. Managing MongoDB

Check MongoDB Service Status

sudo systemctl status mongod

Restart MongoDB

sudo systemctl restart mongod

Stop MongoDB

sudo systemctl stop mongod

Enable MongoDB to Start on Boot

sudo systemctl enable mongod

4. Managing Databases in MongoDB

Connect to MongoDB

mongosh

Create a Database

use mydatabase

Show Databases

show dbs

Create a Collection

db.createCollection("users")

Insert Data

db.users.insertOne({ name: "John Doe", age: 30, email: "john@example.com" })

Find Data

db.users.find()

Delete a Database

use mydatabase
db.dropDatabase()

5. Backups and Restores

Backup MongoDB Database

mongodump --db=mydatabase --out=/backup/

Restore MongoDB Database

mongorestore --db=mydatabase /backup/mydatabase/

6. Performance Optimization

Indexing for Faster Queries

db.users.createIndex({ email: 1 })

Monitoring Performance

db.serverStatus()

Limit Memory Usage

Modify wiredTigerCacheSizeGB in /etc/mongod.conf:

storage:
wiredTiger:
engineConfig:
cacheSizeGB: 1

Use Connection Pooling

Modify /etc/mongod.conf:

net:
maxIncomingConnections: 1000

7. Logs and Debugging

View MongoDB Logs

sudo journalctl -u mongod --no-pager | tail -n 50

Check Error Logs

sudo cat /var/log/mongodb/mongod.log

8. Uninstall MongoDB (If Needed)

For Ubuntu/Debian

sudo systemctl stop mongod
sudo apt purge -y mongodb-org*
sudo rm -r /var/log/mongodb /var/lib/mongodb

For CentOS/RHEL

sudo systemctl stop mongod
sudo yum remove -y mongodb-org*
sudo rm -r /var/log/mongodb /var/lib/mongodb

Conclusion

Managing MongoDB on a Linux VPS requires proper installation, security hardening, and performance optimization. By following these steps, you ensure your MongoDB database runs efficiently and securely. 🚀

Would you like help with automation scripts for managing MongoDB? 😊