Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Install MongoDB on Ubuntu 20.04 - DesignLinux

Aug 18 2020
designlinux 0 Comments
How to Install MongoDB on Ubuntu 20.04

MongoDB is an opensource, cross-platform NoSQL database server. It uses JSON document to store data and fields can be vary from compare to other. In this tutorial, we will explain how to install latest version of MongoDB Community Edition on Ubuntu 20.04.

MongoDB is popular for handling large amounts of data due to its performance, scalability and high availability. It doesn’t require a predefined schema or data structure, it might be different. It is different from the traditional table-based SQL databases like MySQL and PostgreSQL. For more details check official installation page of MongoDB’s documentation.

Install MongoDB on Ubuntu#

MongoDB is not available in the default Ubuntu repositories. We need to enable manually the official MongoDB repository and then install the packages.

Perform the following steps as root or user with sudo privileges.

Step 1 – Install Dependencies#

First of all, install additional packages which are require, run following command:

sudo apt update
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Step 2 – Import GPG Key#

After that, you need to add the MongoDB GPG key to your system using wget:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

Currently, at the time of writing this article, the latest version of MongoDB is 4.4. It may different while you are installing.

Step 3 – Install Meta Package#

Now, update the packages list and install mongodb-org meta-package:

sudo apt update
sudo apt install mongodb-org

Below packages will be installed on the system as a part of the mongodb-org package:

  • mongodb-org-server – The mongod daemon and corresponding init scripts and configurations.
  • mongodb-org-mongos – The mongos daemon.
  • mongodb-org-shell – The mongo shell is an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks through the command line.
  • mongodb-org-tools – Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.

Step 4 – Start MongoDB Service#

Now, you should start the MongoDB service and enable it for start on boot:

sudo systemctl enable mongod --now

Step 5 – Verify Installation#

At last, it’s time to verify that installation is completed successfully. Login to mongo tool and print connection status:

mongo
mongo --eval 'db.runCommand({ connectionStatus: 1 })'

It should look like this:

MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") }
MongoDB server version: 4.4.0
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

If in ok field value is 1 then it indicates success.

Configure MongoDB#

Generally, default configuration settings are enough for most users. But it’s advised to change few settings for production environment, specially security section. The MongoDB configuration file mongod.conf is located in the /etc directory. Edit the mongod.conf file and uncomment the security section and enable authorization:

security:
   authorization: enabled

It will enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled, each user can access all databases and perform any action.

After making changes you need to restart the mongod service for changes to take effect:

sudo systemctl restart mongod

You can get more information about the configuration options, visit the Configuration File Options documentation page.

Creating Administrative MongoDB User#

You will need create an administrative user account to access and manage the MongoDB instance if you enabled the MongoDB authentication. You can access just typing:

mongo

From inside the MongoDB shell, type the following command to connect to the admin database:

use admin
switched to db admin

Issue the following command to create a new user named admin_user with the userAdminAnyDatabase role:

db.createUser(
   {
     user: "admin_user", 
     pwd: "your_password", 
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   }
 )
 Successfully added user: {
     "user" : "admin_user",
     "roles" : [
         {
             "role" : "userAdminAnyDatabase",
             "db" : "admin"
         }
     ]
 }

You can exit from mongo shell by typing:

quit()

Now, access the mongo shell using the administrative user which you have created previously and check the changes:

mongo -u admin_user -p --authenticationDatabase admin

It will prompt to enter password. On success connection select the admin database:

use admin
switched to db admin

You can print list of users by typing:

show users
{
     "_id" : "admin.admin_user",
     "userId" : UUID("dfd92e5s-ds69-3er4-a5c6-654sd0c32e8c"),
     "user" : "admin_user",
     "db" : "admin",
     "roles" : [
         {
             "role" : "userAdminAnyDatabase",
             "db" : "admin"
         }
     ],
     "mechanisms" : [
         "SCRAM-SHA-1",
         "SCRAM-SHA-256"
     ]
 }

Conclusion#

In this guide, you learned how to install MongoDB 4.4 on Ubuntu 20.04. You can get more details about by visit MongoDB Manual.

Feel free to leave comment, if you have any question or suggestion.

Related

Tags: database, mongodb, ubuntu

How to Install and Use PHP Composer on Debian 10

Prev Post

How to Install and Configure Fail2ban on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 3
  • 693
  • 610,052

DesignLinux.com © All rights reserved

Go to mobile version