Install MongoDB on Ubuntu

install mongodb on ubuntu

Installing MongoDB on Ubuntu is only a few steps of procedure.

Let’s install MongoDB on Linux-Ubuntu using the native Ubuntu package manager apt.

Note: This has been tested on Ubuntu 18.04

So let’s get started…

Install MongoDB on Ubuntu

Step 1. Add and Import the public key

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

Step 2. Create a Sources list file for MongoDB

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu lsb_release -c | awk -F '\t' '{print $2}'/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Step 3. Reload local package database

sudo apt-get update

Step 4. Install the MongoDB packages

sudo apt-get install -y mongodb-org

And this will install MongoDB on our Ubuntu system. We will see how we can start & stop Mongo, but before that a few things to check .

Things to Check

Directories

Now, If you installed via the package manager, the data directory /var/lib/mongodb and the log directory /var/log/mongodb are created during the installation.

Configuration File

The official MongoDB package includes a configuration file which is located at /etc/mongod.conf

Check MongoDB version on Ubuntu

Mongo – Database Version

mongod --version
// Below is the output
db version v4.0.9
 git version: fc525e2d9b0e4bceff5c2201457e564362909765
 OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017
 allocator: tcmalloc
 modules: none
 build environment:
     distmod: ubuntu1804
     distarch: x86_64
     target_arch: x86_64

Mongo – Client Version

mongo --version
// Below is the output
MongoDB shell version v4.0.9
 git version: fc525e2d9b0e4bceff5c2201457e564362909765
 OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017
 allocator: tcmalloc
 modules: none
 build environment:
     distmod: ubuntu1804
     distarch: x86_64
     target_arch: x86_64

Starting and Stopping MongoDB

There are a few methods we can start and stop MongoDB database. Let’s have a look at 2 of those methods.

Method 1: Using the `service` command

Start MongoDB

sudo service mongod start

Stop MongoDB

sudo service mongod stop

Restart MongoDB

sudo service mongod restart

Method 2: Using the `mongod` command

We will start mongo with a custom path for our database files. Mongo accepts a custom directory where we can choose to store our database at. So let’s start Mongo with a custom dbpath parameter…

Let’s first create a custom directory named db1 where we can store our database.

Step 1. Create a custom folder

mkdir -p ~/projects/db/mongo/tutorials/db1

Step 2. Start Mongo with the custom folder as dbpath

And now start MongoDB with this directory where our database will be stored.

sudo mongod --dbpath ~/projects/db/mongo/tutorials/db1

Step 3. Verify

Verify that the `mongod` process has started successfully by checking our ports with the following command in a different terminal tab.

sudo netstat -plnt

Step 4. Stop

Press CTRL+C to end the MongoDB process that was started in Step 2 in that same terminal tab where it was started.

Begin using MongoDB.

Start a mongo shell on the same host machine as the mongod. You can run the mongo shell without any command-line options to connect to a mongod that is running on your localhost with default port 27017:

mongo

Uninstall MongoDB

Let’s uninstall the MongoDB server, client and all the related packages from our Ubuntu system.

Step 1. Stop MongoDB.

Stop the mongod process by issuing the following command.

sudo service mongod stop

Step 2. Remove Packages.

Remove any MongoDB packages that you had previously installed.

sudo apt-get purge mongodb-org*

Step 3. Remove Data Directories.

Remove the default MongoDB databases and log files, or any other custom database and log files that you created. We created 1 database directory (`~/projects/db/mongo/tutorials/db1`) here in our blog so we can include that too. But, we’ll just use the default directories for the moment.

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

Author: Admin

Hello Friends, welcome to Hackerdays.com. I am the Admin of the site. I have worked as a software programmer for 7 years. I have started this site to learn as well as share whatever little knowledge i have gained over the period of time. If you wish to contact me, you can drop in an email to: admin[AT]hackerdays[DOT]com or you can also use the contact page. Thank you and I'll see you around.

Let us know your thoughts: