How to Install MongoDB on Ubuntu 18.04, 20.04 and 22.04 + Database and User Setup
MySQL is probably the most popular relational database management system currently available. However, many other powerful management systems like PostgreSQL, SQLite, Oracle, and MariaDB exist as well. One feature that all of these systems share is that they use SQL language.
On the other hand, MongoDB is one of the most popular NoSQL database managers out there. In this tutorial, we will provide a step-by-step guide on how to install the latest stable version of MongoDB on Ubuntu. We will also cover the process of creating a new database and database user and show you how to enable remote authentication.
What Is MongoDB
MongoDB is one of the most popular and best-known NoSQL database managers. It is primarily used for applications where users can save data in formatted documents such as BSON. Instead of saving data in SQL data types, MongoDB saves them in documents.
NoSQL-type database managers such as MongoDB don’t have a predefined schema. While this might concern some users, in a NoSQL system, fields and data are simplified. Thus, it is easier to manage and store them.
In addition to that, MongoDB is popular in environments with massive scalability. MongoDB allows users to perform quick replication techniques that enable fast data scalability. So any application that requires storing semi-structured data can use MongoDB.
How to Install MongoDB on Ubuntu 18.04, 20.04 and 22.04
Installing the MongoDB Community Edition is as simple as using the APT packages manager. However, keep in mind that to install the latest version of MongoDB, you will need to include its package repository first.
Important! At the time of writing this tutorial, MongoDB is not yet available for Ubuntu 22.04. Thus, we recommend sticking with Ubuntu 20.04 or 18.04 until an official MongoDB release for 22.04 comes out.
Step 1 – Installing MongoDB Database Server
Open the terminal and import the MongoDB public GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
If you get an error regarding GnuPG, run the following command:
sudo apt-get install gnupg
Then try to import the public GPG key once more.
Ubuntu 20.04:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Ubuntu 18.04:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Next, refresh the APT command to synchronize all repositories:
sudo apt-get update
Install MongoDB using APT:
sudo apt-get install -y mongodb-org
The previously mentioned command will install the latest version of MongoDB. If you want to install a specific version, proceed with the following command. In our example, we are installing the 5.0.7 MongoDB shell version:
sudo apt-get install -y mongodb-org=5.0.7 mongodb-org-database=5.0.7 mongodb-org-server=5.0.7 mongodb-org-shell=5.0.7 mongodb-org-mongos=5.0.7 mongodb-org-tools=5.0.7
Step 2 – Starting MongoDB Service
After MongoDB installation is complete, it’s time to start it:
sudo systemctl start mongod
In case of an error, run this command and try starting MongoDB again:
sudo systemctl daemon-reload
To confirm that the MongoDB instance is running, check its status with this command:
sudo systemctl status mongod
Your output should look something like the example below.
Lastly, run this command to set MongoDB daemon to start automatically whenever the computer or a VPS setup gets restarted:
sudo systemctl enable mongod
With the MongoDB database server loaded, it is now ready to be used!
How to Setup Databases and Users
With the newest MongoDB server version installed, proceed with its configuration.
Pro Tip
MongoDB configuration file is located in /etc/mongod.conf. Keep in mind that any changes to this file require a restart of the application to work. By default, MongoDB will store logs in /etc/mongod.conf./var/log/mongodb/mongod.log and will have 27017 set as its default port.
Creating a New Database
MongoDB comes with a single database labeled admin. For testing purposes, it’s recommended to create a few additional databases. To do so, access the MongoDB shell:
mongosh
Once there, you can create a database using a single command. Keep in mind that, unlike SQL language, there is no separate command to create databases specifically.
MongoDB uses the same command for accessing and creating databases. If a database exists already, it can be used. Otherwise, the command will create it instead:
use [database_name]
In this tutorial, we will go with the customers database.
use customers
Creating a New User
By default, MongoDB does not include the default administrator account. Instead, different users for each database are needed. However, creating users with specific permissions in each database is necessary.
To create a new user, use the db.createUser() function. Specify the name, password, database, and roles it should have.
Similar to MongoDB, the db.createUser function receives parameters in JSON. So, to create a new user for the newly created database, run this command:
db.createUser( { user: "Hostinger", pwd: "$tr0ngPa$$w0rD", roles: [ { role: "userAdmin", db: "customers" } ] } )
There are several roles, including dbAdmin, dbUser, and readWrite, alongside many others. We recommend visiting the official MongoDB documentation to learn more.
In order to show all created users, use this command:
show users
The output should look something like this on the terminal.
To test the operation, exit the MongoDB console with the exit command and run the following one to test the database connection:
mongosh --port [port] -u [username] -p '[password]' '[database]'
Enabling Remote Authentication
By default, MongoDB authorizes all logins from the local machine. This is not an issue if users are developing applications locally. However, you might encounter some problems when it’s time to deploy an application.
To avoid such issues in the future, open the /etc/mongodb.conf file and comment on the “bindIP: 127.0.0.1” line:
sudo nano /etc/mongodb.conf
Finally, restart the service:
sudo systemctl restart mongod
Now only local users will be able to log in without authentication.
Conclusion
NoSQL database management systems such as MongoDB exist to offer excellent speed, stability, and scalability, which are crucial for any type of enterprise.
In this tutorial, we have covered the MongoDB installation process for Ubuntu 18.04 and 20.04. We’ve also gone through the process of setting up a new database along with users and user roles.
We hope that you found this tutorial helpful. If you have any further questions, leave them in the comments section below.
Comments
July 31 2020
Thank you, but I don't know how can I connect to Mongodb on VPS via Robo3T on local machine. Please help....
November 06 2020
Hey Vi! That would depend on your local machine's settings. I'd strongly recommend checking some official mongodb and robo3t resources for this.
September 11 2021
My Question is how to deploy any website on hostinger with mongodb database?
September 20 2021
Hi there, first things first - make sure you have a VPS (not a shared or cloud plan, as they do not support MongoDB), then you can proceed to setting up your server and installing MongoDB. Lastly, follow the instructions of your framework to deploy it. Good luck!