Install MongoDB on CentOS 7 Rumi, July 17, 2021 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.repo inside the /etc/yum.repos.d/ directory: nano /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.asc If you want to install an older version of MongoDB, replace each instance of 4.0 with your preferred version. Installing MongoDB Now that the repository is enabled you can install the mongodb-org meta-package using the yum utility: sudo yum install mongodb-org During the installation yum will prompt you to import the MongoDB GPG key. Type y and hit Enter. The following packages will be installed on your system as a part of the mongodb-org package: mongodb-org-server - The mongod daemon, and corresponding init scripts and configurations. mongodb-org-mongos - The mongos daemon. 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 mongod Verifying MongoDB Installation To verify the installation we will connect to the MongoDB database server using the mongo tool and print the server version: mongo Once 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: nano /etc/mongod.conf 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 mongod To 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: mongo Once 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" } ] } You can name the administrative MongoDB user as you want. 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 admin use 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. Configurations (Linux) CentOSCentOS 7MongoDB