MONDODB INSTALLATION

MONGODB COMMUNITY EDITION ON CENTOS / REDHAT LINUX

MongoDB is open source document-oriented, NO-SQL database.It uses JSON-like documents with Schemas.

In this tutorial, We are going to install MongoDB Community Edition on Redhat or Centos Linux version 6 and 7 using .rpm packages.

While Redhat has MongoDB packages in its repository by default, We use the Official MongoDB CE Packages to ensure that we have installed the latest version.

Install MongoDB Community Edition

1.Configure the Package Management System (YUM)

Create a file /etc/yum.repos.d/mongodb-org-3.6.repo So that we can directly install MongoDB using Yum.

Use the following repository file:

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

Install the MongoDB Packages

To install the stable version, Run the following command.

#sudo yum install mongodb-org

Start the service

#systemctl start mongo

Check MongoDB is running by checking that the port 27017 is open.

#netstat -plntu

And make sure mongod service is running.

#systemctl status mongod

To make mongodb service UP and running after system reboot,

#chkconfig mongod on

Now Login to MongoDB Shell

#mongo –host 127.0.0.1:27017

To list all the databases:

>show databases

To list all the users:

>show users

To create a new user:

>db.createUser(

… {

… user: “admin”

… pwd: “admin123”,

… roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]

… }
… )

You will see the following output:

To use database or users:

>use database name

>use username

To stop the mongodb service:

#systemctl stop mongod

To Remove/Uninstall MongoDB

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

Remove MongoDB Databases and Log Files

#rm -rf /var/log/mongodb

#rm -rf /var/lib/mongo

That’s it.In this tutorial, We have learned how to install MongoDB Database and how to create and use databases and users in the Mongo shell.