Advance

How To Install MongoDB 4 In CentOS 8

How to install MongoDB 4 in CentOS 8

[Updated on Date: 2021-01-16] MongoDB is a popular document-based and general-purpose NoSQL database engine that stores data in JSON format. The MongoDB is free and open-source and ships with a set of cool and nifty features such as file storage, data replication, Ad-hoc queries, and load balancing just to mention a few. Some of the blue-chip companies that have incorporated MongoDB in their applications include Adobe, Facebook, Google, eBay, and Coinbase. In this article, we are going to learn you How To Install MongoDB 4 In CentOS 8. You can visit the packages available in Eldernode to purchase a CentOS VPS server.

Tutorial Install MongoDB 4 In CentOS 8

In this article, you will learn How to install MongoDB 4 in CentOS 8.  Join us to check the details.

Recommended Article: Tutorial Install Innotop to Monitor MySQL on CentOS

How To Add MongoDB Repository

Since MongoDB is not present in the CentOS 8 default repository, you need to add it manually. So firstly, create a repository file as shown.

vi /etc/yum.repos.d/mongodb.repo  

Paste the configuration below and save the file.

[mongodb-org-4.2]  name=MongoDB Repository  baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/  gpgcheck=1  enabled=1  gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

Install MongoDB in CentOS 8 | CentOS 7

Once the repository enabled, you can install MongoDB using the following dnf command:

dnf install mongodb-org

Then, start and enable MongoDB to start on boot by running the commands below:

systemctl start mongod  
sudo systemctl enable mongod

Use the following command to verify the status of MongoDB:

systemctl status mongod  

And also, to confirm that indeed Mongod service is listening:

netstat -pnltu

 

Verify-MongoDB-Port

 

How To Access MongoDB Shell

It is time to access MongoDB’s shell by simply issuing the command:

mongo

The output will be similar to the below screenshot

 

Connect-MongoDB-Shell

 

How To Create MongoDB Admin User

Let’s now switch gears and create an Admin user. It’s always good advice to create an admin user with elevated privileges to perform elevated tasks. To do so, first access MongoDB’s shell:

mongo

To switch to the database admin, type the command below.

> use admin  

Now, create a new MongoDB user by running the code below.

> db.createUser(   {   user: "mongod_admin",   pwd: "[email protected]@2019",   roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]   }  

You will get the output below when all is ok.

Successfully added user: {  	"user" : "mongod_admin",  	"roles" : [  		{  			"role" : "userAdminAnyDatabase",  			"db" : "admin"  		}  	]  }    

Create-Admin-User

 

Recommended Article: How To Install MongoDB 4 In CentOS 8

Next, run the following command to list MongoDB users created.

show users  

How To Configure Authentication for MongoDB

As you see, all users can access the shell and execute any commands, which is not recommended at all for security purposes. So you need to create authentication for the admin user we just created so as to prevent other users from running commands without authorization.

To enable authentication to edit the /lib/systemd/system/mongod.service file, under the [Service] section, locate and edit the Environment parameter as shown:

Environment="OPTIONS= --auth -f /etc/mongod.conf"  

 

Enable-Authentication

 

Now, you can save and exit the configuration file.

For the changes to come into effect, reload the system and restart MongoDB.

systemctl daemon-reload
systemctl restart mongod

If you now try listing the users without authentication, you should get an error as shown:

 

Check-MongoDB-Authentication

 

To authenticate, simply pass the credentials as shown.

> db.auth('mongod_admin', '[email protected]@2019')  

Now you can run any command after that. Let’s try listing the users once more:

> show users

This time, all went well since the authentication credentials were provided.

 

Verify-User-Authentication

 

Run the command below to exit the database engine:

exit

Conclusion

In this article, you succeeded to learn How to install MongoDB 4 in CentOS 8, and from now on we hope you are comfortable installing MongoDB4 on your CentOS 8 system and getting started with a few necessary steps. You can refer to the articles How to install MongoDB 4 on Debian 10 and How to install MongoDB on Windows.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.
  • Alison Khouth
    Alison Khouth
    4 months ago

    I need to undo uninstalling MogoDB.

    Reply
    • Dani
      Dani
      4 months ago

      Unfortunately it is not possible. The process you have done removed the configuration, and all databases. So this is not reversible. Next time, ensure back up all of your configuration and data before proceeding.

      Reply
  • Miguel Goodman
    Miguel Goodman
    4 months ago

    good tutorial.i wanna remove packages and data. thanks

    Reply
    • Dani
      Dani
      4 months ago

      You can use the following command to remove all packages you have installed.

      sudo yum erase $(rpm -qa | grep mongodb-org)

      And also to remove MongoDB database and log file run the commands below:
      sudo rm -r /var/log/mongodb
      sudo rm -r /var/lib/mongo

      Reply
  • Steve Salle
    Steve Salle
    4 months ago

    is it possible to stop MongoDB?

    Reply
    • Dani
      Dani
      4 months ago

      Yes it is. Stop the MongoDB process by running the command below:
      sudo systemctl stop mongod

      Reply
  • Billy Barrett
    Billy Barrett
    4 months ago

    thank you. After starting the MongoDB service, I received an error that the unit is not found.

    Reply
    • Dani
      Dani
      4 months ago

      – In this situation, run the following command and then the second one (start MongoDB)
      sudo systemctl daemon-reload
      sudo systemctl start mongod

      Reply
  • Dean Ferland
    Dean Ferland
    4 months ago

    What if we need to manage its service?

    Reply
    • Dani
      Dani
      4 months ago

      You can manage the MongoDB service by running the following command:
      sudo systemctl status mongod
      Note: When you see ‘’inactive’’ in the output, It means MongoDB service is not running. To ensure that MongoDB service is automatically starting on system boot, you must see the ‘’enable’’ in output.

      Reply

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+18054214518

7 days a week, 24 hours a day