• 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 Debian 10 Linux

Written by Admin, Updated On August 15, 2020
database, debian, mongodb
How to Install MongoDB on Debian 10 Linux

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 Debian 10 Buster.

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#

MongoDB is not available in the default Debian Buster 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.

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

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

At first, you need to add the MongoDB GPG key to your system using curl:

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

After that enable the MongoDB repository:

sudo add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main'

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.

You should start the MongoDB service and enable it for start on boot:

sudo systemctl enable mongod --now

Now, 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.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5ef18h52-802j-99fg-cvjf-b6b06e801bc5") }
MongoDB server version: 4.2.1
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}

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

Configuring 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.2 on Debian 10 Buster. 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 MySQL on Debian 10 Linux
Next Article   How to Install and Use Docker on Debian 10

Related Posts

  • How to Install Php 8 on Debian 10

    How to Install PHP 8 on Debian 10

    January 2, 2021
  • How to Install GIMP on Debian 10

    How to Install GIMP 2.10 on Debian 10

    December 27, 2020
  • How to Install Python 3.9 on Debian 10

    How to Install Python 3.9 on Debian 10

    December 25, 2020

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on Debian 10
    How to Install Python 3.9 on Debian 10 December 25, 2020
  • How to Install Python 3.9 on Ubuntu 20.04
    How to Install Python 3.9 on Ubuntu 20.04 December 21, 2020
  • How to Install GIMP on Ubuntu 20.04
    How to Install GIMP 2.10 on Ubuntu 20.04 December 22, 2020
  • How to Install Notepad++ on Debian 10
    How to Install Notepad++ on Debian 10 December 23, 2020
  • How to Install Php 8 on Debian 10
    How to Install PHP 8 on Debian 10 January 2, 2021
© 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