Setup MongoDB


Setting up a MongoDB is very simple. The MongoDB documentation provides very simple and clear instructions for it.

To simplify it further, you can follow the steps below, depending upon the type of installation you want - Windows, Ubuntu, Amazon Linux, and also in the cloud.

Ubuntu


Ubuntu is a very popular operating systems among developers. If you want to learn, I would highly recommend that you switch over to Ubuntu - at least have one installed in a virtual machine.

On your Ubuntu, you can just create a small script installMongoDB.sh, and add the below code into it.

#!/bin/sh

apt-get install gnupg -y
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list

apt-get update
apt-get install -y mongodb-org

echo "mongodb-org hold" | dpkg --set-selections
echo "mongodb-org-server hold" | dpkg --set-selections
echo "mongodb-org-shell hold" | dpkg --set-selections
echo "mongodb-org-mongos hold" | dpkg --set-selections
echo "mongodb-org-tools hold" | dpkg --set-selections

systemctl start mongod
systemctl enable mongod

Save it, and change the permissions to make it executable.

chmod +x installMongoDB.sh

And finally, run the script with a sudo.

sudo ./installMongoDB.sh

This will prompt you for a password. With the password, it should be able to finish that task by itself. The MongoDB should be installed on your machine.

Amazon Linux


If you are in the Amazon cloud, it does not make any sense to install the MongoDB on an EC2 instance. You can just get a managed MongoDB server without any effort. But, if you are running Amazon Linux on a local server, you can follow these steps.

Again create a script installMongoDB.sh - with the below code.

#!/bin/sh

cat > /etc/yum.repos.d/mongodb-org-4.4.repo << END
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
END

yum install -y mongodb-org

sudo systemctl daemon-reload
sudo systemctl enable mongod
sudo systemctl start mongod

Save it, and change the permissions to make it executable.

chmod +x installMongoDB.sh

And finally, run the script with a sudo.

sudo ./installMongoDB.sh

This will prompt you for a password. With the password, it should be able to finish that task by itself. The MongoDB should be installed on your machine.

Amazon Linux comes pre-configured low limits for the OS. If you have a problem running the above script, you can raise the limits as described below: ulimit settings

Windows


Installing on Windows is the pretty simple. Just download the installer MSI file from the downloads page and run it. You can safely select all the defaults, and your MongoDB is ready for you.

On Windows, the MSI does all that is required to setup the database. You can directly connect to it once it is installed.

Altas Cloud


This is the right way that I would recommend for learning MongoDB. We cannot get the feel of working on a remote database, unless it is really remote! Rarely, in an application, would you see the database and application running side by side on the same machine. A lot of aspects of the development are missed when we install MongoDB locally.

There are several cloud offerings for MongoDB. AWS, GCP, Azure.. all provide a good deal for a MongoDB database on cloud. But the best of all is the Altas Cloud - that is offered by MongoDB itself.

It is simple to use. Just create an account and the UI leads you to simple steps to create a free MongoDB instance on the cloud. The free tier offered is enough for all you need to learn the subject.