Installing the PHP Driver

PECL

You can download and install the driver using PECL by running:

$ pecl install mongo

Add the following line to php.ini:

extension=mongo.so

That's it, you're done!

Windows Install

The precompiled driver is available for the following setups:

VC8 and VC9

Builds for each release are available on Github. They are all build for PHP 5.3, as PHP 5.2 is no longer supported with VC9.

Binaries for the latest code are also available:

VC6

The latest binaries for 5.2 and 5.3 are available at php.net.

Please inquire on the mailing list if you need a different build.

Install

  1. Download and extract the .zip file. Make sure PHP version_ matches the version of PHP you are running (e.g., if you are running PHP 5.2.6, download the driver for PHP 5.2).
  2. Copy php_mongo.dll to your PHP extensions directory (see "extension_dir" in php.ini).
  3. Add a line to php.ini:
    extension=php_mongo.dll

Manual *NIX Install

Go to Github and click the "download" button.  Then run:

$ tar zxvf mongodb-mongodb-php-driver-<commit_id>.tar.gz
$ cd mongodb-mongodb-php-driver-<commit_id>
$ phpize
$ ./configure
$ sudo make install

Make the following changes to php.ini:

  1. The build will display where it is installing the PHP driver with output that looks something like:
    Installing '/usr/lib/php/extensions/no-debug-zts-20060613/mongo.so'

    Make sure that it is the same as the PHP extension directory by runnning:

    $ php -i | grep extension_dir
      extension_dir => /usr/lib/php/extensions/no-debug-zts-20060613 =>
    /usr/lib/php/extensions/no-debug-zts-20060613  

    If it's not, change the extension_dir in php.ini or move mongo.so.

  2. To load the extension on PHP startup, add a line: Github
    extension=mongo.so

System Oddities

OS X

If your system is unable to find autoconf, you'll need to install Xcode (available on your installation DVD).

MAMP and XAMPP

At the moment, MAMP uses PHP 5.2 and XAMPP uses PHP 5.3. Make sure you download the driver from the correct section below, or PHP will complain about the version mismatch.

MAMP and XAMPP

MAMP and XAMPP cannot compile the driver, as they are missing some PHP header files. You can download a precompiled .so of the PHP 5.3 thread safe build from Github.

MAMP Install

Put mongo.so in MAMP's PHP extension directory and add a line to the php.ini used by MAMP that says: extension=mongo.so

XAMPP Install

  • Download and extract the precompiled extension.
  • Move mongo.so to the XAMPP PHP extensions directory (by default, /Applications/XAMPP/xamppfiles/lib/php/php-5.3.0/extensions/no-debug-non-zts-20090626)
  • Open up the php.ini file used by XAMPP in a text editor (by default, the php.ini file is in /Applications/XAMPP/xamppfiles/etc/php.ini)
  • Add a line (anywhere in the file) that says extension=mongo.so
  • Restart (or start) the XAMPP server
  • Point your browser at localhost and select your language, then phpinfo() from the left menu
  • Make sure there's a Mongo section

You should be all set to use Mongo from XAMPP!

Gentoo

On Gentoo using PECL you might get an error that libtool is the wrong version. Compiling from source you'll need to run aclocal and autoconf.

phpize && aclocal && autoconf && ./configure && make && make install

(Thanks to @riklaunim)

Optional Configuration

There are also a number of options that can be configured by adding lines to php.ini:

[mongo]
; If the driver should reconnect to mongo
mongo.auto_reconnect = true

; Whether to allow persistent connections
mongo.allow_persistent = On

; Maximum number of persistent connections (-1 means unlimited)
mongo.max_persistent = -1

; Maximum number of links (persistent and non-persistent, -1 means unlimited)
mongo.max_connections = -1

; Default host for mongo connection
mongo.default_host = www.example.com

; Default port for mongo database
mongo.default_port = 42

; When saving files to the database, size of chunks to split them into
mongo.chunk_size = 1024

; Specify an alternate character to $ to use for special db functions ($set, $push, $exists, etc.)
mongo.cmd = "$"

If you do not add any of the above lines, the defaults are:

mongo.auto_reconnect = Off
mongo.allow_persistent = On
mongo.max_persistent = -1
mongo.max_connections = -1
mongo.default_host = localhost
mongo.default_port = 27017
mongo.chunk_size = 262400

There are some utility PHP scripts at Github .  These can be added to your include path if you would like to use them.  They include:

  1. Auth.php: includes the MongoAuth class, for authenticating database connections
  2. Admin.php: MongoAdmin class, for authenticated connections to the admin database
  3. Util.php: MongoUtil, which contains a number of helpful constants.

Checking Your Installation

To check that the extension is working, restart Apache and create a PHP page containing:

<?php
phpinfo();
?>

Visit the page, scroll down, and you should see a "mongo" section that looks similar to:

Your driver should now be fully armed and operational! See the PHP Tutorial to start using it.


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Comments (9)

IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.

These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group.

 
  1. Oct 20

    Anonymous says:

    When using later versions of Fedora (I am using 11), changes need not be made di...

    When using later versions of Fedora (I am using 11), changes need not be made directly to php.ini.  Instead,

    Change to root.

    Within the /etc/php.d/ directory, create a subdirectory named mongo (or something else you like). 

    In the mongo directory, create a mongo.ini file.

    In the mongo.ini file, write the following code:

    ; Enable mongodb extension module
    extension=mongo.so
    
    ; If the driver should reconnect to mongo
    mongo.auto_reconnect = true
    
    ; Whether to allow persistent connections
    mongo.allow_persistent = On
    
    ; Maximum number of persistent connections (-1 means unlimited)
    mongo.max_persistent = -1
    
    ; Maximum number of links (persistent and non-persistent, -1 means unlimited)
    mongo.max_connections = -1
    
    ; Default host for mongo connection
    mongo.default_host = www.example.com
    
    ; Default port for mongo database
    mongo.default_port = 42
    
    ; When saving files to the database, size of chunks to split them into
    mongo.chunk_size = 1024
    
    ; Specify an alternate character to $ to use for special db functions ($set, $push, $exists, etc.)
    
    mongo.cmd = "$"
    
  2. Nov 23

    Anonymous says:

    i have to use an old version of xampp, due to the php version i need. anyway i c...

    i have to use an old version of xampp, due to the php version i need. anyway i could get a no-debug-non-zts-20060613 version of the mongo.so?

    1. Nov 24

      Kristina Chodorow says:

      In that case, the .so under the MAMP heading will work fine for you.

      In that case, the .so under the MAMP heading will work fine for you.

  3. Dec 06

    Anonymous says:

    I am a junior university student from China. In our database course, our teacher...

    I am a junior university student from China. In our database course, our teacher asked us to use the MongoDB. I want to use the PHP. However, I cannot install the driver in my windows vista computer. Should I just finish the part in "Windows install" on this page? Or should I first do the "PECL", then "Windows install"?

    1. Dec 06

      Kristina Chodorow says:

      For Windows, just download the pre-compiled driver. Ignore the section on PECL.

      For Windows, just download the pre-compiled driver. Ignore the section on PECL.

      1. Dec 07

        Anonymous says:

        Thanks a lot. I've fixed it. New problem occurred. I don't know why it...

        Thanks a lot. I've fixed it. New problem occurred. I don't know why it says "fatal error...MongoGridFSException..." when i try to upload a mp3/avi file using the sample code in "File Upload Tutorial". It works very well with jpg/doc file etc. 

  4. Jan 05

    Anonymous says:

    I had a few errors on my system that may help someone else. On one of my boxes I...

    I had a few errors on my system that may help someone else. On one of my boxes I had a custom install of php (two versions) which would cause the wrong one to get installed. I would get the error...

    Module compiled with module API=20060613, debug=0, thread-safety=0
    PHP compiled with module API=20050922, debug=0, thread-safety=0

    To fix it I had to use the right version of pecl so instead of
    $pecl
    I had to locate the different version of pecl and use
    $/usr/bin/pecl install mongo

    Also pecl ran into an out of memory error. To fix this I had to

    vi /usr/share/pear/pearcmd.php
    and add to the top...
    ini_set('memory_limit', '32M');

    /http://www.morguefile.com

  5. Feb 05

    Anonymous says:

    Setup on XAMPP Install the Developer package for XAMPP Make sure the the Apple D...

    Setup on XAMPP
    Install the Developer package for XAMPP
    Make sure the the Apple Developer Tools are installed
    !!!Edit your php.ini file with extension=mongo. For some reason, the build did not work until I did that.
    if XAMPP install in your Application folder, run sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongo
    Restart XAMPP

  6. Feb 05

    Anonymous says:

    Debian Install apt-get install php5-dev, php5-pear pecl install mongo Edit php.i...

    Debian Install
    apt-get install php5-dev, php5-pear
    pecl install mongo
    Edit php.ini files for both apache2 and php-cli
    Restart apache server

Add Comment