Install MongoDB on Ubuntu

Install MongoDB on Ubuntu – In this tutorial, we shall learn to install MongoDB Community Edition on Ubuntu 16.04. The process should remain same for 64 bit versions of Ubuntu 14.04 and Ubuntu 12.04.

Following is a quick overview of the steps we go through while installing MongoDB on Ubuntu.

  1. Import MongoDB GPG public key
  2. Create list file for MongoDB
  3. Reload Local Package Database
  4. Install MongoDB Packages
  5. Start MongoDB
  6. Stop MongoDB

Following is a detailed step by step guide to Install MongoDB on Ubuntu.

1. Import MongoDB GPG public key

Open a terminal and run the following command to import MongoDB GPG public key.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
root@arjun-VPCEH26EN:/home/arjun# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Executing: /tmp/tmp.2327Zvbpks/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
0C49F3730359A14518585931BC711F9BA15703C6
gpg: requesting key A15703C6 from hkp server keyserver.ubuntu.com
gpg: key A15703C6: public key "MongoDB 3.4 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

2. Create list file for MongoDB

The list file changes from Ubuntu release to release.

Run the corresponding command in terminal for the Ubuntu version of yours.

Ubuntu ReleaseCommand
Ubuntu 12.04 (Precise)echo “deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 14.04 (Trusty)echo “deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 16.04 (Xenial)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

When run for Ubuntu 16.04 (Xenial)

root@arjun-VPCEH26EN:/home/arjun# 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
deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse

3. Reload Local Package Database

Run the following command in terminal to reload local package database.

sudo apt-get update

4. Install MongoDB Packages

Running following command installs MongoDB Packages.

sudo apt-get install -y mongodb-org
...
The following additional packages will be installed:
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 5 not upgraded.
Need to get 66.9 MB of archives.
After this operation, 270 MB of additional disk space will be used.
Get:1 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-shell amd64 3.4.9 [7,983 kB]
Get:2 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-server amd64 3.4.9 [14.2 MB]
Get:3 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-mongos amd64 3.4.9 [8,113 kB]                     
Get:4 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-tools amd64 3.4.9 [36.5 MB]                       
Get:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org amd64 3.4.9 [3,526 B]                             
Fetched 66.9 MB in 1min 48s (614 kB/s)                                                                                                         
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 1279975 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_3.4.9_amd64.deb ...
Unpacking mongodb-org-shell (3.4.9) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../mongodb-org-server_3.4.9_amd64.deb ...
Unpacking mongodb-org-server (3.4.9) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_3.4.9_amd64.deb ...
Unpacking mongodb-org-mongos (3.4.9) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_3.4.9_amd64.deb ...
Unpacking mongodb-org-tools (3.4.9) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../mongodb-org_3.4.9_amd64.deb ...
Unpacking mongodb-org (3.4.9) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up mongodb-org-shell (3.4.9) ...
Setting up mongodb-org-server (3.4.9) ...
Adding system user `mongodb' (UID 130) ...
Adding new user `mongodb' (UID 130) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 139) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-mongos (3.4.9) ...
Setting up mongodb-org-tools (3.4.9) ...
Setting up mongodb-org (3.4.9) ...

5. Start MongoDB

To start MongoDB, run the following command in terminal

sudo service mongod start

To verify if MongoDB has started, check mongodb.log whose location is /var/log/mongodb/mongodb.log

2017-10-14T17:08:27.786+0530 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-10-14T17:08:27.787+0530 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-10-14T17:08:27.787+0530 I NETWORK  [thread1] waiting for connections on port 27017

MongoDB would be started and waiting for connections on 27017.

6. Stop MongoDB

To stop MongoDB, run the following command in terminal

sudo service mongod stop
2017-10-14T17:11:28.121+0530 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2017-10-14T17:11:28.121+0530 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2017-10-14T17:11:28.121+0530 I NETWORK  [signalProcessingThread] closing listening socket: 7
2017-10-14T17:11:28.122+0530 I NETWORK  [signalProcessingThread] closing listening socket: 8
2017-10-14T17:11:28.122+0530 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2017-10-14T17:11:28.122+0530 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2017-10-14T17:11:28.122+0530 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2017-10-14T17:11:28.127+0530 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2017-10-14T17:11:28.475+0530 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2017-10-14T17:11:28.475+0530 I CONTROL  [signalProcessingThread] now exiting
2017-10-14T17:11:28.475+0530 I CONTROL  [signalProcessingThread] shutting down with code:0
2017-10-14T17:11:28.475+0530 I CONTROL  [initandlisten] shutting down with code:0

Conclusion

In this MongoDB Tutorial – Install MongoDB on Ubuntu, we have learnt to install MongoDB on Ubuntu, start MongoDB and stop MongoDB.