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.