How to Install MongoDB on CentOS 7

Filed Under: MongoDB

In this article, we dive in and see how you can install MongoDB on CentOS 7. MongoDB is a free, flexible and opensource NoSQL database, different from the regular SQL databases like PostgreSQL and MySQL. In MongoDB, data is stored in JSON format and does not require a schema.

Installing MongoDB

At the time of penning down this guide, the latest version of MongoDB was version 4.0. Before proceeding with the installation process, confirm the latest release here.

Step 1: Enabling MongoDB repository

Using your favorite text editor, create a new YUM repository called mongodb-org.repo

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

Next, add the following content.


[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

Step 2: Installing MongoDB

With the MongoDB repository enabled, install MongoDB using the command below.

$ sudo yum install mongodb-org

Sample Output

Yum Install Mongodb CentOS 7

When prompted to import the MongoDB GPG key, type y and press ENTER.

Sample Output

Importing GPG Key For MongoDB

As part of the Mongo-org package, the following packages will also be installed.

  1. mongodb-org-server – This is the mongod daemon, plus corresponding init scripts and configurations.
  2. mongodb-org-mongos – This is the mongos daemon.
  3. mongodb-org-shell – This is the mongo shell, which is an interactive JavaScript interface to MongoDB, used to perform administrative tasks through the command line.
  4. mongodb-org-tools – This package contains several MongoDB tools used for importing and exporting data, statistics, as well as other utilities.

Step 3: Starting MongoDB

With MongoDB successfully installed, start the MongoDB daemon using the command as shown.

$ sudo systemctl start mongod

You can also enable it to start on boot by running the following command.

$ sudo systemctl enable mongod

To confirm that MongoDB daemon is running, execute:

$ sudo systemctl status mongod

Sample Output

Verify Status of Mongod daemon

Awesome, we have successfully installed MongoDB on the CentOS 7 server. Let’s now see how we can configure the database.

Step 4: Configuring MongoDB

For best practices, it’s recommended to have authentication to access the database server, because as it stands, any user can have access to it.

To achieve this, open the /etc/mongod.conf file and uncomment the following lines.


security:
  authorization: enabled

To connect to the MongoDB database run the mongo command below.

$ mongo

logging in to MongoDB

To connect to the admin database, run:

use admin

Sample Output

Use Admin command MongoDB

To create a new user called mongoAdmin with the userAdminAnyDatabase role run:


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

Sample Output


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

To exit from the Mongo shell run:

quit()

To login using the admin user account we just created, run:

$ mongo -u mongoAdmin -p --authenticationDatabase admin

Provide the password and later, you will drop to the Mongo shell.

To display users created in the system, run the command below to switch to the admin user.

use admin

Then run the following command.

show users

Sample Output

Show Users in MongoDB

Wonderful! We have successfully installed MongoDB on CentOS and created an Admin user for authentication.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages