How to install and configure MongoDB on CentOS 7

MongoDB is a free and open source document database. MongoDB belongs to the NoSQL database family, which differs from traditional table-based SQL databases like MySQL and PostgreSQL.

In MongoDB, data is stored in flexible documents, such as JSON where fields can vary from document to document. Databases do not require a predefined chart and data structures can change over time.

In this tutorial, we’ll cover the process of installing and configuring the latest version of MongoDB Community Edition on CentOS 7 from the official MongoDB repository.

Before proceeding with this tutorial, make sure that you are logged in as a user with sudo privileges.

Install MongoDB

At the time of writing this article, the most recent version of MongoDB is version 4.0.

Before proceeding with the installation wizard, go to the MongoDB documentation page under the Red Hat Installation section and check if a new version is available.

The following steps describe how to install MongoDB on CentOS 7:

  1. Enable MongoDB repository,

    To add the MongoDB repository to the system, open a text editor and create a new YUM repository configuration file named mongodb-org.repo in the directory /etc/yum.repos.d/:

    sudo nano /etc/yum.repos.d/mongodb-org.repo

    And add the following code:

    name=MongoDB Repository

    To install an older version of MongoDB, just replace it 4.0 With the version you choose.

  2. Install MongoDB

    Now that the repository is enabled, you can install the descriptive package mongodb-org Using the yum tool:

    sudo yum install mongodb-org

    The following packages will be installed on your system as part of the packages mongodb-org:

    • mongodb-org-servermongod Daemon and appropriate script initialization.
    • mongodb-org-mongos – A demon mongos.
    • mongodb-org-shell Mongo shell is MongoDB’s JavaScript interface. This shell is used to perform administrative tasks via the command line.
    • mongodb-org-tools It contains many MongoDB tools for importing and exporting data, statistics, and other utilities.
  3. Start MongoDB

    After the installation completes, run the MongoDB daemon and enable it to start up by typing:

    sudo systemctl start mongod
    sudo systemctl enable mongod
  4. Verify your installation

    To check if the installation completed successfully, we will contact the MongoDB database server with a tool mongo And see the server version in action:


    Once in the MongoDB shell, type the following command that will display the MongoDB version:


    The output will look like this:


Configure MongoDB

You can configure an instance of MongoDB by editing the configuration file /etc/mongod.conf Written in YAML.

See also  A tutorial on using the timeout command in Linux

The default configuration settings should be suitable for most users. However, for a production environment, it is advised to slightly modify the safety portion and enable authorization as shown below:

sudo nano /etc/mongod.conf
  authorization: enabled

Selection authorization Make it possible Role-based access control (RBAC) that governs user access to database resources and processes. If this option is disabled, then every user will have access to all databases and take any action.

After making the changes to the MongoDB configuration file, restart the service mongod By command:

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.0, visit the configuration documentation page.

Create MongoDB admin user

If you enable MongoDB authentication, create a MongoDB admin user that will be used to access and manage the MongoDB instance.

First, access the mongo shell by:


Once in the MongoDB shell, type the following command to connect to the database admin:

use admin
switched to db admin

Use the following command to create a new user named mongoAdmin With a role userAdminAnyDatabase:

    user: "mongoAdmin", 
    pwd: "gantidenganpassword", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"

You can rename MongoDB admin user as you wish.

Exit the mongo shell by typing:


To test the changes, access the mongo shell using the administrative user you created earlier:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin

Now try to view the user with the command:

show users
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
	"mechanisms" : [

You can also try to access mongo shell without arguments (just type mongo) And see if you can create a user list using the same command as above.

See also  How to display line numbers in Vim / Vi


You learned how to install and configure MongoDB 4.0 on your CentOS 7 server.


Source link

Originally posted 2020-11-19 05:05:03.

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply