MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDBHP extension


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


Install the MongoDB PHP extension on Linux

Install on the terminal

You can execute the following command in Linux to install the MongoDB PHP extension driver

$ sudo pecl install mongo

Using the pecl installation command of php must ensure that the network connection is available and root permissions are available.

Installation Manual

If you want to compile the extension driver from source code. You have to manually compile the source package. The good thing about this is that the latest bug fixes are included in the source package.

You can download the MongoDB PHP driver package on Github. Visit the github website and search for "mongo php driver" (download address: https://github.com/mongodb/mongo-php-driver), download the source code package, and then execute the following command:

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

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

$ tar zxvf mongodb-mongodb-php-driver-<commit_id>.tar.gz
$ cd mongodb-mongodb-php-driver-<commit_id>
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ sudo make install

After executing the above command, you need to modify php.ini file, add the mongo configuration in the php.ini file, the configuration is as follows:

extension=mongo.so

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


Installing MongoDB PHP extension on window

Github has provided a precompiled php mongodb driver binary package for the window platform (download address: https://s3 .amazonaws.com/drivers.mongodb.org/php/), you can download the version corresponding to your php, but you need to pay attention to the following issues:

  • VC6 Is running on the Apache server

  • 'Thread safe' (thread safe) is running on Apache as a module on PHP. If you run PHP in CGI mode, please select non-thread safe mode('non-thread safe').

  • VC9 is running on the IIS server.

  • After downloading the binary package you need, unzip the compressed package and add the 'php_mongo.dll' file to your PHP extension directory (ext). The ext directory is usually in the ext directory under the PHP installation directory.

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

extension=php_mongo.dll

Restart the server.

Access phpinfo through the browser. If the installation is successful, you will see the following information:

mongo-php-driver-installed-windows

Install the MongoDB PHP extension driver in 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 use the following command to install the MongoDB PHP extension driver:

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

If the above command does not work in XMPP or MAMP, you need to download a compatible pre-installed version from Github Compile the package.

Then add 'extension=mongo.so' configuration to your php.ini file.

php.cn