Saturday, 17 September 2016

Installing MongoDB on Linux

Article illustrates the step by step procedure, how to install the MongoDB on Linux. MongoDB is one of the popular document based NoSQL database.


Add MongoDB Repository 

Lets login to the server as root user and add the MongoDB repository on our system. Goto repository location 

[root@pract1 ~]# cd  /etc/yum.repos.d

Create the mongodb repository. Goto vi editor to create repository 
[root@pract1 yum.repos.d]# vi  mongodb.repo
Press i  It performs insert operation in the file 

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

Press ESC button then
:wq   
[root@pract1 ~]#
Note:- :wq or :x in editor will save the file and quit the vi editor


Begin MongoDB Installation

Now install the MongoDB by running below mentioned command

[root@pract1 yum.repos.d]# yum install mongodb-org
Loaded plugins: refresh-packagekit, security
mongodb                                  |  951 B     00:00
mongodb/primary                          |  45 kB     00:00
mongodb                                             279/279
Setting up Install Process
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
Is this ok [y/N]: y
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:07
(3/5): mongodb-org-server-2.6.12-1.x86_64.rpm     | 9.1 MB     00:16
(4/5): mongodb-org-shell-2.6.12-1.x86_64.rpm      | 4.3 MB     00:05
(5/5): mongodb-org-tools-2.6.12-1.x86_64.rpm      |  90 MB     01:24
------------------------------------------------------------------------------
Total                                              968 kB/s | 110 MB     01:56
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!
This completes the installation 


Verify the MongoDB installation 

[root@pract1 yum.repos.d]# rpm -ql mongodb-org-server
/etc/init.d/mongod
/etc/mongod.conf
/etc/sysconfig/mongod
/usr/bin/mongod
/usr/share/man/man1/mongod.1
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
/var/run/mongodb


Start the MongoDB service 

Lets check the mongod service if its stopped, start service with the command specified below
[root@pract1 yum.repos.d]# service mongod status
mongod is stopped
[root@pract1 yum.repos.d]# service mongod start
Starting mongod:                                           [  OK  ]
[root@pract1 yum.repos.d]#

Also will check the log if there is any errors reported. Here in the below log everything seems to be fine
[root@pract1 yum.repos.d]# cat /var/log/mongodb/mongod.log
2016-09-16T18:21:09.609+0530 ***** SERVER RESTARTED *****
2016-09-16T18:21:09.612+0530 [initandlisten] MongoDB starting : pid=2791 port=27017 dbpath=/var/lib/mongo 64-bit host=pract1.localdomain
2016-09-16T18:21:09.612+0530 [initandlisten] db version v2.6.12
2016-09-16T18:21:09.612+0530 [initandlisten] git version: d73c92b1c85703828b55c2916a5dd4ad46535f6a
2016-09-16T18:21:09.612+0530 [initandlisten] build info: Linux build5.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2016-09-16T18:21:09.612+0530 [initandlisten] allocator: tcmalloc
2016-09-16T18:21:09.612+0530 [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-09-16T18:21:09.637+0530 [initandlisten] journal dir=/var/lib/mongo/journal
2016-09-16T18:21:09.637+0530 [initandlisten] recover : no journal files present, no recovery needed
2016-09-16T18:21:09.785+0530 [initandlisten] allocating new ns file /var/lib/mongo/local.ns, filling with zeroes...
2016-09-16T18:21:10.037+0530 [FileAllocator] allocating new datafile /var/lib/mongo/local.0, filling with zeroes...
2016-09-16T18:21:10.037+0530 [FileAllocator] creating directory /var/lib/mongo/_tmp
2016-09-16T18:21:10.082+0530 [FileAllocator] done allocating datafile /var/lib/mongo/local.0, size: 64MB,  took 0.043 secs
2016-09-16T18:21:10.087+0530 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2016-09-16T18:21:10.087+0530 [initandlisten]     added index to empty collection
2016-09-16T18:21:10.087+0530 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0  reslen:37 301ms
2016-09-16T18:21:10.092+0530 [initandlisten] waiting for connections on port 27017
2016-09-16T18:22:09.793+0530 [clientcursormon] mem (MB) res:30 virt:456
2016-09-16T18:22:09.793+0530 [clientcursormon]  mapped (incl journal view):160
2016-09-16T18:22:09.793+0530 [clientcursormon]  connections:0


Perform Basic Tasks onMongoDB

In MongoDB we use mongo shell to connect to the MongoDB database. Goto mongo shell as specified below

[root@pract1 yum.repos.d]# mongo
MongoDB shell version: 2.6.12
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
>
********** By default mongo connects to test database. So lets create a new database called mydb
> use mydb
switched to db mydb
>
********** Now create a collection in mydb
> db.createCollection("mycollection");
{ "ok" : 1 }
********** Get the list of collections in the database
> show collections
mycollection
system.indexes
********** We will insert some documents to the collection
> db.mycollection.insert({"Name":"Manjunath","City":"Bangalore"});
WriteResult({ "nInserted" : 1 })
*********** Lets check the documents stored in the collections
> db.mycollection.find();
{ "_id" : ObjectId("57dd17915aaf4c5e06833092"), "Name" : "Manjunath", "City" : "Bangalore" }
********** Below mentioned command will show the list of databases
> show dbs
admin  (empty)
local  0.078GB
mydb   0.078GB
test   0.078GB
>

No comments:

Post a Comment