How to Install MongoDB on Ubuntu 20.04

[ad_1]

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:

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:

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

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

{
	"_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.

[ad_2]

Source link

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.