How to install MongoDB on Ubuntu 20.04 or 18.04

MongoDB is a database system that is open-source and based on NoSQL technology. It is ideal for the development of modern web applications. This database has many features, such as expressive query languages, flexibility, secondary indexes and more. It offers excellent scalability and performance that enable the building of powerful modern applications with robust databases.

Unlike traditional relational databases that save data in tables, MongoDB stores data in BSON data structures that have a dynamic schema. This makes data integration in some applications faster and easier.

Many industries widely use MongoDB because of its multiple functionalities, making it suitable for production use. It has an available source code for operating systems like Linux, Windows, OSX, and Solaris.

Read: Install MongoDB on Ubuntu 22.04

MongoDB installation

MongoDB is available on Ubuntu official repositories. In order to install it, you would first have to update the packages list as follows: :

sudo apt update

Next run the command :

sudo apt install -y mongodb                                               [install mongodb linux command]

Several packages which contain the most recent MongoDB stable version will be installed.

Read: How to use the APT command on Ubuntu/Debian Linux systems

After the installation is completed, the DB server will start automatically as verified below :

sudo systemctl status mongodb

Which will check the status of the service. In the output, you should see an active status being returned which means that the server is up and running successfully.

You should be able to connect to the database server and retrieve the connection status as follows:

mongo –eval ‘db.runCommand({ connectionStatus: 1 })’

This will display connection status, the database version as well as the server address along with other details.

Here is an example of such information :

MongoDB server version: 4.4.10

{
“authInfo” : {
“authenticatedUsers” : [ ],
“authenticatedUserRoles” : [ ]
},
“ok” : 1
}

Notice the OK field. A value of 1 shows that the server is active and running.

Read: How to install MySQL on Ubuntu 18.04

If the service is not active, you can start it manually using the command :

sudo systemctl start mongodb

To stop the service, invoke the command :

sudo systemctl stop mongodb

In case you would like to restart the service, issue the command :

sudo systemctl restart mongodb

Note however that by default, MongoDB is launched automatically as soon as the service is up and running. You can however override this using the command :

sudo systemctl disable mongodb

And to enable it, run :

sudo systemctl enable mongodb

Note that in order to configure MongoDB, you would need to edit the file /etc/mongod.conf.
You can also locate the MongoDB configuration file by executing this command:

sudo locate mongod.conf

It will return the absolute path of the configuration file. Head over to the official documentation website for more information.

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Marianne elanotta

Marianne is a graduate in communication technologies and enjoys sharing the latest technological advances across various fields. Her programming skills include Java OO and Javascript, and she prefers working on open-source operating systems. In her free time, she enjoys playing chess and computer games with her two children.

Leave a Reply