Installing MongoDB on CentOS 7

Installing MongoDB on CentOS 7


MongoDB is a database engine that provides access to non-relational, document-oriented databases. It is known as a NoSQL database, because it is not based on a conventional table-oriented relational database framework.

Unlike relational databases, before you add data to a database, MongoDB doesn’t require a predefined schema. You can alter the schema at any time and as often as is necessary without having to setup a new database with an updated schema.

Add MongoDB Yum Repository

Create a new file, vi /etc/yum.repos.d/mongodb-org-4.2.repo so that you can install the latest release using yum. Add the following contents to the file:

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

Install MongoDB

sudo yum install mongodb-org

Configure MongoDB

The configuration file for MongoDB is located at /etc/mongod.conf, and is written in YAML format.

/etc/mongod.conf

security:
authorization: enabled

Start and Stop MongoDB

Start the MongoDB service with the systemctl utility:

sudo systemctl start mongod

To restart the mongod process

sudo systemctl restart mongod

The stop command halts all running mongod processes.

sudo systemctl stop mongod

MongoDB can also be started on boot:

sudo systemctl enable mongod

Create Database Users

1. Open the mongo shell

mongo

2. By default, MongoDB connects to a database called test. Before adding any users, create a database to store user data for authentication:

use admin

3. Use this command to create an administrative user with the ability to create other users on any database. For better security, change the values mongo-admin and password:

db.createUser({user: “mongo-admin”, pwd: “password”, roles:[{role: “userAdminAnyDatabase”, db: “admin”}]})

Hold these credentials for future reference in a secure spot. All the information will be displayed to the database except the password.

4. Exit the mongo shell:

quit()

Backup and restore MongoDB Database

We use these tools:

  • mongodump
  • mongorestore

Command to mongodb backup

The mongodump command will create a backup / dump of the MongoDB database with the name specified by the --db [DB NAME] argument.

The --out /var/backups/`date +"%Y%m%d" argument specifies the output directory as /var/backups/[TODAY'S DATE] e.g. /var/backups/20190903

sudo mongodump –db [DB NAME] –out /var/backups/date +”%Y%m%d”

It assumes that you have a running mongodb instance on port 27017.

Mongodb backup when database is on remote server or port is different on localhost

mongodump –host <hostname>: –db <source database name> –authenticationDatabase <source database name> -u <username> -p <password>

Commands to restore mongodb database

sudo mongorestore –db [DB NAME] /var/backups/[BACKUP FOLDER NAME]

Thankyou.

    • Related Articles

    • How to Connect Node.js Application with MongoDB on CentOS

      This article will help you to connect Node.js application with MongoDB. Also, configure MongoDB drive for nodejs using Mongoose node application on CentOS and Redhat systems. Prerequsities We assume that Node.js and MongoDB are already installed on ...
    • How to Install and Configure PowerDNS on centos 7 using MariaDB.

      PowerDNS is a DNS server that works on many derivatives from Linux / Unix. It can be configured with various backends, including zone files of the BIND style, relational databases and failover / load balancing. The DNS recurrent can also be set up as ...
    • How To Configure BIND as a Private Network DNS Server on CentOS 7

      A DNS or Domain Name System is a distributed database, allows the association of zone records, for example, IP addresses with domain names. If a computer, like your laptop or phone, has to communicate over the internet, they use each other IP ...
    • Install CWP in Centos 7

      CWP is an free and paid license based hosting panel for managing website database,emails,file,etc using a single Panel. In this article we will discuss how to install CWP in Centos 7 Software Requirements You must have a clean/fresh installation of ...
    • Install VnStat Network Monitoring on CentOS 7

      Console based network traffic monitoring This article will help you install VnStat network monitoring on your CentOS 7 server. VnStat is console based network traffic monitor for Linux. This will help monitor multiple interfaces at the same time. ...