• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install MongoDB on Ubuntu 22.04

Written by Admin, Updated On January 21, 2023
database, mongodb, ubuntu
How to Install Mongodb on Ubuntu 22.04

MongoDB is a cross-platform and open-source 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 on Ubuntu 22.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.

How to 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#

At first, 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#

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

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

Currently, at the time of writing this article, the latest version of MongoDB is 6.0. 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#

After that, 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 v6.0.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: 6.0.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 recommend 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 6.0 on Ubuntu 22.04. You can get more details about by visit MongoDB Manual.

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

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install and Use Docker on Debian 11
Next Article   How to Install Microsoft Edge Browser on Ubuntu 22.04

Related Posts

  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install LEMP Stack on Ubuntu 22.04

    How to Install LEMP Stack on Ubuntu 22.04

    March 18, 2023
  • How to Install Memcached on Ubuntu 22.04

    How to Install Memcached on Ubuntu 22.04

    March 16, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook