Install MongoDB 2.0.6 on Fedora 17/16, CentOS/Red Hat (RHEL) 6.3/5.8


mongodb-logoThe first part of this article is copied from http://www.if-not-true-then-false.com/2010/install-mongodb-on-fedora-centos-red-hat-rhel/ since we will not reinvent the wheels 🙂

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.0.6 on Fedora 17/16/15/14/13/12, CentOS 6.3/6.2/6.1/6/5.8 and Red Hat (RHEL) 6.3/6.2/6.1/6/5.8. 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 17/16/15/14/13/12, CentOS 6.3/5.8 and Red Hat (RHEL) 6.3/5.8

    1. Switch to root user

      su -
      ## OR ##
      sudo -i
    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

      1. Mongodb-repo for Fedora 17/16/15/14/13/12, CentOS 6.3/5.8 and Red Hat (RHEL) 6.3/5.8 on i686 (32-bit)
        [10gen]
        name=10gen Repository
        baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
        gpgcheck=0
      2. Mongodb-repo for Fedora 17/15/14/13/12, CentOS 6.3/5.8 and Red Hat (RHEL) 6.3/5.8 on x86_64 (64-bit)
        [10gen]
        name=10gen Repository
        baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
        gpgcheck=0
    3. Install mongo server and mongo client packages

      yum install mongo-10gen mongo-10gen-server
  2. Configure MongoDB Database Server

    1. Edit /etc/mongod.conf file:

      nano -w /etc/mongod.conf
    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
    3. Start MongoDB service:

      service mongod start
      ## OR ##
      /etc/init.d/mongod start
    4. Start MongoDB on boot

      chkconfig --levels 235 mongod on
  3. Test MongoDB Server

    1. Open MongoDB Command Line Client

      mongo
    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)

    1. Edit /etc/sysconfig/iptables file:

      nano -w /etc/sysconfig/iptables
    2. Add following line before COMMIT:

      -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
    3. Restart Iptables Firewall:

      service iptables restart
      ## OR ##
      /etc/init.d/iptables restart
    4. Test remote connection:

      mongo server:port/database
      ## Example ##
      mongo localhost:27017/test
  5. Install PHP MongoDB Driver

    1. Check that the PEAR and PECL are working with the following commands:

      [root@webserver ~]# pear version
      PEAR Version: 1.9.4
      PHP Version: 5.3.3
      Zend Engine Version: 2.3.0
      Running on: Linux webserver.younetco.com 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64
      [root@webserver ~]# pecl version
      PEAR Version: 1.9.4
      PHP Version: 5.3.3
      Zend Engine Version: 2.3.0
      Running on: Linux webserver.younetco.com 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64
      [root@webserver ~]#
    2. Check that Mongo Database Driver found:

      pecl search mongo
    3. Install Mongo Database Driver with following command:

      pecl install mongo
      1. If you get:
        sh: phpize: command not found
        ERROR: `phpize' failed

        , install php-devel package:

        yum install php-devel
      2. If you get
        configure: error: in `/var/tmp/pear-build-rooteHkukU/mongo-1.3.4':
        configure: error: no acceptable C compiler found in $PATH
        See `config.log' for more details.
        ERROR: `/var/tmp/mongo/configure' failed

        , install gcc:

        yum install gcc
    4. Add extension=mongo.so to php.ini (we normally create a new file in configuration inclusion path):

      ## vi /etc/php.d/mongo.ini
      
      ## Add the following lines
      # MongoDB Driver
      extension=mongo.so
    5. Restart Web Server (as root):

      ## Example ##
      /etc/init.d/apache2 restart
      ## OR ##
      /etc/init.d/httpd restart
  6. In order to program with MongoDB, we can use some libraries such as Shanty Mongo

About NhocConan

A super lazy guy who tries to write tech blog entries in English.He is lazy, so he can only write when he is in a good mood or when he is tired of coding.

Leave a comment

Your email address will not be published. Required fields are marked *