Linux Server setup

Install MongoDB

In this chapter, we will install and set up the NoSQL database MongoDB.

The community version is free and probably sufficient for most projects.


Install MongoDB

We install MongoDB 8.0 Community Edition for Ubuntu 24.04 LTS in several steps. Before using the commands later, check the official installation guide to see whether this version is still supported.


__$ sudo apt-get update
__$ sudo apt-get install -y gnupg curl
__$ curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
__$ echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
__$ sudo apt-get update
__$ sudo apt-get install -y mongodb-org
 

Let's open the main configuration file of MongoDB with less.


__$ sudo less /etc/mongod.conf

/etc/mongod.conf


# 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:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


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

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

In the output we can see:

  • Data: /var/lib/mongodb
  • Log file: /var/log/mongodb/mongod.log
  • Address: 127.0.0.1
  • Port: 27017

We can retrieve the version with --version:


__$ mongosh --version


Uninstall MongoDB

How to uninstall MongoDB is described here only for the sake of completeness.

Stop MongoDB:


__$ sudo systemctl stop mongod
 

Uninstall MongoDB:


__$ sudo apt-get purge mongodb-org*
__$ sudo apt-get autoremove
 

Delete MongoDB data and logs:


__$ sudo rm -r /var/lib/mongodb
__$ sudo rm -r /var/log/mongodb
 

Common MongoDB commands

Launch MongoDB:


__$ sudo systemctl start mongod

Stop MongoDB:


__$ sudo systemctl stop mongod

Restart MongoDB:


__$ sudo systemctl restart mongod

MongoDB status:


__$ sudo systemctl status mongod

MongoDB Shell via command line

Start the current MongoDB shell:


__$ mongosh

The MongoDB Shell:


Current Mongosh Log ID: ...
Connecting to: mongodb://127.0.0.1:27017/
Using MongoDB: 8.0.x
Using Mongosh: 2.x
test>

Show all databases:


__gt show databases;

Switch to the admin database:


__gt use admin;

Create a new user named admin with all privileges:


__gt db.createUser({
  user: "admin",
  pwd: "MyPassword",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
});

Create a new database:


__gt use MyDatabase;

A newly created database will not be displayed by show databases; as long as it is empty!

Insert a collection with a document:


__gt db.MyCollection.insert({ key: "val" });

List Collections:


__gt show collections;

List the contents of a collection:


__gt db.MyCollection.find().pretty();

pretty() is an addition to make the output more structured and readable.

Insert another document:


__gt db.MyCollection.insert({ key: "val2" });

Delete single document:


__gt db.MyCollection.deleteOne({ key: "val2" });

Output statistics of the current database:


__gt db.stats();

Delete Collection:


__gt db.MyCollection.drop();

Delete current database:


__gt db.dropDatabase();

Exit the MongoDB shell:


__gt exit