How to install MongoDB on Ubuntu 20.04

s2 0

MongoDB is a free, 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.

MongoDB stores data in flexible, JSON-like
Documents where fields can vary from document to document. It does not require a pre-defined schema and the data structure can change over time.

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

The standard Ubuntu repositories contain an outdated MongoDB version. Installing the latest MongoDB on Ubuntu is pretty straightforward. We activate the MongoDB repository, import the repository GPG key and install the MongoDB server.

Install 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 required 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 GPG key of the repository 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 this writing, the latest version of MongoDB is version 4.4. To install a different version, replace 4.4 with your preferred version.

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

    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 Demon.
    • mongodb-org-shell – The Mongo Shell, an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks from 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 at boot by typing:

    sudo systemctl enable --now mongod
  5. To verify that the installation was successful, connect to the MongoDB database server using the mongo Tool and print the connection status:

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

    The output looks something 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
    }

    A value of 1 for the ok Field indicates success.

Configure MongoDB

The MongoDB configuration file is called mongod.conf and is located in /etc Directory. The file is in YAML
Format.

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

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

the authorization Option activated Role Based Access Control (RBAC)
governing user access to database resources and operations. If this option is deactivated, every user has access to all databases and can take any action.

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

sudo systemctl restart mongod

For more information about the configuration options available in MongoDB 4.4, see Configuration file options
Documentation page.

Create a MongoDB administrative user

If you’ve enabled MongoDB authentication, you’ll need to create an administrator who can access and manage the MongoDB instance.

Access the Mongo Shell:

mongo

In the MongoDB shell, enter the following command to connect to the. to manufacture admin Database:

use admin
switched to db admin

Run the following command to create a new user named. to create 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"
		}
	]
}
Don’t forget to set a more secure password. You can name the MongoDB administrative user whatever you want.

When you’re done, exit the Mongo shell with:

quit()

To test the changes, access the Mongo shell with the administrator user created earlier:

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 accessing the Mongo shell with no arguments (just type mongo) and see if you can list the users using the same commands as above.

diploma

We showed you how to install and configure MongoDB on Ubuntu 20.04. Further information on this topic can be found on the MongoDB manual
.

If you run into any problem or have any feedback, please leave a comment below.

Tags: