How To Install MongoDB on Ubuntu 18.04 LTS (Bionic Beaver)

0

MongoDB is a free and open source cross-platform, document-oriented database designed with both scalability and developer agility in mind. It is written in C++ and JavaScript. MongoDB uses JSON-like documents with dynamic schemas to store data.

MongoDB is available for Windows, Linux, OS X, and Solaris.

MongoDB is released under the GNU Affero General Public License. The language drivers are available under an Apache License.

Install MongoDB on Ubuntu 18.04 LTS

You can install MongoDB in two ways, using either Ubuntu repository or Official MongoDB repository.

  1. Install MongoDB from Official MongoDB repository
  2. Install MongoDB from Ubuntu Repository

1. Install MongoDB from MongoDB Repository

MongoDB Inc releases stable packages for Ubuntu operating systems such as Ubuntu 18.04 and Ubuntu 16.04, and their packages are generally fresher than those in the Ubuntu repositories. You should always use the official mongodb-org packages

This package might conflict with the mongodb packages in Ubuntu (If you have it already installed).

Import the public key to the system.

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

Add the official MongoDB repository to the system.

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

Update the repository index.

sudo apt update

Install the MongDB package using the apt command. The name of the MongoDB package is mongodb-org.

sudo apt install -y mongodb-org

MongoDB has its controlling system, and it can be started/stopped by the easy known commands.

To start MongoDB server, run:

sudo systemctl start mongod

To enable MongoDB service to start automatically at the system start up, run:

sudo systemctl enable mongod

To check the status of MongoDB service, run.

sudo systemctl status mongod

Output:

 mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-01-18 02:20:30 UTC; 10min ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2857 (mongod)
   CGroup: /system.slice/mongod.service
           └─2857 /usr/bin/mongod --config /etc/mongod.conf

Jan 18 02:20:30 mongodb1804 systemd[1]: Started MongoDB Database Server.

Confirm the version of MongoDB.

mongod --version

Output:

db version v4.0.5
git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

2. Install MongoDB from Ubuntu Repository

Installing MongoDB from Ubuntu’s default repository is a straightforward way, but the version available in Ubuntu repository may be bit older than the one in official MongoDB repository.

MongoDB Inc does not maintain the MongoDB package provided by Ubuntu.
sudo apt update
sudo apt -y install mongodb mongodb-server mongo-tools

By this time, MongoDB service should be up and running. To check the status of MongoDB service, run.

sudo systemctl status mongodb

Output:

 mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-01-18 02:38:23 UTC; 17s ago
     Docs: man:mongod(1)
 Main PID: 2726 (mongod)
    Tasks: 23 (limit: 4401)
   CGroup: /system.slice/mongodb.service
           └─2726 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.conf

Jan 18 02:38:23 mong1804 systemd[1]: Started An object/document-oriented database.

Confirm the version of MongoDB.

mongod --version

Output:

db version v3.6.3
git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017
allocator: tcmalloc
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

MongoDB has its controlling system, and it can be started/stopped by the easy known commands.

To start MongoDB server, run:

sudo systemctl start mongodb

To stop MongoDB server, run:

sudo systemctl stop mongodb

Access MongoDB

Connect to MongoDB shell by using the following command.

mongo

Output:

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2e6a6f70-b591-4316-9a7d-d593d6fe35e5") }
MongoDB server version: 4.0.5
Server has startup warnings: 
2019-01-18T02:20:30.994+0000 I STORAGE  [initandlisten] 
2019-01-18T02:20:30.994+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-01-18T02:20:30.994+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-01-18T02:20:31.719+0000 I CONTROL  [initandlisten] 
2019-01-18T02:20:31.719+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-18T02:20:31.719+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-18T02:20:31.719+0000 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 

Fix MongoDB errors

Error 1

2019-01-18T02:20:30.994+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-01-18T02:20:30.994+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem

To clear this issue, we recommend you to use XFS filesystem for MongoDB.

Error 2

2019-01-18T02:20:31.719+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-18T02:20:31.719+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

To resolve this issue, follow the MongoDB security checklist.

That’s all.

Further Reading

You might also like

How To Install MongoDB on Ubuntu 18.04 LTS (Bionic Beaver)

0