How to Install MongoDB on CentOS 7
Updated on
•6 min read

MongoDB is a free and open-source document database. It is classified as a NoSQL database which is different than traditional table-based SQL databases like MySQL and PostgreSQL.
In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema and data structure can be changed over time.
In this tutorial, we will show you how to install and configure MongoDB Community Edition on a CentOS 7 server from the official MongoDB repositories.
Prerequisites
Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges .
Installing MongoDB
At the time of writing this article, the latest version of MongoDB available from the official MongoDB repositories is version 4.0. Before continuing with the next step visit the Install on Red Hat section of MongoDB’s documentation and check if there is a new release available.
Follow the steps below to install the latest stable version of MongoDB on your CentOS server :
Enabling MongoDB repository
To add the MongoDB repository to your system, open your text editor and create a new YUM repository configuration file named
mongodb-org.repoinside the/etc/yum.repos.d/directory:/etc/yum.repos.d/mongodb-org.repo[mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.ascIf you want to install an older version of MongoDB, replace each instance of
4.0with your preferred version.Installing MongoDB
Now that the repository is enabled you can install the
mongodb-orgmeta-package using the yum utility:sudo yum install mongodb-orgDuring the installation yum will prompt you to import the MongoDB GPG key. Type
yand hitEnter.The following packages will be installed on your system as a part of the
mongodb-orgpackage:mongodb-org-server- Themongoddaemon, and corresponding init scripts and configurations.mongodb-org-mongos- Themongosdaemon.mongodb-org-shell- The mongo shell, an interactive JavaScript interface to MongoDB, used to perform administrative tasks thought the command line.mongodb-org-tools- Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.
Starting MongoDB
Once the installation is completed, start the MongoDB daemon and enable it to start on boot by typing:
sudo systemctl start mongodsudo systemctl enable mongodVerifying MongoDB Installation
To verify the installation we will connect to the MongoDB database server using the
mongotool and print the server version:mongoOnce you are inside the MongoDB shell type the following command which will display the MongoDB version:
db.version()The output will look like the following:
4.0.1
Configuring MongoDB
You can configure your MongoDB instance by editing the /etc/mongod.conf configuration file which is written in YAML
.
The default configuration settings are sufficient in most cases. However, for production environments we recommend uncommenting the security section and enabling authorization as shown below:
security:
authorization: enabled
The authorization option enables Role-Based Access Control (RBAC)
that regulates users access to database resources and operations. If this option is disabled each user will have access to any database and will be able to execute any action.
After making changes to the MongoDB configuration file, restart the mongod service:
sudo systemctl restart mongodTo find more information about the configuration options available in MongoDB 4.0 visit the Configuration File Options documentation page.
Creating Administrative MongoDB User
If you enabled the MongoDB authentication, create one administrative MongoDB user that you will use to access and manage your MongoDB instance.
First access the mongo shell with:
mongoOnce you are inside the MongoDB shell type the following command to connect to the admin database:
use admin
switched to db admin
Create a new user named mongoAdmin with the userAdminAnyDatabase role:
db.createUser(
{
user: "mongoAdmin",
pwd: "changeMe",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Successfully added user: {
"user" : "mongoAdmin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
Exit the mongo shell with:
quit()
To test the changes, access the mongo shell using the administrative user you have previously created:
mongo -u mongoAdmin -p --authenticationDatabase adminuse admin
switched to db admin
Now, print the users with:
show users
{
"_id" : "admin.mongoAdmin",
"user" : "mongoAdmin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.
Conclusion
You have learned how to install and configure MongoDB 4.0 on your CentOS 7 server.
You can consult The MongoDB 4.0 Manual for more information on this topic.


