Install php MongoDB driver on Ubuntu/CentOS 7/Windows

In order to use MongoDB in our PHP programs, we need to install php mongodb driver. It is a PHP extension that manages the connection to the MongoDB server and enables you to perform all kinds of operations on a NoSQL database through PHP.

In this tutorial we will see how to install and configure the mongodb php driver on Ubuntu, CentOS 7 and Microsoft Windows. As distributions and environments vary, installation instructions will also vary.

Install MongoDB PHP Driver on Ubuntu

MongoDB PHP Driver for Ubuntu provides by the php-mongodb package which we can install with apt-get command.

sudo apt-get update
sudo apt-get install php-mongodb

Then, restart the Apache Web Server:

sudo systemctl restart apache2.service

To verify the installation, we can run phpinfo() function. The phpinfo page should display mongodb details as shown below.

mongodb php driver on ubuntu

From the command line you can run php -i command to get information on mongodb driver.

php -i | grep mongo

Install PHP MongoDB Driver on CentOS 7

The php mongodb driver on CentOS 7 for PHP 5 available from the epel-repository.

First, enable the epel repository:

sudo yum -y install epel-release
sudo yum repolist

Install the php-mongodb package:

sudo yum -y install php-mongodb

Restart the HTTPD server:

sudo systemctl restart httpd

If you installed PHP 7 using webtatic repository, then the package name should be something like "php71w-pecl-mongodb". You can search available packages with yum search command.

yum search php | grep -i mongo

Selinux will block mongodb driver on CentOS 7. As a solution you can either disable Selinux or enable the 'httpd_can_network_connect_db' boolean.

sudo setsebool -P httpd_can_network_connect_db 1

Following is the sample PHP error caused by Selinux on CentOS 7:

PHP Fatal error: Uncaught MongoDB\\Driver\\Exception\\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on 'localhost:27017']

Install PHP MongoDB Driver on Windows 10/Windows Server 2016

Important: For Windows, You need to download correct mongodb driver for PHP, based on your PHP version, Architecture and whether Thread Safety is enabled. You can get those information from the phpinfo page.

Do the following steps to install and configure MongoDB driver on Windows XAMPP Server.

  1. Download the latest stable version of the PHP MongoDB driver from following URL
    Download PHP MongoDB Driver for Windows
  2. Extract the archive File.
  3. Copy the php_mongodb.dll file from the extracted folder to the PHP extension directory. this is usually the "C:\xampp\php\ext" folder in XAMPP Server.
  4. Open the php.ini file inside your PHP installation(C:\xampp\php) and add the following line:
  5. Save the file and close it. Restart the Apache web server.

Test MongoDB Connection From PHP Script

Let's write a very simple PHP program that creates a connection to the MongoDB server and dump the connection status.

Add following PHP code to your php script and access from the Web browser.

$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");

The above PHP example will output something similar to:

object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(25) "mongodb://localhost:27017" ["cluster"]=> array(1) { [0]=> array(11) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(0) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["tags"]=> array(0) { } ["last_is_master"]=> array(0) { } ["round_trip_time"]=> int(-1) } } }

And that is mean, you have a connection to the MongoDB Server in your PHP script.