Install MongoDB Community Edition on Ubuntu

Latest Page of MongoDB Community Edition:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Install MongoDB Community Edition on Ubuntu 15

1) Import the public key used by the package management system.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

2) Create a list file for MongoDB.

echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

3) Reload local package database.

sudo apt-get update

4) Install the latest stable version of MongoDB.

sudo apt-get install -y mongodb-org

systemd script file of mongodb

sudo mkdir /lib/systemd/system

sudo vim /lib/systemd/system/mongodb.service

[Unit]
Description=High-performance, schema-free document-oriented database
Documentation=man:mongod(1)
After=network.target

[Service]
Type=forking
User=mongodb
Group=mongodb
RuntimeDirectory=mongod
PIDFile=/var/run/mongod/mongod.pid
ExecStart=/usr/bin/mongod -f /etc/mongod.conf --pidfilepath /var/run/mongod/mongod.pid --fork
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

sudo systemctl enable mongodb.service
sudo service mongodb start
sudo systemctl status mongod.service

If this gives some error with respect to starting the database then check if the file /var/lib/mongodb/mongod.lock exists. If it does, remove it and execute mongod --repair and then again run sudo service mongodb start

Leave a Reply