Preamble
MongoDB is a free open source database of documents. It belongs to a family of databases called NoSQL, which is different from traditional table-based SQL databases such as MySQL and PostgreSQL.
In MongoDB, data is stored in flexible documents such as JSON, where fields may differ from document to document. This does not require a predefined pattern, and the data structure may change over time.
This article describes how to install and configure MongoDB Community Edition in Ubuntu 20.04.
Standard Ubuntu repositories include the outdated version of MongoDB. Installing the latest version of MongoDB in Ubuntu is quite easy. We will enable the MongoDB repository, import the GPG repository key, and install the MongoDB server.
Install MongoDB in Ubuntu 20.04
To install MongoDB in Ubuntu do the following as root or sudo user:
- Set the dependencies needed to add a new repository via HTTPS:
sudo apt update
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
- Import the GPG repository key and add the MongoDB repository using it:
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 was version 4.4. To install another version, replace it with your preferred version. - After turning on the repository, install the meta-package mongodb-org by typing:
sudo apt install mongodb-org
The following packages will be installed in your system:
- mongodb-org-server – mongod daemon and corresponding initialization and configuration scripts.
- mongodb-org-mongos – The mongos daemon.
- mongodb-org-shell – Mongo shell, JavaScript interactive interface for MongoDB. It is used to perform administrative tasks from the command line.
- mongodb-org-tools – Contains several MongoDB tools to import and export data, statistics, and other utilities.
Start the MongoDB daemon and start it at boot by typing:
sudo systemctl enable --now mongod
To check if the installation has completed successfully, connect to the MongoDB database server with the mongo tool, and print the connection status:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
The result will look 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.
Setting up MongoDB
The MongoDB configuration file has the name mongod.conf and is located in the /etc directory. The file is in YAML format.
In most cases the default configuration settings are sufficient. However, for production environments we recommend that you comment on the security section and enable authorization as shown below:
sudo nano /etc/mongod.conf
/etc/mongod.conf
security:
authorization: enabled
The authorization parameter includes role-based access control (RBAC), which regulates user access to database resources and operations. If this option is disabled, each user will have access to all databases and perform any actions.
When editing the MongoDB configuration file, restart the mongod service for the 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.
Create a MongoDB administrator
If you enabled MongoDB authentication, you need to create a user with administrator rights who can access and manage the MongoDB instance.
Access to the shell of mongo:
mongo
From within the MongoDB shell, enter the following command to connect to the admin database:
use admin
switched to db admin
Perform the following command to create a new user with the mongoAdmin name, changeMe password, and the 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 administrative user MongoDB as you want.
After that, exit the mongo shell with the help:
quit()
To check for changes, log in to the mongo shell using the previously created administrator:
mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin
Launch, 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 to access the mongo shell without any arguments (just type mongo) and see if you can enumerate users using the same commands as above.
Conclusion
We showed you how to install and configure MongoDB in Ubuntu 20.04. For more information on this topic please visit the MongoDB manual.
About Enteros
IT organizations routinely spend days and weeks troubleshooting production database performance issues across multitudes of critical business systems. Fast and reliable resolution of database performance problems by Enteros enables businesses to generate and save millions of direct revenue, minimize waste of employees’ productivity, reduce the number of licenses, servers, and cloud resources and maximize the productivity of the application, database, and IT operations teams.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Optimizing Healthcare Enterprise Architecture with Enteros: Leveraging Forecasting Models for Enhanced Performance and Cost Efficiency
- 15 November 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Transforming Banking Operations with Enteros: Leveraging Database Solutions and Logical Models for Enhanced Performance
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing Database Performance on AWS EC2 with Enteros: A Cloud FinOps Solution for the Financial Sector
- 14 November 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing IT Sector Budgeting with Enteros: Enhancing Database Performance for Cost-Effective Operations
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…