Install MongoDB on Centos / RedHat

by lifeLinux on August 19, 2011

MongoDB (from “humongous”) is an open source, high-performance, schema-free, document-oriented database written in the C++ programming language. The database is document-oriented so it manages collections of JSON-like documents. Many applications can thus model data in a more natural way, as data can be nested in complex hierarchies and still be query-able and indexable.

Install MongoDB

Download the latest binaries from mongodb.org. When downloading MongoDB, ensure that you download a version that is compiled for the proper system architecture. (Please note that the 32-bit version of MongoDB has a database size limit of 2 gigabytes).

# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.8.2.tgz
# tar zxvf mongodb-linux-x86_64-1.8.2.tgz
# mv mongodb-linux-x86_64-1.8.2 /mongodb

Create folder /mongodb/data, it’s used to store all data in MongoDB

# mkdir /mongodb/data

Create the file /mongodb/config and use the following example as a template:

dbpath = /mongodb/data
logpath = /mongodb/mongodb.log
logappend = true
bind_ip = 127.0.0.1
port = 27017
fork = true
auth = true

Create two scripts to start or stop the MongoDB process. To create start script, type the following command

# vi /mongodb/bin/start

Add the following content

#!/bin/sh
/mongodb/bin/mongod --config /mongodb/config \

Create stop script

# vi /mongodb/bin/stop

Add the following content

#!/bin/bash
pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;
if [ "${pid}" != "" ]; then
    kill -2 ${pid};
fi

Set execute permission

# chmod +x /mongodb/bin/start
# chmod +x /mongodb/bin/stop

After, create init script which provides a means for ensuring that MongoDB will start at boot. Type the following command

# vi /etc/init.d/mongodb

Add the following content

#! /bin/sh
#
# mongodb – this script starts and stops the mongodb daemon
#
# chkconfig: - 85 15
# description: MongoDB is a non-relational database storage system.
# processname: mongodb
# config: /opt/config/mongodb
# pidfile: /opt/mongodb/mongo.pid

PATH=/mongodb/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=mongodb

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
        echo -n "Starting MongoDB... "
        su - mongodb -c "/mongodb/bin/start"
        ;;
  stop)
        echo -n "Stopping MongoDB... "
        /mongodb/bin/stop
        ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop}" >&2
            exit 1
            ;;
    esac
exit 0

Install PHP Driver

Once Mongo is installed we need to install the appropriate extension so that we can begin using it from within PHP. Luckily we can find the mongo extension through pecl.

# pecl install mongo

Or you can also compile Mongo driver from the latest source code on Github

# yum install php53-devel
# wget https://github.com/mongodb/mongo-php-driver/tarball/master
# tar zxvf mongodb-mongo-php-driver-*.tar.gz
# cd mongodb-mongo-php-driver-*
# phpize
# ./configure
# make install

Ater that, to load the extension on PHP startup, type the command

# echo "extension=mongo.so" >> /etc/php.ini

{ 1 trackback }

Install MongoDB on Linux Using Yum Command
August 19, 2011 at 6:11 am

{ 0 comments… add one now }

Previous post:

Next post: