Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB PHP Extension

MongoDB PHP Extension

This tutorial will show you how to install the MongoDB extension on Linux, Windows, and Mac platforms.


Install the MongoDB PHP extension on Linux

Install on the terminal

You can run the following command on Linux to install the MongoDB PHP extension driver



$ sudo pecl install mongodb

When using PHP's pecl install command, you must ensure that network connectivity is available and that you have root privileges.

Installation manual

If you want to compile the extension driver from source, you must manually compile the source package. The advantage of doing so is that the latest bug fixes are included in the source package.

You can download the MongoDB PHP driver package from the official PHP website; download address:http://pecl.php.net/package/mongodb。

The complete installation command is as follows:



$ wget http://pecl.php.net/get/mongodb-1.5.2.tgz

$ cd /mongodb-1.5.2

$ phpize

$ ./configure

$ make && make install

If you compiled PHP yourself, the installation method is as follows (assuming it was compiled in the /usr/local/php directory):



$ wget http://pecl.php.net/get/mongodb-1.5.2.tgz

$ cd /mongodb-1.5.2

$ /usr/local/php/bin/phpize

$ ./configure --with-php-config=/usr/local/php/bin/php-config

$ make && make install

After a successful installation, output similar to the following installation directory information appears:

...

Installing shared extensions:     /usr/lib/php/extensions/debug-non-zts-20151012/

After running the above command, you need to modify the php.ini file by adding the mongo configuration to it, as follows:



extension_dir=/usr/lib/php/extensions/debug-non-zts-20151012/

extension=mongodb.so

Note:You need to specify the path of the extension_dir configuration item.

You can view the directory address with the following command:

$ php -i | grep extension_dir

extension_dir => /usr/lib/php/extensions/debug-non-zts-20151012 => /usr/lib/php/extensions/debug-non-zts-20151012


Install the MongoDB PHP extension on Windows

PECL provides precompiled php mongodb driver binary packages for the Windows platform (download address: https://pecl.php.net/package/mongodb); you can download the version that corresponds to your PHP, but you need to pay attention to the following points:

Open the PHP configuration file php.ini and add the following configuration:



extension=php_mongodb.dll

Restart the server.

Access phpinfo through a browser; if the installation was successful, you will see information similar to the following:


Install the MongoDB PHP extension driver on Mac

You can use autoconf to install the MongoDB PHP extension driver.

You can use Xcode to install the MongoDB PHP extension driver.

If you use XAMPP, you can install the MongoDB PHP extension driver with the following command:



sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongo

If the above commands do not work in XMPP or MAMP, you need to download a compatible precompiled package from GitHub.

Then add the extension=mongodb.so configuration to your php.ini file.