REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
How to install MongoDB in Ubuntu 20.04
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.
Installing 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.
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...