Linux, Windows, Virtualization, OpenSource & Blogging

How to Install MongoDB 3.4 on CentOS 7 / RHEL 7

0
Install MongoDB 3.4 on CentOS 7
Install MongoDB 3.4 on CentOS 7

MongoDB is an open source, document-oriented database designed with both scalability and developer agility in mind. Instead of storing your data in tables and rows as you would with a relational database, in MongoDB you store JSON-like documents with dynamic schemas.

The goal of MongoDB is to bridge the gap between key-value stores (which are fast and scalable) and relational databases (which have rich functionality). Binaries are available for Windows, Linux, OS X, and Solaris, MongoDB is available for free under the GNU Affero General Public License. The language drivers are available under an Apache License. Also, 10gen offers commercial licenses for MongoDB.

Features of MongoDB:

  • Collection oriented storage – easy storage of object-style data
  • Full index support, including on inner objects
  • Query profiling
  • Replication and fail-over support
  • Efficient storage of binary data including large
  • objects (e.g., videos)
  • Auto-sharding for cloud-level scalability (Q209)
  • High performance, scalability, and reasonable depth of functionality are the goals for the project.

Here is the tutorial about installing latest MongoDB ( As of now MongoDB 3.4 ) on CentOS 7 / RHEL 7.

Switch to the root user.

$ su

Add MongoDB repository

MongoDB does not exist on base repositories of CentOS / RHEL. But, MongoDB maintains a dedicated repository. Let us add it to our server.

vi /etc/yum.repos.d/mongodb.repo

Add below information to the above repo file.

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

Visit MongoDB for updated repository information.

Install MongoDB

Once you have added MongoDB repository, run the following command to install MongoDB.

yum -y install mongodb-org

mongob-org, a meta package that installs below four components.

mongodb-org-server – The server daemon (mongod) with init scripts and configurations.

mongodb-org-mongos – The MongoDB Shard daemon

mongodb-org-shell – The MongoDB shell, a command line interface.

mongodb-org-tools – Contains MongoDB tools for import, export, restore, dump, and other functions.

Linux ulimit Settings

By default, most of the systems have low default ulimit values for process, open files, memory, etc and these settings may cause issues in MongoDB operation. So, we need to increase these values for better operation. To do that,

Create a limit file inside /etc/security/limits.d directory.

vi /etc/security/limits.d/99-mongodb-nproc.conf

Set the highest ulimit value for mongo user.

mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 64000
mongod hard nproc 64000

SELinux

We recommend you to disable SELinux on CentOS 7 / RHEL 7. If you still want to use SELinux then run the following command to enable access.

semanage port -a -t mongod_port_t -p tcp 27017

Control MongoDB service

To start MongoDB service, run:

systemctl start mongod

To stop MongoDB service, run:

systemctl stop mongod

To restart MongoDB service, run:

systemctl restart mongod

To check the status MongoDB service, run:

systemctl status mongod

Output:

 mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2017-09-14 11:31:15 EDT; 1s ago
     Docs: https://docs.mongodb.org/manual
  Process: 5898 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 5896 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 5894 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 5903 (mongod)
   CGroup: /system.slice/mongod.service
           └─5903 /usr/bin/mongod -f /etc/mongod.conf

Sep 14 11:31:15 server.itzgeek.local systemd[1]: Starting High-performance, schema-free document-oriented database...
Sep 14 11:31:15 server.itzgeek.local systemd[1]: Started High-performance, schema-free document-oriented database.
Sep 14 11:31:15 server.itzgeek.local mongod[5900]: about to fork child process, waiting until server is ready for connections.
Sep 14 11:31:15 server.itzgeek.local mongod[5900]: forked process: 5903

Check MongoDB version.

mongod --version

Output:

db version v3.4.9
git version: 876ebee8c7dd0e2d992f36a848ff4dc50ee6603e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

Use netstat command to check whether the MongoDB is listening on port “27017”

netstat -antup | grep -i 27017

Output:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      5903/mongod

READ: netstat command not found on CentOS 7 / RHEL 7 – Quick Fix

Access MongoDB

Connect to MongoDB shell by entering the following command.

mongo

Ouuput:

MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-09-14T11:31:16.094-0400 I CONTROL  [initandlisten]
2017-09-14T11:31:16.094-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-09-14T11:31:16.094-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-09-14T11:31:16.094-0400 I CONTROL  [initandlisten]
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten]
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten]
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten]
>

Fix MongoDB errors

Error 1

2017-09-14T11:31:16.094-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-09-14T11:31:16.094-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

To resolve this issue, edit the MongoDB configuration file and enable Access control.

vi /etc/mongod.conf

Add below lines to mongod.conf file.

security:
  authorization: enabled

Error 2

2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten]
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-09-14T11:31:16.095-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

To clear this issue, you need to disable transparent huge pages.

Restart the MongoDB service.

systemctl restart mongod

Access the MongoDB now.

mongo

Output:

MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
>

That’s all. You now have MongoDB installed on your machine.

POSTS YOU MAY LIKE -:)

You might also like

How to Install MongoDB 3.4 on CentOS 7 / RHEL 7

0