How to Install MongoDB 4 on CentOS 7 / RHEL 7

0

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 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-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/testing/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.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

READ: semanage command not found in CentOS 7 / 6 & RHEL 7 / 6 – Quick Fix

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 - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-06-27 03:57:31 UTC; 10s ago
     Docs: https://docs.mongodb.org/manual
  Process: 1519 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 1517 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 1515 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 1514 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 1522 (mongod)
   CGroup: /system.slice/mongod.service
           └─1522 /usr/bin/mongod -f /etc/mongod.conf

Jun 27 03:57:30 mongoc7 systemd[1]: Starting MongoDB Database Server...
Jun 27 03:57:30 mongoc7 mongod[1519]: 2018-06-27T03:57:30.236+0000 I CONTROL  [main] Automatically disablin...none'
Jun 27 03:57:30 mongoc7 mongod[1519]: about to fork child process, waiting until server is ready for connections.
Jun 27 03:57:30 mongoc7 mongod[1519]: forked process: 1522
Jun 27 03:57:31 mongoc7 systemd[1]: Started MongoDB Database Server.
Hint: Some lines were ellipsized, use -l to show in full.

Check MongoDB version.

mongod --version

Output:

db version v4.0.0-rc7
git version: 7230641bb09b1ceb04c3135cf83a5044c4838906
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      1522/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 v4.0.0-rc7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0-rc7
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: 
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] 
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] 
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] 
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] 
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-06-27T03:57:31.470+0000 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service to collect and display
metrics about your deployment (disk utilization, CPU, operation statistics,
etc).

The monitoring data will be available on a MongoDB website with a unique
URL created for you. Anyone you share the URL with will also be able to
view this page. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command:
db.enableFreeMonitoring()
---

> 

Fix MongoDB errors

Error 1

2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-27T03:57:31.469+0000 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

2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] 
2018-06-27T03:57:31.469+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-06-27T03:57:31.469+0000 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 v4.0.0-rc7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0-rc7
> 

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

Further Reading

You might also like

How to Install MongoDB 4 on CentOS 7 / RHEL 7

0