In a broad way, there are two kinds of Linux one is RPM based and another one is deb based. In terms of functionality, there is no difference in both of these Linux. We will see how to install MongoDB in both of these kinds of Linux.
The steps to install MongoDB illustrated below uses CentOS and Ubuntu variant of Linux. Steps of CentOS can be used on Red Hat Enterprise Linux, Fedora, etc. The install process show for Ubuntu can be used on Debian or its variants.
For installing MongoDB in Linux, we need to have installation repository for Linux environment on which we are going to install. MongoDB maintains one parent package mongodb-org, on the installation of this package in Linux, it will install all the tools required to work with MongoDB. The tools are:
Let’s start the installation on CentOS first. To install MongoDB on CentOS, you need to create on yum repo file so that yum installer can invoke all the dependencies required to install MongoDB.
This repo file must be created at /etc/yum.repos.d/. You are free to choose any for the file but make sure that extension should be repo. Here is the content of the file.
[mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
Change the SELinux to disabled or permissive. If you need to keep SELinux status to enforcing, configure it to access for MongoDB.
Once you have created the file, just clean the yum cache and proceed with the installation process.
yum install mongodb-org -y
This command will install all the four component list above. Start the mongod service and test it by running the client.
service mongod start
Connect MongoDB shell with the following command
If each step executed properly, you will get mongo shell prompt.
Now let’s start installation in Ubuntu. To install it you MongoDB recommends you to import a public GPG key which it maintains for the integrity of packages.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Create list file to define the repository from where Ubuntu needs to download package required for installation of MongoDB. Since we are using Ubuntu 16.04 LTS version, the following list file is required. If you have any other version of Ubuntu, get the appropriate list file from MongoDB documentation.
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
If you are using for a quite a while, you might be aware that you need to reload the local package database.
sudo apt-get update
To install latest available version of MongoDB for Ubuntu run
sudo apt-get install mongodb-org -y
This command will install all the components and tools of MongoDB. After this, like CentOS you need to start the service and connect it via MongoDB client
sudo service mongod start mongo
Now you can install MongoDB which includes mongod (server) mongo (mongodb shell) and other mongodb tools in each Linux flavor.