Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

How to install MongoDB 4.2 – CentOS 7, Ubuntu 18.04 LTS and Ubuntu 19.04

 

Today we’ll be looking at how to install MongoDB 4.2 on CentOS 7, Ubuntu 18.04 LTS and Ubuntu 19.04

A little bit about MongoDB as per their website:

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era.

Before you set off you need to make sure you’re either logged in as root or a user with sudo privileges

Installation on CentOS 7[ps2id id=’centos-anchor’ target=”/]

Add the MongoDB 4.2 repository

Create a new file at /etc/yum.repos.d/mongodb-4.2.repo and paste the following into the file:

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

Install MongoDB

Issue the following to install MongoDB onto the server:

sudo yum install -y mongodb-org

This will install the following packages and dependencies:

mongodb-org
mongodb-org-mongos
mongodb-org-server
mongodb-org-shell
mongodb-org-tools

Start the service

To start the MongoDB service issue the following:

sudo service mongod start

Add MongoDB to startup

To start MongoDB on server boot issue the following:

sudo chkconfig mongod on

Verify the installation

To verify the installation, issue the following at the terminal:

mongod --version

The server should return something like the following:

[root@vm109 ~]# mongod --version
db version v4.2.0
git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

Start using MongoDB

Start a mongo shell and start using it on the server:

mongo
[root@vm109 ~]# mongo
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
MongoDB server version: 4.2.0
---

>

Installation on Ubuntu 18.04 LTS and Ubuntu 19.04[ps2id id=’ubuntu-anchor’ target=”/]

Import the public key

Issue the following at the terminal to import the MongoDB public GPG key:

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

Create a list file for MongoDB

Issue the following to create a list file for MongoDB:

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

Reload the local package database

Issue the following to reload the local package database of Ubuntu:

sudo apt-get update

Install the MongoDB

Issue the following to install MongoDB server and it’s dependencies

sudo apt-get install -y mongodb-org

This will install the following packages and it’s dependencies:

mongodb-org
mongodb-org-mongos
mongodb-org-server
mongodb-org-shell
mongodb-org-tools

Start the service

Issue the following to start the MongoDB service:

sudo service mongod start

Verify the installation

To verify the installation, issue the following:

mongod --version
theshell@ub1804:~$ mongod --version
db version v4.2.0
git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

Start using MongoDB

Start a mongo shell and start using it on the server:

mongo

That’s it, you’re done!