How to install MongoDB in CentOS

To install MongoDB in CentOS

MongoDB is an open-source, cross-platform, document-oriented database which stores data in JSON-like documents. The main features of MongoDB are Ahoc queries, indexing, load balancing and file storage. It is the fourth popular database system. Installation of MongoDb in CentOS is explained in this article.

To install MongoDB

Create a repository for the MongoDB

[root@linuxhelp1 ~]# cd /etc/yum.repos.d/
[root@linuxhelp1 yum.repos.d]# vi mongodb.repo 
[mongodb]
name=MongoDB 3.2 Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc


Run the following command to install the MongoDB.

[root@linuxhelp1 ~]# yum install mongodb-org -y 
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
-->  Running transaction check
--->  Package mongodb-org.x86_64 0:3.2.9-1.el7 will be installed
-->  Processing Dependency: mongodb-org-tools = 3.2.9 for package: mongodb-org-3.2.9-1.el7.x86_64
-->  Processing Dependency: mongodb-org-shell = 3.2.9 for package: mongodb-org-3.2.9-1.el7.x86_64
-->  Processing Dependency: mongodb-org-server = 3.2.9 for package: mongodb-org-3.2.9-1.el7.x86_64
-->  Processing Dependency: mongodb-org-mongos = 3.2.9 for package: mongodb-org-3.2.9-1.el7.x86_64
.
.
.
Installed:
  mongodb-org.x86_64 0:3.2.9-1.el7
Dependency Installed:
  mongodb-org-mongos.x86_64 0:3.2.9-1.el7             mongodb-org-server.x86_64 0:3.2.9-1.el7             mongodb-org-shell.x86_64 0:3.2.9-1.el7
  mongodb-org-tools.x86_64 0:3.2.9-1.el7
Complete!


Start and enable the MongoDB service by running the following command.

[root@linuxhelp1 ~]# systemctl start mongod.service
[root@linuxhelp1 ~]# systemctl enable mongod.service 
mongod.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mongod on


Now add the MongoDB port number to the firewall as shown below.

[root@linuxhelp1 ~]# firewall-cmd --zone=public --add-port=27017/tcp --permanent 
success
[root@linuxhelp1 ~]# firewall-cmd --reload 
Success

To launch MongoDB

Connect to the MongoDB from terminal by running the following command.

[root@linuxhelp1 ~]# mongo 
While connecting to MongoDB we may get below warning message
connecting to: test
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:
2016-09-16T04:04:23.640+0530 I CONTROL  [initandlisten]
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ' always' .
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten] **        We suggest setting it to ' never' 
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten]
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ' always' .
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten] **        We suggest setting it to ' never' 
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten]
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2016-09-16T04:04:23.641+0530 I CONTROL  [initandlisten]
> 


Run the following command to resolve " transparent huge page error"

[root@linuxhelp1 ~]# echo never >  /sys/kernel/mm/transparent_hugepage/enabled
[root@linuxhelp1 ~]# echo never >  /sys/kernel//mm/transparent_hugepage/defrag  


To overcome the warning error, open the configuration file and add the following entry into it.

[root@linuxhelp1 ~]# vi /etc/security/limits.d/20-nproc.conf 
mongodsoft  nproc   64000


Now restart the mongodb service by running the following command.

[root@linuxhelp1 ~]# systemctl restart mongod 
try to connect MongoDB using mongo command:
[root@linuxhelp1 ~]# mongo 
MongoDB shell version: 3.2.9
connecting to: test
>  
Tag : Mongodb
FAQ
Q
Does MongoDB support SQL?
A
Not directly, no. However, MongoDB does support a rich query language of its own. For examples on using MongoDB’s query language, see MongoDB CRUD Operations
Q
What does the leaf mean in the MongoDB logo?
A
Our founders believe that coding should be natural, and so should using a database. They want the experience of using MongoDB to be simple and natural. Thus, the leaf.
Q
Is sharding appropriate for a new deployment?
A
Sometimes. However, if your data set fits on a single server, you should begin with an unsharded deployment as sharding while your data set is small provides little advantage .
Q
is mongodb .repo aleady exists?
A
No you need to create and add the configuration
Q
Should I normalize my data before storing it in MongoDB?
A
No. Schema design is very important when using MongoDB, but very different from schema design for relational databases.