OS X How to Install MongoDB for PHP

Installing MongoDB

This tutorial will use MacPorts, so please first of all make sure you have XCode installed as it will be needed for MacPorts (and many other things too if you are a developer and you probably are if you are reading this)

You can see how to install MacPorts here

Let's install MongoDB. It's one easy command with MacPorts


$ sudo port install mongodb

***If you had MacPorts installed before, consider updating it

Create Mongo config file


$ sudo vim /etc/mongodb.conf

*** you can use nano instead of vim if you prefer

sudo nano /etc/mongodb.conf

Paste the following into the

mongodb.conf

and save the file.

```bash # This is an example config file for MongoDB. # Place it at /etc/mongodb.conf # Based on a sample provided at # http://www.mongodb.org/display/DOCS/File+Based+Configuration dbpath = /var/lib/mongodb bind_ip = 127.0.0.1 noauth = true ```

After you are finished installing MongoDB via MacPorts, you should see the following notice:

```bash ########################################################### # A startup item has been generated that will aid in # starting mongodb with launchd. It is disabled # by default. Execute the following command to start it, # and to cause it to launch at startup: # # sudo port load mongodb ########################################################### ```

so running

```bash $ sudo port load mongodb ```

should get you setup with automatic start of mongo.

To start it manually you should create the following file:

/Library/LaunchDaemons/org.mongo.mongod.plist

and populate it with the following:

```xml <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> Labelorg.macports.mongodb ProgramArguments /opt/local/bin/daemondo --label=mongodb --start-cmd sudo -u _mongo /opt/local/bin/mongod --dbpath /opt/local/var/db/mongodb --logpath /opt/local/var/log/mongodb/mongodb.log --logappend ; --pid=exec Debug Disabled KeepAlive ```

to launch MongoDB on demand you will need run:

```bash $ sudo launchctl load /Library/LaunchDaemons/org.mongo.mongod.plist ```

once you've done the above and MongoDB is either autostarted or you created the start script and started it manually, you should be able to run

mongo

to get to the MongoDB shell:
MongoDB shell

Installing MongoDB PHP Driver

Installing wget

```bash $ sudo port install wget ```

Installing PHP PEAR

```bash $ cd /tmp $ wget http://pear.php.net/go-pear.phar $ sudo php -d detect_unicode=0 go-pear.phar ```

Configuring PHP PEAR

You will see the following prompt:

       1. Installation base ($prefix)                   : /Users/Admin/pear
       2. Temporary directory for processing            : /tmp/pear/install
       3. Temporary directory for downloads             : /tmp/pear/install
       4. Binaries directory                            : /Users/Admin/pear/bin
       5. PHP code directory ($php_dir)                 : /Users/Admin/pear/share/pear
       6. Documentation directory                       : /Users/Admin/pear/docs
       7. Data directory                                : /Users/Admin/pear/data
       8. User-modifiable configuration files directory : /Users/Admin/pear/cfg
       9. Public Web Files directory                    : /Users/Admin/pear/www
      10. Tests directory                               : /Users/Admin/pear/tests
      11. Name of configuration file                    : /Users/Admin/.pearrc

      1-11, 'all' or Enter to continue:

Type 1 and then type /usr/local/pear

You will get the previous prompt but with different path values

       1. Installation base ($prefix)                   : /usr/local/pear
       2. Temporary directory for processing            : /tmp/pear/install
       3. Temporary directory for downloads             : /tmp/pear/install
       4. Binaries directory                            : /usr/local/pear/bin
       5. PHP code directory ($php_dir)                 : /usr/local/pear/share/pear
       6. Documentation directory                       : /usr/local/pear/docs
       7. Data directory                                : /usr/local/pear/data
       8. User-modifiable configuration files directory : /usr/local/pear/cfg
       9. Public Web Files directory                    : /usr/local/pear/www
      10. Tests directory                               : /usr/local/pear/tests
      11. Name of configuration file                    : /Users/Admin/.pearrc

      1-11, 'all' or Enter to continue:

Type 1 and then type /etc/pearrc

Now type the script will ask whether you want to change your php.ini. Answer Yes to this.

Now you need to run:

```bash $ sudo /usr/local/pear/bin/pecl install mongo ```

Configure the system PHP to load MongoDB extension.
Add this line to your php.ini:

extension=mongo.so

Restart Apache:

```bash $ sudo apachectl graceful ```

You should be all set to go... Enjoy!