- How to Install the PHP MongoDB Extension
- Installing the MongoDB Extension
- Verify the Installation
- Uninstalling the MongoDB Extension
- Using the MongoDB PHP Library
- Removing the Deprecated mongo PHP Extension
- MongoDB PHP Driver
- Installation
- Note
- Connect to MongoDB Atlas
- Note
- Connect to MongoDB Atlas Without the Stable API
- Connect to a MongoDB Server on Your Local Machine
- Important
- Compatibility
- MongoDB Compatibility
- Php mongo extension install
- Php mongo extension install
- Manual Installation
- Installing on *NIX
- Installing on Windows
- macOS
- Gentoo
- Red Hat
- Third-Party Installation Instructions
How to Install the PHP MongoDB Extension
In the examples shown, replace «X.Y» with your app’s PHP version (for example, «7.0»). To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version.
This PHP extension provides client access to the MongoDB server. It is not MongoDB itself. MongoDB is a NoSQL database server. When you install the mongo PHP extension, it will allow PHP to communicate with a MongoDB server.
The MongoDB PHP extension supports PHP 5.4, 5.5, 5.6, 7.0, 7.1, 7.2, and 7.3.
Installing the MongoDB Extension
To install this extension, run the following commands as your server’s root user:
sudo apt-get -y install gcc make autoconf libc-dev pkg-config sudo apt-get -y install libssl-dev sudo peclX.Y-sp install mongodb
Once installed, create a configuration file for the extension and restart PHP by running the following commands as root:
sudo bash -c "echo extension=mongodb.so > /etc/phpX.Y-sp/conf.d/mongodb.ini" sudo service phpX.Y-fpm-sp restart
Verify the Installation
You can verify the MongoDB extension is installed by running this command:
phpX.Y-sp -i | grep -i mongodb
You should see output such as the following:
/etc/phpX.Y-sp/conf.d/mongodb.ini, mongodb mongodb support => enabled mongodb version => 1.1.8 mongodb stability => stable mongodb.debug => no value => no value
Uninstalling the MongoDB Extension
To uninstall this extension, as root run the commands:
sudo rm /etc/phpX.Y-sp/conf.d/mongodb.ini sudo peclX.Y-sp uninstall mongodb
Next, restart PHP-FPM with the command:
sudo service phpX.Y-fpm-sp restart
Using the MongoDB PHP Library
Once you’ve installed the mongodb PHP extension, the easiest way to use the extension is through the MongoDB PHP library.
Removing the Deprecated mongo PHP Extension
If you previously installed the older mongo extension (the mongo extension is different than the mongodb extension), you can remove it with the following commands run as root:
sudo rm /etc/phpX.Y-sp/conf.d/mongo.ini sudo service phpX.Y-fpm-sp restart
MongoDB PHP Driver
Welcome to the documentation site for the official MongoDB PHP driver. You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components:
While it is possible to use the extension alone, MongoDB recommends using both the extension and the library together. Download the components you need or set up a runnable project by following our tutorials.
Installation
First, make sure you have a recent version of PHP installed on your system. See the official PHP manual
for download and installation instructions.
Install the PHP MongoDB Extension before installing the PHP Library for MongoDB. You can install the extension using PECL
$ sudo pecl install mongodb
Finally, add the following line to your php.ini file:
Note
On some systems, there may be multiple INI files for individual SAPIs (e.g. CLI, FPM). Make sure to enable the extension in all SAPIs that you need.
The preferred method of installing the PHP library is with Composer
by running the following from your project root:
$ composer require mongodb/mongodb
Once you have installed the library, ensure that your application includes Composer’s autoloader as in the following example:
require_once __DIR__ . '/vendor/autoload.php';
Additional installation instructions may be found in the library documentation .
Connect to MongoDB Atlas
You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas:
use Exception; use MongoDB\Client; use MongoDB\Driver\ServerApi; // Replace the placeholder with your Atlas connection string $uri = ''; // Specify Stable API version 1 $apiVersion = new ServerApi(ServerApi::V1); // Create a new client and connect to the server $client = new MongoDB\Client($uri, [], ['serverApi' => $apiVersion]) ; try // Send a ping to confirm a successful connection $client->selectDatabase('admin') ->command(['ping' => 1]) ; echo "Pinged your deployment. You successfully connected to MongoDB!\n"; > catch (Exception $e) printf($e->getMessage() ) ; >
This connection snippet uses the Stable API feature, which you can enable when using the PHP driver v1.9 and later to connect to MongoDB Server v5.0 and later. When you use this feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.
To learn more about the Stable API feature, see Stable API in the Server manual.
Note
Starting from Feburary 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.
Connect to MongoDB Atlas Without the Stable API
If you are using a version of MongoDB or the driver that doesn’t support the Stable API feature, you can use the following code snippet to test your connection to your MongoDB deployment on Atlas:
use Exception; use MongoDB\Client; // Replace the placeholder with your Atlas connection string $uri = ''; // Create a new client and connect to the server $client = new MongoDB\Client($uri) ; try // Send a ping to confirm a successful connection $client->selectDatabase('admin') ->command(['ping' => 1]) ; echo "Pinged your deployment. You successfully connected to MongoDB!\n"; > catch (Exception $e) printf($e->getMessage() ) ; >
Connect to a MongoDB Server on Your Local Machine
If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:
Important
Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.
After you successfully start your MongoDB server, specify your connection string in your driver connection code.
If your MongoDB Server is running locally, you can use the connection string «mongodb://localhost:» where is the port number you configured your server to listen for incoming connections.
If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings .
To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.
Compatibility
Due to potential problems representing 64-bit integers on 32-bit platforms, users are advised to use 64-bit environments. When using a 32-bit platform, be aware that any 64-bit integer read from the database will be returned as a MongoDB\BSON\Int64
instance instead of a PHP integer type.
MongoDB Compatibility
The following compatibility table specifies the recommended version(s) of the PHP driver for use with a specific version of MongoDB.
The first column lists the driver version(s).
Php mongo extension install
Precompiled binaries are attached to the project’s » Github releases. Archives are published for various combinations of PHP version, thread safety (TS or NTS), and architecture (x86 or x64). Determine the correct archive for your environment and extract the php_mongodb.dll file to the PHP extension directory («ext» by default).
Add the following line to the php.ini file for each environment in which you intend to use the driver:
Failure to select the correct binary will result in an error when attempting to load the extension DLL at runtime:
PHP Warning: PHP Startup: Unable to load dynamic library 'mongodb'
- PHP version ( PHP_VERSION )
- Thread safety ( PHP_ZTS )
- Architecture ( PHP_INT_SIZE )
In addition to the aforementioned constants, these properties can also be inferred from phpinfo() . If your system has multiple PHP runtimes installed, double-check that you are examining the phpinfo() output for the correct environment.
Note: Additional DLL dependencies for Windows Users
In order for this extension to work, there are DLL files that must be available to the Windows system PATH . For information on how to do this, see the FAQ entitled «How do I add my PHP directory to the PATH on Windows». Although copying DLL files from the PHP folder into the Windows system directory also works (because the system directory is by default in the system’s PATH ), this is not recommended. This extension requires the following files to be in the PATH : libsasl.dll
Php mongo extension install
This extension is deprecated. Instead, the MongoDB extension should be used.
The MongoDB PHP driver should work on nearly any system: Windows, macOS, Unix, and Linux; little- and big-endian machines; 32- and 64-bit machines; PHP 5.3 through 5.6 (versions prior to 1.6 also support PHP 5.2).
This » PECL extension is not bundled with PHP.
Manual Installation
For driver developers and people interested in the latest bugfixes, you can compile the driver from the latest source code on » Github. Go to Github and click the «download» button. Then run:
$ tar zxvf mongo-php-driver-legacy-.tar.gz $ cd mongo-php-driver-legacy- $ phpize $ ./configure $ make all $ sudo make install
Make the following changes to php.ini :
- Make sure the extension_dir variable is pointing to the directory containing mongo.so. The build will display where it is installing the PHP driver with output that looks something like:
Installing '/usr/lib/php/extensions/no-debug-non-zts-20060613/mongo.so'
$ php -i | grep extension_dir extension_dir => /usr/lib/php/extensions/no-debug-non-zts-20060613 => /usr/lib/php/extensions/no-debug-non-zts-20060613
Installing on *NIX
Add the following line to your php.ini file:
If pecl runs out of memory while installing, make sure memory_limit in php.ini is set to at least 128MB.
Installing on Windows
Precompiled binaries for each release are available from » PECL for a variety of combinations of versions, thread safety, and VC libraries. Unzip the archive and put php_mongo.dll in your PHP extension directory («ext» by default).
Add the following line to your php.ini file:
Note: Additional DLL dependencies for Windows Users
In order for this extension to work, there are DLL files that must be available to the Windows system PATH . For information on how to do this, see the FAQ entitled «How do I add my PHP directory to the PATH on Windows». Although copying DLL files from the PHP folder into the Windows system directory also works (because the system directory is by default in the system’s PATH ), this is not recommended. This extension requires the following files to be in the PATH : libsasl.dll
macOS
In most cases installing from PECL is the easiest way:
If your system has multiple version of PHP installed (e.g. macOS default, Homebrew, » XAMPP), note that each version of PHP has its own pecl command and php.ini file.
Note: Xcode dependency for compiling on macOS
Compiling the driver on macOS will require Xcode developer tools, which may be installed with xcode-select —install . If that command is not available, you may first need to install the » Command Line Tools package.
Gentoo
Gentoo has a package for the PHP PECL driver called dev-php/pecl-mongo, which can be installed with:
$ sudo emerge -va dev-php/pecl-mongo
If you use PECL, you may 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 $ sudo make install
Red Hat
This includes Fedora and CentOS.
The default Apache settings on these systems do not let requests make network connections, meaning that the driver will get «Permission denied» errors when it tries to connect to the database. If you run into this, try running:
$ /usr/sbin/setsebool -P httpd_can_network_connect 1
Third-Party Installation Instructions
A number of people have created excellent tutorials on installing the PHP driver.
- » (Re)installing PHP on macOS This article by Justin Hileman details the process of installing PHP and additional extensions with Homebrew on macOS. This complements the earlier instructions on this page for installing the driver with Homebrew.
- » PHP 5.3.1 with Xdebug, MongoDB and Lithium on Ubuntu 9.10 / Apache 2.2 This screencast by Jon Adams demonstrates how to quickly get up and running with PHP 5.3.1, Xdebug, and MongoDB on Ubuntu 9.10 with Apache.