Steps to Install and Set Up MongoDB on CentOS

Install and Set Up MongoDB on CentOS

MongoDB is a free and open-source document-oriented database. It is a scalable and flexible NoSQL database independent of the traditional table-based relational database structure. So, JSON-like documents are used. In this article, you will learn how to install and set up MongoDB on CentOS. It supports most 64-bit versions of Red Hat Enterprise Linux (RHEL), CentOS Linux, Oracle Linux, Rocky Linux, and AlmaLinux on x86_64 architecture.

MongoDB is available in Community and Enterprise server editions to help you store large amounts of data in document storage with dynamic schemas. Since MongoDB does not rely on a predefined schema before adding data to a database, you can change the schema and data structure whenever and as frequently as necessary.

Prerequisites to Install MongoDB on CentOS

To let this tutorial works correctly, provide the options below and move on.

  • A system running Linux VPS.
  • A non-root user with sudo privileges.

Tutorial Install and Set up MongoDB on CentOS

Previously, you got familiar with MongoDB as a popular DBMS in Database Management Systems article. MongoDB is highly available, horizontally scaled, and easy to use as a distributed database. Developers can learn and use the document model of MongoDB easily since it provides all the capabilities needed to meet the most complex requirements at any scale. With indexing, ad hoc queries, and real-time aggregation, MongoDB enables you to access and analyze your data in powerful ways.

Let’s go through the steps of this guide to learn how to install and configure MongoDB on your CentOS server.

Step1. Enable MongoDB Repository

Since the official MongoDB package is not available in the standard CentOS repositories, you need to add the MongoDB repository first. To do this, open your preferred text editor to create a new YUM repository file. Here, we use vim to create a configuration ”mongodb-org.repo” file inside the /etc/yum.repos.d/ directory.

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

Then, add the following information to the file you have just created.

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

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

Obviously, you must replace your considered version of MongoDB if you prefer to install an older version of MongoDB. Finally, press the ESC key and type :wq, and hit ENTER to save the recent changes to the file.

Use the following command to check if the MongoDB repository exists within the yum tool and view the list of enabled repositories.

yum repolist

Step 2. Install MongoDB

In the previous step, you enabled the repository. So, you are ready to install the mongodb-org meta-package. Before that, you are recommended to update your packages.

sudo yum -y update

Now, run the command below to install MongoDB on CentOS using the yum utility:

sudo yum install mongodb-org

During the installation, you will view two prompts that you should choose [y/n]. The first prompt lets the MongoDB packages be installed and the second prompt imports a GPG key. On both of them, type y and then hit the ENTER key.

In this way, the mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools, will be installed as a part of the mongodb-org package.

Step 3. Enable MongoDB Start on Boot

When you are done with the installation procedure, use the commands below to start the MongoDB service.

sudo systemctl start mongod
sudo systemctl enable mongod

You can also use some commands to change the state of the MongoDB service. For example, the following command requests that the mongod process reads the confirmation file /etc/mongod.conf. In this way, changes will be applied with no need to restart.

sudo systemctl reload mongod

To stop all running mongod processes run:

sudo systemctl stop mongod

Step 4. Verify Installation and start Using MongoDB

It makes sense to verify the installation. To do this, connect to the MongoDB database server to view the server version using the mongoutility.

mongo

Now that you are inside the MongoDB shell, you can run the following command to view the MongoDB version.

db.version()

If you are concerned about the interaction with MongoDB from the shell, use the command below to have a list of methods for the db object.

db.help()

The output will look like the following:

interaction with MongoDB

Finally, you can quit the shell while the mongod process is being run in the background.

exit

Step 5. Set up MongoDB

Although the default settings of MongoDB are trustable, you can configure your MongoDB. Especially, for production environments, you are recommended to enable authorization and edit the /etc/mongod.conf configuration file which is written in YAML.

/etc/mongod.conf

security:
  authorization: enabled

RBAC which limits users’ access to database resources and actions, is enabled via the authorization option. Each user will have access to every database and be able to perform any operation if this option is disabled. Restart the mongod service after making modifications to the MongoDB configuration file:

sudo systemctl restart mongod

How to Create Administrative MongoDB User

When MongoDB authentication is enabled, you can create one administrative MongoDB user. In this way, you will be able to access and manage your MongoDB instance. As you learned, you can use the command below to access the mongo shell:

mongo

Run the command below after entering the MongoDB shell to connect to the admin database.

use admin

Once the output says that you are switched to db admin, you are ready to create a new user with the userAdminAnyDatabase role which is called mongoAdmin. However, you can name it as you prefer.

db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Then, you can exit the MongoDB shell:

quit()

Test the Administrative User

To check the changes you have made recently, use the administrative user to access the mongo shell that you have created in the above part.

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin

You can also see if you can list the users using the same commands by entering the mongo shell directly (simply type mongo).

Again, once you are switched to db admin, print the users with:

show users

The output would be like this:

Test Administrative MongoDB User

How to Uninstall MongoDB & Remove MongoDB Packages

The MongoDB applications themselves, the configuration files, and any directories containing data and logs must all be deleted from a system in order for MongoDB to be fully uninstalled. First, stop the mongod process:

sudo service mongod stop

The below command enables you to remove any MongoDB packages that you had previously installed.

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

Also, you can remove MongoDB databases and log files by running:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo

FAQ

Since yum upgrades the packages when a new version becomes available, pin your considered package by adding the exclude directive to your /etc/yum.conf file.

It is a metapackage that automatically installs the component packages.

You can disable Transparent Huge Pages on your system to increase MongoDB installation performance. On CentOS, it will be disabled through a tuned profile configuration.

Conclusion

In this article, you learned Install and Set Up MongoDB on CentOS. After adding a third-party repository to yum, you reviewed how to install and configure MongoDB database on your CentOS server. Finally, by creating an administrative MongoDB user, you can use it to access your MongoDB instance. MongoDB has been built for people building internet and business applications who need to evolve quickly and scale elegantly. Companies and development teams of all sizes use MongoDB for a wide variety of reasons.

If you follow the above steps properly then you can smoothly install without any errors but do not hesitate to contact us if you encounter any problems. Our technical support team will try their best to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked.