Install MongoDB on Ubuntu 18.04 LTS Server
Before starting the installation of MongoDB on Ubuntu. Let’s have a description about MongoDB. MongoDB a type of database in which we deal with document i.e. database which is more scalable and flexible with the queries that you run to get results on specific conditions. MongoDB is a very fast and more easy to use than traditional database.
In this article we will learn how to install and manage MongoDb to use in our Ubuntu 18.04 server. So let’s start by the following steps.
STEP1:Â ADDING MONGODB PACKAGE REPOSITORY TO UBUNTU.
The first step for installing MongoDB on Ubuntu is to add repository files of MongoDB. Actually MongoDB is already in the repository in the Ubuntu by default. But the version in the repositories might not be the latest to your requirements. Therefore, to get the latest version of MongoDB, you have to add its repository in the Ubuntu. Following is the process to add official repository key by command.
Command: Â
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
After adding repository key into Ubuntu server, then you have to add the repository to your own system. For this do follow the command mentioned below:
Command:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
Step 2: Install MongoDB on Ubuntu 18.04
After adding repositories, you have to install MongoDB on Ubuntu, for this purpose follow the instructions by typing the following commands. This will install MongoDB completely on your Ubuntu and you will get access to use MongoDB.
Command:
sudo apt update sudo apt install -y mongodb-org
Step 3: Start MongoDB
After the successful installation of MongoDB over Ubuntu, you can now start MongoDB and manage its components. To verify whether MongoDB is running and active, run following command.
Command:
sudo systemctl status mongod
To start MongoDB type
sudo service mongod start
Step 4: Configure Mongo or Open Mongo Shell
When MongoDB is installed, then you can configure database username and passwords for further use. But before configuring username and passwords MongoDB shell has to be in running state or you have to open its shell. So for opening shell run or type the following command.
Command:Â
mongo
Â
Step 5:Â Use database Admin Mode
After opening the shell, you may need to move or switch to the database admin where you can create users and passwords respectively. For this purpose, just use the command mentioned below.
Command:Â
use admin
Step 6:Â Create root user for database
Once you are in the database admin mode, now you can create root user for database. This root user will have permission and roles as an admin to the database. To create root user type following command using your own username and password.
Command:
db.createUser({user:"user1", pwd:"adminuser", roles:[{role:"root", db:"admin"}]})
Step 7: Â MongoDB authentication
When you done with creating your root user, then after exiting the MongoDB, you can now authenticate this. To authenticate MongoDb use the following command.
Command:Â
nano /lib/systemd/system/mongod.serviceExecStart=/usr/bin/mongod --auth --config /etc/mongod.confsystemctl daemon-reload
Â
Â
Step 8: Connect MongoDB after restarting
After complete authentication and restarting of MongoDB, now you have to connect again with the details of username and password, you have created earlier in the creating root user step. Step 8: to connect run the following command.
Command:
to connect MongoDB
mongo -u admin -p admin123 --authenticationDatabase admin
to restart MongoDB*
sudo service mongod restart   Â
Conclusion:
After following all the guideline, you are now able to install MongoDb repositories in Ubuntu as well as in your system. And using MongoDb to manage and authenticate as well as creating root user. However, you should follow the same procedure to make it accurate. If you face any problem, then leave a message or comment. We will help you in every matter.