Table of Contents

Installing packages

The installation of Mongo DB is pretty straight forward. You can install a metapackage which will automatically install all necessary packages.

Configure YUM repository

The YUM repository for the MongoDB which I have used is the following:

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

Installation

The installation of the metapackage is done as follows:

[root@lpara yum.repos.d]# yum -y install mongodb-org.x86_64
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: ftp.hosteurope.de
 * extras: ftp.hosteurope.de
 * updates: ftp.hosteurope.de
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:2.6.12-1 will be installed
--> Processing Dependency: mongodb-org-shell = 2.6.12 for package: mongodb-org-2.6.12-1.x86_64
--> Processing Dependency: mongodb-org-server = 2.6.12 for package: mongodb-org-2.6.12-1.x86_64
--> Processing Dependency: mongodb-org-tools = 2.6.12 for package: mongodb-org-2.6.12-1.x86_64
--> Processing Dependency: mongodb-org-mongos = 2.6.12 for package: mongodb-org-2.6.12-1.x86_64
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:2.6.12-1 will be installed
---> Package mongodb-org-server.x86_64 0:2.6.12-1 will be installed
---> Package mongodb-org-shell.x86_64 0:2.6.12-1 will be installed
---> Package mongodb-org-tools.x86_64 0:2.6.12-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                                   Arch                          Version                           Repository                      Size
================================================================================================================================================
Installing:
 mongodb-org                               x86_64                        2.6.12-1                          mongodb                        4.6 k
Installing for dependencies:
 mongodb-org-mongos                        x86_64                        2.6.12-1                          mongodb                        6.9 M
 mongodb-org-server                        x86_64                        2.6.12-1                          mongodb                        9.1 M
 mongodb-org-shell                         x86_64                        2.6.12-1                          mongodb                        4.3 M
 mongodb-org-tools                         x86_64                        2.6.12-1                          mongodb                         90 M

Transaction Summary
================================================================================================================================================
Install       5 Package(s)

Total download size: 110 M
Installed size: 279 M
Downloading Packages:
(1/5): mongodb-org-2.6.12-1.x86_64.rpm                                                                                   | 4.6 kB     00:00
(2/5): mongodb-org-mongos-2.6.12-1.x86_64.rpm                                                                            | 6.9 MB     00:02
(3/5): mongodb-org-server-2.6.12-1.x86_64.rpm                                                                            | 9.1 MB     00:02
(4/5): mongodb-org-shell-2.6.12-1.x86_64.rpm                                                                             | 4.3 MB     00:01
(5/5): mongodb-org-tools-2.6.12-1.x86_64.rpm                                                                             |  90 MB     00:21
------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                           3.7 MB/s | 110 MB     00:29
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mongodb-org-server-2.6.12-1.x86_64                                                                                           1/5
  Installing : mongodb-org-mongos-2.6.12-1.x86_64                                                                                           2/5
  Installing : mongodb-org-tools-2.6.12-1.x86_64                                                                                            3/5
  Installing : mongodb-org-shell-2.6.12-1.x86_64                                                                                            4/5
  Installing : mongodb-org-2.6.12-1.x86_64                                                                                                  5/5
  Verifying  : mongodb-org-shell-2.6.12-1.x86_64                                                                                            1/5
  Verifying  : mongodb-org-tools-2.6.12-1.x86_64                                                                                            2/5
  Verifying  : mongodb-org-mongos-2.6.12-1.x86_64                                                                                           3/5
  Verifying  : mongodb-org-server-2.6.12-1.x86_64                                                                                           4/5
  Verifying  : mongodb-org-2.6.12-1.x86_64                                                                                                  5/5

Installed:
  mongodb-org.x86_64 0:2.6.12-1

Dependency Installed:
  mongodb-org-mongos.x86_64 0:2.6.12-1            mongodb-org-server.x86_64 0:2.6.12-1            mongodb-org-shell.x86_64 0:2.6.12-1
  mongodb-org-tools.x86_64 0:2.6.12-1

Complete!

In order to start the Mongo DB, you have to issue the following command:

Startup

--/bin/mongod -f /etc/mongod.conf -fork

[root@tain-cx-mdb1 scripts]# ./startMongoDB.sh
about to fork child process, waiting until server is ready for connections.
forked process: 61483
child process started successfully, parent exiting
[root@tain-cx-mdb1 scripts]#
[root@tain-cx-mdb1 scripts]# ps -ef | grep mongod
root      61483      1  0 17:24 ?        00:00:00 /bin/mongod -f /app/mongo/hunterServer/conf/mongo.conf --fork
root      61495  60595  0 17:24 pts/0    00:00:00 grep --color=auto mongod

Connect

You can connect to mongo DB using the following command:

[root@tain-cx-mdb1 scripts]#  mongo localhost:9005
MongoDB shell version: 2.6.12
connecting to: localhost:9005/test
Server has startup warnings:
2018-03-20T17:24:46.332+0100 [initandlisten]
2018-03-20T17:24:46.332+0100 [initandlisten] ** WARNING: Readahead for /var/mongodb/db is set to 4096KB
2018-03-20T17:24:46.332+0100 [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2018-03-20T17:24:46.332+0100 [initandlisten] **          http://dochub.mongodb.org/core/readahead
>
> show dbs
admin  (empty)
local  0.031GB
>

Where the configuration file is:

###mongo.conf file
port = 9005
logpath = /var/mongodb/logs/mongodb.log
logappend = true
pidfilepath = /var/mongodb/run/mongodb.pid
unixSocketPrefix = /var/mongodb/db
dbpath = /app/mongo
smallfiles = true
maxConns = 16000

You can also query all tables/collections within one database as follows:

var collections = db.getCollectionNames();
for(var i = 0; i< collections.length; i++) {    
   print('Collection: ' + collections[i]); // print the name of each collection
   db.getCollection(collections[i]).find().forEach(printjson); //and then print     the json of each of its elements
}