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.

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 =

# 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

Create these aliases to have an smaller command to start and stop mongodb:

alias mongodbstart='sudo mongod -f /opt/local/etc/mongodb/mongodb.conf'
alias mongodbstop="mongo --eval \"db.getSiblingDB('admin').shutdownServer()\""

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:

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

If you use php54 you have to install them like this:

$ sudo port install pear-PEAR

And then add this path to your PATH environment variable:


So in ~/.bash_profile:

export PATH=/opt/local/bin:/opt/local/lib/php/pear/bin:${PATH}

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/'
install ok: channel://
configuration option "php_ini" is not set to php.ini location
You should add "" 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


You might also like

Solve “mongodb cannot be built while v8 is active” in MacPorts
While updating all the ports of macports I got this error:---> Configuring mongodb Error: mongodb...

Install MongoDB in Ubuntu 12.04 with PHP driver
Install MongoDB:sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10 echo...

Install MongoDB and the PHP driver in CentOS
Install MongoDB serverCreate the /etc/yum.repos.d/mongodb-org-3.0.repo file with this content:name=MongoDB...

MongoDB: Recover Data after an Unexpected Shutdown in Ubuntu
If you try to run mongod but it fails to start and have this error in the log file (/var/log/mongodb/mongodb.log)...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.