/mongodb-tutorial

Primary LanguageShellMIT LicenseMIT

MongoDB tutorial

Follow lessons from MongoDB Manual after installation.

Installation with Vagrant

MongoDB 6.0 Community Edition installation on Ubuntu 22 is implemented in script provision/provision.sh in function install_mongodb(). Please use Vagrant to start virtual machine with installed MongoDB instance:

cd vagrant/
vagrant up

You will get running virtual machine with IP address specified in vagrant/Vagrantfile. Add this IP on your host system to file /etc/hosts:

192.168.56.30 mongodb.vm

Login to the guest's shell:

vagrant ssh

Open MongoDB Shell:

mongosh

Validate:

db.runCommand(
   {
      hello: 1
   }
)

Installation with Docker

Pull the image:

docker pull mongodb/mongodb-community-server

Run image as container:

docker run --name mongo -d mongodb/mongodb-community-server:latest

Check if it's running:

docker container ls

Open shell:

docker exec -it mongo mongosh

Validate:

db.runCommand(
   {
      hello: 1
   }
)

Installed instance

The most important details about running instance of MongoDB:

  • Linux user: mongodb
  • Data directory: /var/lib/mongodb
  • Logs directory: /var/log/mongodb
  • Config file: /etc/mongod.conf
  • Process name: mongod
  • Default port: 27017
  • CLI client: mongosh [documentation]

Check your built-in init system:

ps --no-headers -o comm 1

Result may be one of them:

  • systemd - systemd systemctl
  • init - System V Init service

Commands for systemd without description:

sudo systemctl start mongod
sudo systemctl status mongod
sudo systemctl enable mongod
sudo systemctl stop mongod
sudo systemctl restart mongod

In case of Failed to start mongod.service: Unit mongod.service not found do:

sudo systemctl daemon-reload

Commands for System V Init without description:

sudo service mongod start
sudo service mongod status
sudo service mongod stop
sudo service mongod restart

Track logs:

sudo tail -f /var/log/mongodb/mongod.log

IP binding

Exposing the service to your host machine can be done by changing option bindIp in your config file /etc/mongod.conf:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

where:

  • 127.0.0.1 is set by default after installation
  • 0.0.0.0 (all IPv4 addresses) is set by provision/provision.sh script

Later restart is required. There is no authentication by default.

Executables

Metapackage mongodb-org delivers following components:

  • mongod - database daemon
  • mongos - daemon interface between the client and the sharded cluster
  • mongosh - MongoDB Shell
  • mongodump - creates binary export of a database's contents
  • mongorestore - opposite to mongodump
  • bsondump - converts BSON files into human-readable formats
  • mongoimport - imports the content created by mongoexport
  • mongoexport - creates JSON or CSV from data in MongoDB
  • mongostat - quick overview for mongod or mongos
  • mongotop - tracks amount of time for reading and writing data
  • mongofiles - manipulates files stored in GridFS objects for storing big files exceeding the BSON-document size limit of 16MB

Removing MongoDB

sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb