Superb MongoDB for Your Ubuntu 20.04

MongoDB is an excellent, easy-to-use, and important, free, open-source document database. It belongs to a family of databases called NoSQL. This family has one big difference from traditional table-based SQL databases such as MySQL and PostgreSQL. This database is developed by MongoDB Inc. and distributed under the Server Side Public License. What is remarkable is that this database is written in C++, so it can be easily ported to a variety of platforms. With its help, it can be deployed on Windows, Linux, MacOS, Solaris platforms. You can also download the source code and compile this database yourself, but it is recommended to use libraries from offsite.
As for the data format in this database, one of the popular standards for data exchange and, accordingly, their storage is JSON (JavaScript Object Notation). JSON effectively describes data that is complex in structure. MongoDB’s way of storing data is similar to JSON in this regard, although JSON is not formally used. For storage, MongoDB uses a format called BSON (BiSon), or short for binary JSON. Further, after a little familiarization with the basic properties of the database, we will proceed directly to the installation. The following will detail how you can install this on your server running Ubuntu 20.04 Operating System.
Installing MongoDB on Ubuntu 20.04
Follow these steps to install MongoDB on Ubuntu. This will require root or sudo user access. Before installing the database, you need to install the dependencies that will be needed to add a new repository via HTTPS:
apt update
apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
An important next step is to import your repository’s GPG key. After a successful import – you need to add the Mongo repository using the following command, which is described below (you can install it using wget program):
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'
With this step, you have enabled your repository. Once you’ve done this successfully, you should install the mongodb-org meta package. It can be followed like this:
apt install mongodb-org
Next, after successfully completing the previous steps, you need to start your database daemon and enable it to start at boot. This can be done like this:
systemctl enable --now mongod
It is recommended to check if the installation completed correctly and successfully, connect to the database server. You can check it like this:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v...
connecting to: mongodb://Your_IP_Address:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") }
MongoDB server version: ...
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}
Setting up Your Mongo
The database configuration file is named mongod.conf and is located in the /etc directory. This special file contains the YAML file. It is important to note that in most cases the default configuration settings are sufficient for basic configuration and operation.
But it’s also worth noting that a security section is recommended for production environments. You should also enable authentication.
This can be done using a special command:
nano /etc/mongod.conf
For security mode please use:
security:
authorization: enabled
The parameter that is specified as authorization has an important property. It points to role-based access control (RBAC). This, in turn, is the regulator for user access to the resources and operations of the entire database. It is very important to note that when editing your Mongo configuration file, restart the mongod service for the changes to take effect. This can be done using a special command:
systemctl restart mongod
About securing Mongo you can find it here
Creating Your Mongo Administrator
Once you have successfully installed your Mongo database, you can proceed to the next steps. You will definitely need to create a user with administrator rights. The user will control your database and be able to access the instance. Please enter this command for access to Mongo
mongo
Then, inside your Mongo shell, enter the following command to connect to the admin database. This will help you get started on the next steps.
use admin
switched to db admin
Then you should run the following special command. It will be responsible for creating a new user called MongoAdmin, as well as the password changepasswd and the role user AdminYourDatabase:
db.createUser(
{
user: "MongoAdmin",
pwd: "changepasswd",
roles: [ { role: "AdminYourDatabase", db: "admin" } ]
}
)
We recommend setting a strong password. After that, you will be able to exit your MongoDB. To do this, enter the following:
quit()
As an addendum, it’s worth noting that you, as an administrator, have the ability to access the mongo shell without any arguments. You can just type “mongo”.