What is MongoDB?

MongoDB (from “humongous”) is a scalable, high-performance, open source, schema-free, document-oriented database. Written in C++. MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS systems (which provide structured schemas and powerful queries).

MongoDB is very interesting document-oriented database, because it has really awesome features:

  • Document-oriented storage (the simplicity and power of JSON-like data schemas)
  • Dynamic queries
  • Full index support, extending to inner-objects and embedded arrays
  • Query profiling
  • Fast, in-place updates
  • Efficient storage of binary data large objects (e.g. photos and videos)
  • Replication and fail-over support
  • Auto-sharding for cloud-level scalability
  • MapReduce for complex aggregation
  • Commercial Support, Training, and Consulting

This guide shows howto install MongoDB 2.4.4 on Fedora 18/17/16/15/14/13/12, CentOS 6.4/6.3/6.2/6.1/6/5.9 and Red Hat (RHEL) 6.4/6.3/6.2/6.1/6/5.9. Using MongoDB own YUM repositories. Fedora / CentOS / Red Hat (RHEL) RPM packages are currently available for x86 (32-bit) and x86_64 (64-bit) architectures.

1. Install MongoDB on Fedora 18/17/16/15/14/13/12, CentOS 6.4/5.9 and Red Hat (RHEL) 6.4/5.9

1.1 Change to root User

su -
## OR ##
sudo -i

1.2 Add and enable 10gen MongoDB repository

Select suitable repo for your system and add one of following to /etc/yum.repos.d/10gen-mongodb.repo

Mongodb-repo for Fedora 18/17/16/15/14/13/12, CentOS 6.4/5.9 and Red Hat (RHEL) 6.4/5.9 on i686 (32-bit)

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0

Mongodb-repo for Fedora 18/17/15/14/13/12, CentOS 6.4/5.9 and Red Hat (RHEL) 6.4/5.9 on x86_64 (64-bit)

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

1.3 Install mongo server and mongo client packages

Install stable version of MongoDB

yum install mongo-10gen mongo-10gen-server

2. Configure MongoDB Database Server

2.1 Edit /etc/mongod.conf file:

nano -w /etc/mongod.conf

2.2 Check and set basic settings, before starting MongoDB (default settings are good)

logpath=/var/log/mongo/mongod.log
port=27017
dbpath=/var/lib/mongo

2.3 Start MongoDB Server

service mongod start
## OR ##
/etc/init.d/mongod start

2.4 Start MongoDB on boot

chkconfig --levels 235 mongod on

3. Test MongoDB Server

3.1 Open MongoDB Command Line Client

mongo

3.2 Save, Update and Find Some Test Data on MongoDB

> use test
switched to db test
> db.foo.find()
> db.foo.save({a: 1})
> db.foo.find()
{ "_id" : ObjectId("4b8ed53c4f450867bb35a1a9"), "a" : 1 }
> db.foo.update( {a: 1}, {a: 5})
> db.foo.find()
{ "_id" : ObjectId("4b8ed53c4f450867bb35a1a9"), "a" : 5 }

4. Open MongoDB Port (27017) on Iptables Firewall (as root user again)

Edit /etc/sysconfig/iptables file:

nano -w /etc/sysconfig/iptables

4.1 Add following line before COMMIT:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT

4.2 Restart Iptables Firewall:

service iptables restart
## OR ##
/etc/init.d/iptables restart

Note: Open MongoDB port only if you have enabled authentication or operating trusted environment.

4.3 Test remote connection

mongo server:port/database
## Example ##
mongo 10.0.10.45:27017/test