How to install MongoDB on Linux (Ubuntu 16.04)


Currently MongoDB only supports ubuntu 64-bit LTS (long term support). Example 14.04 LTS (trusty) and 16.04 LTS (xenial)

In this article, I install MongoDB Community Edition (free version) on ubuntu 16.04 operating system

Use package .deb to install MongoDB (Recommended)

1. Import GPG key

MongoDB public GPG Key .

  • sudo apt-key adv –keyserver hkp : // : 80 –recv 9 DA31620334BD 75D 9DCB49F368818C72E 52529D 4

2. Create file list for MongoDB.

With Ubuntu 14.04

  • echo “deb [arch = amd64] trusty / mongodb-org / 4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org- 4 . 0 .list

With Ubuntu 16.04

  • echo “deb [arch = amd64, arm64] xenial / mongodb-org / 4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org- 4 . 0 .list

3. Update package of ubuntu

  • sudo apt-get update

4. Install the latest version of MongoDB package.

  • sudo apt-get install -y mongodb-org

Access the MongoDB command line interface with the command mongo

How to install MongoDB on Linux (Ubuntu 16.04)

Show all databases

How to install MongoDB on Linux (Ubuntu 16.04)

Create a database stackjavawith collection user+ 1 document with information{"name":"kai"}

How to install MongoDB on Linux (Ubuntu 16.04)

Revisiting the databases you will see more database stackjava (if the database has no data (no collections, documents) it will not appear)

How to install MongoDB on Linux (Ubuntu 16.04)

Display the collections in the database stackjava

How to install MongoDB on Linux (Ubuntu 16.04)

Method 2, install MongoDB using tar.gz file

B1: Requirements: To install MongoDB  .tar.gz we need to install the following dependencies:

  • sudo apt-get install libcurl3 openssl

Download the MongoDB file  .tar.gz on the mongo homepage:

B2: Decompress the downloaded file:

For example:

  • tar -zxvf mongodb-linux – * – 4 . 0 . 0 .tgz

B3: Create an environment variable for mongo:

  • export PATH = <mongodb-install-directory> / bin: $ PATH

In it  mongodb-install-directoryis the unzipped directory path

Some commands used with MongoDB

Start MongoDB:

  • sudo service mongod start

(If it’s an error mongod.service not found, you run the following command:  )sudo systemctl enable mongod

Stop MongoDB:

  • sudo service mongod stop
Restart MongoDB:
  • sudo service mongod restart

Connect with MongoDB:

  • mongo –host ip: post

For example: mongo --host

Where you connect with local MongoDB above default port 27 017, just run the command mongois

Uninstall MongoDB Community Edition

B1: Stop MongoDB is working with stop command:

  • sudo service mongod stop

B2: Remove all MongoDB packages on your computer:

  • sudo apt-get purge mongodb-org *

B3: Delete the generated logs and database files of MongoDB

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

Okay, Done!