Skip to content

5 Mongodb Database

Tomato6966 edited this page Nov 25, 2022 · 2 revisions

This is a SELF HOSTING GUIDE for MONGODB

Click here for the Official Link

Mongodb self hosting cheat sheet for ubuntu 18.04

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update -y # updated packages
sudo apt-get install -y mongodb-org=4.4.8 mongodb-org-server=4.4.8 mongodb-org-shell=4.4.8 mongodb-org-mongos=4.4.8 mongodb-org-tools=4.4.8

If you're on ubuntu 20. swap the echo "deb... Command with this: echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

if you encounter any error while starting,

Make sure to maybe adjust the port 27017 and the path file name

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod restart # Restart mongodb

Access start/stop etc. the mongodb

systemctl start mongod # Start mongod via systemd
systemctl restart mongod # Restart mongod via systemd
systemctl stop mongod # Stop mongod via systemd
systemctl status mongod # Show the status of mongod via systemd

service mongod start # Start mongod via service (should always work)
service mongod stop # Stop mongod via service (should always work)

Add a user to the db with root access:

make sure that mongodb is running

> mongo
> use admin
> db.createUser(
  {
    user: "useradmin",
    pwd: "thepianohasbeendrinking",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Example /etc/mongod.conf File

Make sure to enable security.authorization AFTER You added a user (restart the db)

 mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  engine: "wiredTiger"
  wiredTiger:
    engineConfig:
      cacheSizeGB: 8 # Size of RAM CACHE (Default: 0.5*(50% - 1))
      journalCompressor: "zlib" # Default "snappy" | Options: "none", "snappy", "zlib", "zstd"
      directoryForIndexes: false # Default "false" | Options: true, false
    collectionConfig:
      blockCompressor: "zlib"
    indexConfig:
      prefixCompression: 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

# how the process runs
processManagement:
  fork: false
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: "enabled"
Clone this wiki locally