Install PHP MongoDB (mongo) Driver on Linux, Mac OS X, Windows, UNIX, BSD


PHP MongoDB (Mongo Database) Driver Installation on Linux, UNIX, BSD and Mac OS X

MongoDB PHP driver is very simple install on Linux, UNIX, BSD and Mac OS X. You need just PEAR (PHP Extension and Application Repository) with PECL repository. Normally PHP development (dev) package and PHP Pear package installation from package management system is enough to get PEAR working. Also go-pear.php script can be used.

Check that the PEAR and PECL are working with the following commands:

## Check PEAR ##
pear version
 
PEAR Version: 1.9.0
PHP Version: 5.3.1
Zend Engine Version: 2.3.0
Running on: Linux 
 
## Check PECL ##
pecl version
 
PEAR Version: 1.9.0
PHP Version: 5.3.1
Zend Engine Version: 2.3.0
Running on: Linux

Check that Mongo Database Driver found:

pecl search mongo
 
Retrieving data...0%
Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest) Local
mongo   1.0.4 (stable)        Mongo Database Driver

Install Mongo Database Driver with following command:

## Normal user ##
sudo pecl install mongo
## OR as root ##
pecl install mongo
 
downloading mongo-1.0.4.tgz ...
Starting to download mongo-1.0.4.tgz (51,006 bytes)
.............done: 51,006 bytes
16 source files, building
running: phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
building in /var/tmp/pear-build-root/mongo-1.0.4
running: /var/tmp/mongo/configure
[...]
running: make INSTALL_ROOT="/var/tmp/pear-build-root/install-mongo-1.0.4" install
Installing shared extensions:     /var/tmp/pear-build-root/install-mongo-1.0.4/usr/lib/php/modules/
running: find "/var/tmp/pear-build-root/install-mongo-1.0.4" | xargs ls -dils
2654249   4 drwxr-xr-x. 3 root root   4096 2010-03-05 15:06 /var/tmp/pear-build-root/install-mongo-1.0.4
2654282   4 drwxr-xr-x. 3 root root   4096 2010-03-05 15:06 /var/tmp/pear-build-root/install-mongo-1.0.4/usr
2654283   4 drwxr-xr-x. 3 root root   4096 2010-03-05 15:06 /var/tmp/pear-build-root/install-mongo-1.0.4/usr/lib
2654284   4 drwxr-xr-x. 3 root root   4096 2010-03-05 15:06 /var/tmp/pear-build-root/install-mongo-1.0.4/usr/lib/php
2654285   4 drwxr-xr-x. 2 root root   4096 2010-03-05 15:06 /var/tmp/pear-build-root/install-mongo-1.0.4/usr/lib/php/modules
2654281 464 -rwxr-xr-x. 1 root root 468698 2010-03-05 15:06 /var/tmp/pear-build-root/install-mongo-1.0.4/usr/lib/php/modules/mongo.so
 
Build process completed successfully
Installing '/usr/lib/php/modules/mongo.so'
install ok: channel://pecl.php.net/mongo-1.0.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

Note: If PECL runs out of memory, set memory_limit in php.ini at least to 32M.

Add extension=mongo.so to php.ini

## Find php.ini example with locate command ##
locate php.ini
 
## Open php.ini with your favourite text editor (as root) ##
nano -w /etc/php.ini
 
## Add following lines, end of php.ini file ##
# MongoDB Driver
extension=mongo.so

Restart Web Server (as root)

## Example ##
/etc/init.d/apache2 restart
## OR ##
/etc/init.d/httpd restart

And now you are ready to use MongoDB with PHP.

PHP MongoDB (Mongo Database) Driver Installation on Windows

Download MongoDB Windows driver package:
PHP 5.3 VC9 Thread-Safe Mongo extension
PHP 5.3 VC9 Non-Thread-Safe Mongo extension
Need more download options? Check out this and this.

Then unzip package.

copy php_mongo.dll in your PHP extension directory (which is “ext” by default).

Finally restart web server and then you are ready to use MongoDB with PHP on Windows.

Follow If Not True Then False Updates!

7 Comments

  1. Thanks Dude!

  2. Don’t you have to add extension=php_mongo.dll in your php.ini file as well?

    • I forgot to specify: for Windows install.

      • Hi Adrian,

        Yes you are right. I update this guide soon. :)

        • No problem. As an aside, I have an issue trying to run MongoDB as a service on Windows Server 2008 x64. I used the built in install: “mongod -install” It goes through successfully but it fails to start the service. It seems to get stuck in an infinite loop, starting, restarting. Have you come across this? I ended up having to right a start-up script that starts the mongo database manually. However, this requires a user to be logged in at all times, which will be an issue in a system crash/restart.

  3. Another thing to remember is to check where your mongo.so file is. Do a simple:

    locate mongo.so

    and verify the location of your mongo.so file is the same as your php extension folder. I just got done pulling out my hair for about a week with that error.

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. Install PHP MongoDB (mongo) Driver on Linux, Mac OS X, Windows … | Drakz Free Online Service - [...] rest is here: Install PHP MongoDB (mongo) Driver on Linux, Mac OS X, Windows … Share and ...
  2. mongoDB調査メモ | tonbyブログ - [...] Database Driver http://www.if-not-true-then-false.com/2010/03/install-php-mongodb-mongo-driver-on-linux-mac-os-x-win... # pecl search mongo Retrieving data…0%Matched packages, channel pecl.php.net: [...]
  3. Install PHP MongoDB (mongo) Driver on Linux, Mac OS X, Windows … | Sinnerx - [...] original here: Install PHP MongoDB (mongo) Driver on Linux, Mac OS X, Windows … bsd, database, ...
  4. Configuring Mongo for PHP on Ubuntu : The right way « EntropyLess - [...] >> Read the full details here: http://www.if-not-true-then-false.com/2010/install-php-mongodb-mongo-driver-on-linux-mac-os-x-window... [...]