Install MongoDB 2.4.4 on Fedora 18/17, CentOS/Red Hat (RHEL) 6.4/5.9


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
Follow If Not True Then False Updates!

28 Comments

  1. I followed these instructions exactly, no errors or warnings from any of the commands, but the file /etc/sysconfig/mongod was never created. Do I need to create it manually? If so, what should the contents be? Thanks.

    • Hi Mark,

      The /etc/sysconfig/mongod file should not be created.

      You possibly mean or try to find /etc/sysconfig/iptables, /etc/mongod.conf or /etc/init.d/mongod file?

  2. @JR/Mark The issue he referes to is this:

    [root@main ~]# service mongod start
    /etc/init.d/mongod: line 24: /etc/sysconfig/mongod: No such file or directory
    Starting mongod: warning: some regex utf8 things will not work. pcre build doesn’t have –enable-unicode-properties
    forked process: 13626
    all output going to: /var/log/mongo/mongod.log
    [root@main ~]# [ OK ]

    Though it seems to start okay.

  3. /etc/init.d/mongod: line 25: /etc/sysconfig/mongod: No such file or directory
    Starting mongod: runuser: user mongod does not exist

    same

  4. /etc/init.d/mongod: line 25: /etc/sysconfig/mongod: No such file or directory
    Starting mongod: runuser: user mongod does not exist

    get this error on service mongod start

    PLEASE HELP?

  5. Hi,

    Everybody with following problem:

    /etc/init.d/mongod: line 25: /etc/sysconfig/mongod: No such file or directory
    Starting mongod: runuser: user mongod does not exist

    Old repositories caused this problem (they worked nice with 1.2.x versions, but not with 1.4.x versions and now this guide installs 1.6.x version), I updated this guide to use MongoDB own repositories and now I tested this new installation method on different systems and it worked smoothly.

    So if you have old packages installed then simply remove them, with following command:

    yum remove mongo mongo-server

    And then follow this guide.

  6. tks

  7. Thanks so much for this guide. I was really stuggling to install mongodb on centos5.5 until I followed these instruction. Amazing! Works great! Persistent connection that autostarts on server reboot – fantastic!

    Only one thing – on the last step (4.2) I get this:

    Flushing firewall rules: [ OK ]
    Setting chains to policy ACCEPT: mangle filter nat [ OK ]
    Unloading iptables modules: [ OK ]
    Applying iptables firewall rules: iptables-restore: line 1 failed [FAILED]

    Is this a problem? I added the line exactly as shown “-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 27017 -j ACCEPT”

    Thanks

  8. Thanks for this guide. It was very helpful. Just one thing.. I run whm/cpanel on my server and mongodb was throwing errors about memory all over the place. I managed to sort this by disabling “Shell Fork Bomb Protection”.

    I was wondering if there is a way to enable fork bomb protection and keep mongo happy?

    • Hi Marc,

      I’m not sure is it possible to use “Shell Fork Bomb Protection” simultaneously with MongoDB, but maybe you could use /etc/security/limits.conf file to limit certain users or groups processes, like following:

      <domain>        <type>  <item>  <value>
      someuser        hard    nproc     20
      @somegroup      hard    nproc     15
  9. you might want to modify your /etc/init.d/mongod script as follows, to avoid mongod not being able to start if you have a different dbpath location in your config file:

    add these two lines towards the top of the /etc/init.d/mongod file:

    MONGO_DBPATH=`grep ‘^dbpath’ /etc/mongod.conf | cut -d’=’ -f2` # get dbpath from config
    MONGO_LOCKFILE=”$MONGO_DBPATH/mongod.lock”

    replace the line containing “killproc” with:

    killproc -p $MONGO_LOCKFILE -t30 -TERM /usr/bin/mongod

    • hi tilo,

      Good Afternoon, when I used your instruction then after I restart mongod or check status I got this message. See below
      [root@ip-10-174-15-18 ~]# service mongod status
      cut: the delimiter must be a single character
      Try `cut –help’ for more information.
      [root@ip-10-174-15-18 ~]# /etc/init.d/mongod start
      cut: the delimiter must be a single character
      Try `cut –help’ for more information.
      Starting mongod: [ OK ]
      forked process: 22975

      please help thanks a lot..

      • Hi Karl,

        I’m not Tilo and if you check Tilo’s comment time, I think that he’s not answering your question very soon.

        But yes, I’m not huge fan of custom hacks to init scripts, but I think that you just have “wrong” single quotes on you config, it should be:

        MONGO_DBPATH=`grep '^dbpath' /etc/mongod.conf | cut -d'=' -f2`
  10. Hi, i’m getting this error:

    MongoDB shell version: 1.8.1
    connecting to: 87.106.244.253:27017/test
    Thu May 19 15:36:29 Error: couldn’t connect to server 87.106.244.253:27017 shell/mongo.js:81
    exception: connect failed

  11. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT

    This configuration is not working in my fedora 15. Someone clue what the problem is?

    • Hi Roberto,

      Could you post your /etc/sysconfig/iptables file content?

    • Please make sure the “accept” line is before “commit” line **AND** all “reject” lines.

  12. @colin you need to ensure that you add the chain on top of it on /etc/sysconfig/iptables config file.

    like this :

    # Firewall configuration written by system-config-firewall
    # Manual customization of this file is not recommended.
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :RH-Firewall-1-INPUT - [0:0]
    -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    ...
    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
    COMMIT

  13. Thank you for this guide. Works for me on CENTOS 6. I did not use your iptables command, as this is only accessed from the local host, so I can not confirm that working, but it looks sane.

    Thanks!

  14. thks again

  15. Thanks! Everything worked perfectly for me on CentOS 6.2.

  16. Nice post, helped my setup thanks.

  17. Hi,

    Nice tutorial.
    But I am running into a dependency problem while running the yum installation.
    Error:
    ———————————————————————————————-
    –> Processing Dependency: /bin/sh for package: mongo-10gen-server-2.4.4-mongodb_1.x86_64
    –> Finished Dependency Resolution
    mongo-10gen-server-2.4.4-mongodb_1.x86_64 from 10gen has depsolving problems
    –> Missing Dependency: /bin/sh is needed by package mongo-10gen-server-2.4.4-mongodb_1.x86_64 (10gen)
    Error: Missing Dependency: /bin/sh is needed by package mongo-10gen-server-2.4.4-mongodb_1.x86_64 (10gen)
    —————————————————————————————————
    But I see that sh executable is already present in the /bin directory.
    Could you please help me on this?

    Thanks,
    Deepak

    • Hi Deepak,

      Could you first post output of following commands:

      uname -a
       
      cat /etc/redhat-release
       
      yum repolist
       
      rpm -qa mongo\*
       
      yum list all mongo\*
       
      rpm -qa bash
       
      ls -la /bin/sh
       
      /bin/sh --version
  18. Hi JR,

    Please find the details below:


    $ uname -a
    Linux a26 3.5.0-32-generic #53~precise1-Ubuntu SMP Wed May 29 20:33:37 UTC 2013
    x86_64 x86_64 x86_64 GNU/Linux
    $ cat /etc/redhat-release
    cat: /etc/redhat-release: No such file or directory

    $ yum repolist
    10gen | 951 B 00:00
    10gen 90/90
    repo id repo name status
    10gen 10gen Repository enabled: 90
    repolist: 90

    $ rpm -qa mongo\*
    mongo-10gen-2.4.4-mongodb_1.x86_64

    $ yum list all mongo\*
    Installed Packages
    mongo-10gen.x86_64 2.4.4-mongodb_1 @10gen
    Available Packages
    mongo-10gen-server.x86_64 2.4.4-mongodb_1 10gen
    mongo-10gen-unstable.x86_64 2.5.0-mongodb_1 10gen
    mongo-10gen-unstable-server.x86_64 2.5.0-mongodb_1 10gen
    mongo18-10gen.x86_64 1.8.5-mongodb_1 10gen
    mongo18-10gen-server.x86_64 1.8.5-mongodb_1 10gen
    mongo20-10gen.x86_64 2.0.8-mongodb_1 10gen
    mongo20-10gen-server.x86_64 2.0.8-mongodb_1 10gen

    $ rpm -qa bash

    $ ls -la /bin/sh
    lrwxrwxrwx 1 root root 4 Mar 29 2012 /bin/sh -> dash

    $ /bin/sh --version
    /bin/sh: 0: Illegal option --

  19. JR,

    Thanks for the help.
    I was doing a silly mistake. Got it.
    Thanks,
    Deepak

    • Hi Deepak,

      Okay, nice to hear that you got it working. Btw. this is not best method install mongodb on Ubuntu.

  20. Hello, thanks for great tutorial, I tried alot of tutorials this one is simplest and easiest one.

    Let people know that I was already shut down my iptables due to I have already ddos protection so when I restarted iptables accidently everything stop working, so guys dont forget to check other iptables rules.

    Cheers.

Leave a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks/Pingbacks

  1. Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux … | Software Firewall - [...] original post here: Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux … Share and …
  2. Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux … | TuxWire : The Linux Blog Aggregator - [...] Read the rest here: Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux … [...]
  3. Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux … | Just linux! - [...] See the rest here: Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux … [...]
  4. Jose da Silva » Blog Archive » MongoDB and PHP - [...] You can follow the great step-by-step article over at If Not True Than Else blog. [...]
  5. mongoDB調査メモ | tonbyブログ - [...] Howto Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux http://www.if-not-true-then-false.com/2010/03/howto-install-mongodb-on-centos-linux-and-red-hat-rhel... [...]
  6. Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux | if not … | Software Firewall - [...] more here: Install MongoDB on CentOS Linux and Red Hat (RHEL) Linux | if not … Share …
  7. Eat.Write.Code. » Blog Archive » MongoDB (Sorta Quickstart Guide) - A blog about eating, writing, and coding. And eating while writing code. Plus some other stuff. - [...] to share some information for those of you starting MongoDB to help save you some time. Read this blog …