Tag Archives: MongoDB

Install and configure MongoDB and PHP driver with MacPorts

Installing and configuring MongoDB with MacPorts

Installing MongoDB with MacPorts is as easy as running this command:

$ sudo port install mongodb

MongoDB uses /data/db as the default path for storing its data files, but it’s likely that your won’t have that folder and running MongoDB server (mongod) will end with this error:

*********************************************************************
 ERROR: dbpath (/data/db/) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************

There are several options:

Create that folder and configure it with the correct permissions:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db

Supply another path as the data storage folder with the --dbpath option:

$ mongodb --dbpath /var/lib/mongo

And the best of all, create the necessary paths for data and log files and also the configuration file to specify those paths:

$ sudo mkdir /opt/local/var/db/mongodb
$ sudo mkdir /opt/local/var/log/mongodb
$ sudo touch /opt/local/var/log/mongodb/mongodb.log
$ sudo mkdir /opt/local/etc/mongodb/

Add this to /opt/local/etc/mongodb/mongodb.conf file:

# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /opt/local/var/db/mongodb

# Only accept local connections
bind_ip = 127.0.0.1

# Running as daemon
fork = true

# Take log
logpath = /opt/local/var/log/mongodb/mongodb.log
logappend = true

You can then run mongod specifying that configuration file:

$ sudo mongod -f /opt/local/etc/mongodb/mongodb.conf

An easier way is to use the load/unload option that comes with MacPorts that defines all those configuration values in /opt/local/etc/LaunchDaemons/org.macports.mongodb/org.macports.mongodb.plist file. You can load and unload MongoDB with those commands (using them will make MongoDB autoload on boot!):

$ sudo port load mongodb
$ sudo port unload mongodb

More configuration options: http://docs.mongodb.org/manual/reference/configuration-options/

Installing the PHP driver for MongoDB

Before installing the driver we need to be sure that we have pear and pecl installed. Run this commands to see if they return some value:

$ pear version
$ pecl version

Now we have to install the PHP driver with pecl:

$ sudo pecl install mongo

When it finishes the installation you will see something like this:

Build process completed successfully
Installing '/opt/local/lib/php/extensions/no-debug-non-zts-20090626/mongo.so'
install ok: channel://pecl.php.net/mongo-1.3.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

That last line means that you have to edit yourself the php.ini file, which in my case is in /opt/local/etc/php5/php.ini, and add the extension load for mongo. You can add that line at the end of the file but it is supposed to be in the “Dynamic Extensions”.

Now restart Apache and you’ll be ready to use MongoDB in PHP.

$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start

Ref: http://api.mongodb.org/wiki/current/PHP%20Language%20Center.html
http://noteit.jiaeil.com/install-mongodb-via-macports-on-mac
http://stackoverflow.com/questions/8029064/new-to-mongodb-can-not-run-command-mongo