Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Install MongoDB on Ubuntu 20.04 - DesignLinux

designlinux 0 Comments

MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.

In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and data structure can be changed over time.

This tutorial describes how to install and configure MongoDB Community Edition on Ubuntu 20.04.

The standard Ubuntu repositories include an outdated MongoDB version. Installing the latest MongoDB on Ubuntu is fairly straightforward. We’ll enable the MongoDB repository, import the repository GPG key, and install the MongoDB server.

Installing MongoDB on Ubuntu 20.04 #

Perform the following steps as root or user with sudo privileges to install MongoDB on Ubuntu:

  1. Install the dependencies necessary to add a new repository over HTTPS:

    sudo apt updatesudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
  2. Import the repository’s GPG key and add the MongoDB repository with:

    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'

    At the time of writing this article, the latest version of MongoDB is version 4.4. To install another version, replace 4.4 with your preferred version.

  3. Once the repository is enabled, install the mongodb-org meta-package by typing:

    sudo apt install mongodb-org

    The following packages will be installed on your system:

    • mongodb-org-server – The mongod daemon and corresponding init scripts and configurations.
    • mongodb-org-mongos – The mongos daemon.
    • mongodb-org-shell – The mongo shell, an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks thought the command line.
    • mongodb-org-tools – Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.
  4. Start the MongoDB daemon and enable it to start on boot by typing:

    sudo systemctl enable --now mongod
  5. To verify whether the installation has completed successfully, connect to the MongoDB database server using the mongo tool, and print the connection status:

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

    The output will look something like below:

    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
    }

    A value of 1 for the ok field indicates success.

Configuring MongoDB #

The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML format.

The default configuration settings are sufficient in most cases. However, for production environments, we recommend uncommenting the security section and enabling authorization, as shown below:

sudo nano /etc/mongod.conf
/etc/mongod.conf
security:
  authorization: enabled

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

When editing the MongoDB configuration file, restart the mongod service for changes to take effect:

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.4, visit the Configuration File Options documentation page.

Creating Administrative MongoDB User #

If you enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance.

Access the mongo shell:

mongo

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

use admin
switched to db admin

Run the following command to create a new user named mongoAdmin, with password changeMe and userAdminAnyDatabase role:

db.createUser(
  {
    user: "mongoAdmin",
    pwd: "changeMe",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
Do not forget to set a more secure password. You can name the administrative MongoDB user as you want.

Once done, exit the mongo shell with:

quit()

To test the changes, access the mongo shell using the administrative user you have previously created:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin

Run show users and you should see information about the newly created user:

show users
{
	"_id" : "admin.mongoAdmin",
	"userId" : UUID("49617e41-ea3b-4fea-96d4-bea10bf87f61"),
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.

Conclusion #

We’ve shown you how to install and configure MongoDB on Ubuntu 20.04. For more information on this topic, visit the MongoDB Manual .

If you hit a problem or have feedback, leave a comment below.

ubuntu mongodb database

Related

Tags: database, mongodb, ubuntu

How to Install CouchDB on Ubuntu 20.04

Prev Post

How to Install Sublime Text 3 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
  • 4
  • 688
  • 610,047

DesignLinux.com © All rights reserved

Go to mobile version