How to Install MongoDB in Linux

MongoDB is a free, cross-platform, open source, NoSQL document-oriented database. It supports GNU/Linux, Microsoft Windows, Solaris, and Mac OS X. MongoDB is available as two editions, Community and Enterprise edition. MongoDB community edition is free for personal usage, whereas enterprise edition is paid version that have more features and support than the community edition.

In this brief tutorial, we will see how to install MongoDB community edition in Linux.

Install MongoDB community edition in Linux

In RHEL / CentOS / Scientific Linux:

Set SELinux to permissive mode or disable it completely. To do so, edit /etc/selinux/config file:

sudo vi /etc/selinux/config

and set the SELINUX value as permissive or disabled.

SELINUX=permissive

Reboot the system to take effect the changes.

Add MongoDB repository and install it using yum.

To do so, create /etc/yum.repos.d/mongodb.repo file:

sudo vi /etc/yum.repos.d/mongodb.repo

Add the following lines:

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

Save and close the file. Then, update the repository list with command:

sudo yum update

Finally, install MongoDB using command:

sudo yum install mongodb-org

Start the MongoDB service using command:

sudo systemctl start mongod

To verify the service is started, run:

sudo systemctl status mongod

Similarly, to restart or stop the service, run:

sudo systemctl restart mongod
sudo systemctl stop mongod

To start MongoDB service on every reboot, run:

sudo systemctl enable mongod

In Debian 7 wheezy:

MongoDB latest version is available only for Debian 7 64 bit system.

First, Import MongoDB public key:

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

Create a new file called /etc/apt/sources.list.d/mongodb.list:

sudo vi /etc/apt/sources.list.d/mongodb.list

Add the following lines:

deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main

Update the repository list:

sudo apt-get update

Finally, install it using command:

sudo apt-get install mongodb-org

Start MongoDB service using command:

sudo service mongod start

Similarly, restart/stop the service with command:

sudo service mongod restart
sudo service mongod stop

In Ubuntu and derivatives:

Add MongoDB repository as shown below.

Create a repository file with command:

sudo vi /etc/apt/sources.list.d/mongodb.list

Then, add the MongoDB repository URL in it as mentioned below.

For Ubuntu 14.04:

deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse

For Ubuntu 16.04:

deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse

Then, Import the MongoDB public key.

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

Update the repository lists using command:

sudo apt-get update

Finally, install MongoDB using the following command:

sudo apt-get install mongodb-org

For Ubuntu 16.04 LTS versions, you need to create the service file.

To do so, create a new file called /lib/systemd/system/mongod.service:

sudo vi /lib/systemd/system/mongod.service

Add the following lines:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Save and close the file. This is only for Ubuntu 16.04 LTS versions, you don’t need to do it on Ubuntu 14.04 LTS.

Now, start the MongoDB service with command:

sudo systemctl start mongod

Similarly, to restart/stop this service, run:

sudo systemctl restart mongod
sudo systemctl stop mongod

Run this command to start this service automatically on every reboot.

sudo systemctl enable mongod

On openSUSE:

Add MongoDB using command:

sudo zypper addrepo --no-gpgcheck https://repo.mongodb.org/zypper/suse/$(sed -rn 's/VERSION=.*([0-9]{2}).*/\1/p' /etc/os-release)/mongodb-org/3.2/x86_64/ mongodb

Then, run the following command to install MongoDB:

sudo zypper install mongodb-org

Start the MongoDB service command with command:

sudo service mongod start

To restart the service, run:

sudo service mongod restart

To stop it:

sudo service mongod stop

Enable the service on every reboot, run:

sudo chkconfig mongod on

After installing MongoDB, run the following command to log in to mongo shell.

mongo

To exit from the shell, run:

exit

Check the official MongoDB getting started guide for in-depth MongoDB usage.

Uninstall MongoDB

To completely remove MongoDB from your system, do the following.

First, stop the service using command:

sudo systemctl stop mongod

Or,

sudo service mongod stop

Next, remove the mongodb packages using command:

On RHEL / CentOS:

sudo yum erase $(rpm -qa | grep mongodb-org)

On Debian:

sudo apt-get purge mongodb-org*

On openSUSE:

sudo zypper remove $(rpm -qa | grep mongodb-org)

Finally, delete database and log files.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo

That’s it. I hope you now know how to install MongoDB in different Linux flavors, and how to remove MongoDB from your system if you don’t need it anymore.

If you like this guide, please share it on your social networks and support us.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • MightyMoo

    Mongo only pawn… in game of life.