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.
Table of Contents
Perform the following steps as root or user with sudo privileges to install MongoDB on Ubuntu:
Install the dependencies necessary to add a new repository over HTTPS:
$ sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
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.
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.Start the MongoDB daemon and enable it to start on boot by typing:
$ sudo systemctl enable --now mongod
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.
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
Update the following lines.
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.
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
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" } ] } )
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
Run show users and you should see information about the newly created user:
show users
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.
We’ve shown you how to install and configure MongoDB on Ubuntu 20.04. For more information on this topic, visit the MongoDB Manual .
The digital revolution has propelled us into a cloud-based world. Businesses are increasingly migrating their… Read More
Serverless computing has become a game-changer in the cloud development landscape. By abstracting away server… Read More
The digital age has brought an explosion of data. From personal photos and documents to… Read More
In the ever-evolving landscape of cloud computing, selecting the right tools for your applications is… Read More
Cloud Computing is present and future of remote technologies. In today's dynamic business landscape, digital… Read More
NGINX Server Nginx, a popular open-source web server, excels at handling high traffic websites efficiently.… Read More