MongoDB: Installation

MongoDB is a document database with scalability and flexibility in mind. It is a distributed database at its core – so scaling and high availability are built in and easy to use.

Run MongoDB on Docker


Make sure you have Docker installed on the machine.

Check the official MongoDB image details on the dockerhub at the link below-

Run the following command-

 docker run --name bigboxcode-mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=bigboxuser -e MONGO_INITDB_ROOT_PASSWORD=bigboxpass -d mongo:latest

Here are the details of arguments used in the command-

–name bigboxcode-mongoUse the –name argument to give the container a custom name.
Here we have named it “bigboxcode-mongo“.
-p 27017:27017Use the -p for mapping the container port to the host machine port.
We are doing this, so that we can connect our MongoDB client to the server.
Passed values should be in the format {host_port}:{container_port}.
Here we have mapped the same port 27017 from the container to the host.
But if you want to map some different(because of not availability) port of your host, say 27018, then pass it as -p 27018:27017. The port of the container will always be 27017.
-e MONGO_INITDB_ROOT_USERNAME=bigboxuserPass an environment variable using -e.
Here the name of the environment variable is MONGO_INITDB_ROOT_USERNAME.
The value is “bigboxuser“, so the username will be “bigboxuser“.
-e MONGO_INITDB_ROOT_PASSWORD=bigboxpassPass an environment variable using -e.
Here the name of the environment variable is MONGO_INITDB_ROOT_PASSWORD.
The value is “bigboxpass“, so the username will be “bigboxpass“.
-dThis runs the container in detached mode. This way the command will run in the background and the terminal will be free to use.
mongo:latestThis indicates two things- the first part before the colon (:) is “mongo” which is the image name.
The second part after the colon(:) is “latest” this is the tag that is related to the version and distribution.
Here we are running the latest version available of the “mongo” image on docker.
If you want to use any other version choose a different tag, ie. “mongo:4.4.26

You can choose a different username and password. After running this command the docker container will run and we can connect to the MongoDB container using our preferred client through port 27017 (or your defined port in the command.

Use the following command to run the docker container for later usage-

 docker start bigboxcode-mongo

Install on Ubuntu

Use the following command to import the MongoDB public GPG Key

sudo apt-key adv --keyserver hkp:// --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Then use the following command:

echo "deb [ arch=amd64 ] bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Run following command for getting the updates:

sudo apt-get update

Then install the MongoDB using command:

sudo apt-get install -y mongodb-org

You can restart the service by using:

sudo service mongod restart

To start use:

sudo service mongod start

And to stop:

sudo service mongod stop

To enable the service after reboot:

sudo systemctl enable mongodb

Now open /etc/systemd/system/mongodb.service file in any editor. Like, to edit it with vim use following command.

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

Add following lines to the file /etc/systemd/system/mongodb.service:

#Unit contains the dependencies to be satisfied before the service is started.


Description=MongoDB Database


# Service tells systemd, how the service should be started.

# Key `User` specifies that the server will run under the mongodb user and

# `ExecStart` defines the startup command for MongoDB server.




ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

# Install tells systemd when the service should be automatically started.

# `` means the server will be automatically started during boot.


Enable authentication

Use following commands to access mongo db:


Switch to ‘admin’ database:

use admin

Use following command to create your user: (set your password in place of ’test pass’)

db.createUser({user:"admin", pwd:"testpass", roles:[{role:"root", db:"admin"}]})

Update file /etc/systemd/system/mongodb.service with following line:

ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Update /etc/mongodb.conf with following line

    authorization: enabled

Restart mongoldb service, and you are done.

If you want to enable remote access– then open file /etc/mongod.conf and make changes in following lines:

  port: 27017
  bindIp: #This will make the server available from any ip. To restrict access from specific IP, set that IP here

Leave a Comment

The reCAPTCHA verification period has expired. Please reload the page.