Skip to content

Building PHP MongoDB Driver

aborkar-ibm edited this page Dec 27, 2017 · 60 revisions

Building the PHP Driver for MongoDB

The instructions provided below specify the steps to build PHP Driver for MongoDB version 1.3.4 on Linux on IBM Z for following distributions:

  • RHEL (6.9, 7.1, 7.2, 7.3, 7.4)
  • SLES (11 SP4, 12, 12 SP1, 12 SP2, 12 SP3)
  • Ubuntu (16.04, 17.04, 17.10)

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified .
  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.

Step 1 : Building and Installing PHP driver for MongoDB

1.1) Install dependencies

  • RHEL (6.9, 7.1, 7.1, 7.3, 7.4)
    sudo yum install -y  gcc make openssl-devel wget tar autoconf libxml2 libxml2-devel 
  • SLES 11 SP4
     sudo zypper install -y gcc make tar wget libxml2 libxml2-devel awk autoconf  pkg-config openssl-devel timezone
  • SLES (12, 12 SP1, 12 SP2, 12 SP3)
    sudo zypper install -y  gcc make openssl-devel php5-devel php5-pear php5-json tar wget 
  • Ubuntu (16.04, 17.04)
    sudo apt-get update -y
    sudo apt-get install -y gcc make autoconf pkg-config openssl php7.0 php7.0-json php7.0-dev 
  • Ubuntu (17.10)
    sudo apt-get update -y
    sudo apt-get install -y gcc make autoconf pkg-config openssl php7.1 php7.1-json php7.1-dev 

1.2) Build and Install PHP (RHEL and SLES 11 SP4)

wget http://www.php.net/distributions/php-5.6.8.tar.gz
tar xvzf php-5.6.8.tar.gz
cd /<source_root>/php-5.6.8 
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php
make
sudo make install
sudo cp /<source_root>/php-5.6.8/php.ini-development /usr/local/php/php.ini
export PATH=/usr/local/php/bin:$PATH

1.3) Install PHP driver for MongoDB

sudo env PATH=$PATH pecl install mongodb-1.3.4

1.4) Enable PHP extension

  • RHEL and SLES 11 SP4
       
     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a  /usr/local/php/php.ini
  • SLES (12, 12 SP1, 12 SP2, 12 SP3)
       
      echo -e "\n; PHP driver for MongoDB\nextension=json.so" | sudo tee -a /etc/php5/cli/php.ini
      echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php5/cli/php.ini
  • Ubuntu (16.04, 17.04)
       echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php/7.0/cli/php.ini
  • Ubuntu (17.10)
       echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php/7.1/cli/php.ini

Step 2: Testing

The example code section given below is used to perform a basic test to ensure that the MongoDB C Driver is working as expected, and can connect and query a MongoDB server.

2.1) Prerequisites

The MongoDB Driver needs access to a running MongoDB server, either on your local server or a remote system. The following commands are an example of how to start up a MongodDB server and then connect to it with the client shell, but note that MongoDB has not been installed as part of these instructions, and typically you would be running MongoDB on a remote server.

        mongod > /tmp/mongodb.log &
        mongo --host localhost 
		

2.2) The test code

Create a file named 'test.php' in /<source_root>/ with the content shown below.

<?php
   
   // Config
   $dbhost = 'localhost';
   $dbname = 'ibm_test_db';
   $collection = 'mongodb_php_client';
   $test_msg = 'test message';
   
   $manager = new MongoDB\Driver\Manager("mongodb://$dbhost");
   
   /* The driver connects to the database server lazily, so Manager::getServers()
    * may initially return an empty array. */
   var_dump($manager->getServers());
   
   $command = new MongoDB\Driver\Command(['ping' => 1]);
   $manager->executeCommand('db', $command);
   
   var_dump($manager->getServers());
   
   $bulk = new MongoDB\Driver\BulkWrite;
   $bulk->insert(['x' => 1]);
   $bulk->insert(['x' => 2]);
   $bulk->insert(['x' => 3]);
   
   $manager->executeBulkWrite('db.collection', $bulk);
   
   $filter = ['x' => ['$gt' => 1]];
   $options = [
       'projection' => ['_id' => 0],
       'sort' => ['x' => -1],
   ];
   
   $query = new MongoDB\Driver\Query($filter, $options);
   $cursor = $manager->executeQuery('db.collection', $query);
   
   foreach ($cursor as $document) {
       var_dump($document);
   }
   
   $command = new MongoDB\Driver\Command(['ping' => 999]);
   
   try {
       $cursor = $manager->executeCommand('admin', $command);
   } catch(MongoDB\Driver\Exception $e) {
       echo $e->getMessage(), "\n";
       exit;
   }
   
   /* The ping command returns a single result document, so we need to access the
    * first result in the cursor. */
   $response = $cursor->toArray()[0];
   
   var_dump($response);
   
?>

2.3) Execute script

Execute the test script by :

   cd /<source_root>/
   php test.php

Executing the script should produce output similar to this

   
   array(0) {
   }
   array(1) {
     [0]=>
     object(MongoDB\Driver\Server)#3 (10) {
       ["host"]=>
       string(9) "localhost"
       ["port"]=>
       int(27017)
       ["type"]=>
       int(1)
       ["is_primary"]=>
       bool(false)
       ["is_secondary"]=>
       bool(false)
       ["is_arbiter"]=>
       bool(false)
       ["is_hidden"]=>
       bool(false)
       ["is_passive"]=>
       bool(false)
       ["last_is_master"]=>
       array(8) {
         ["ismaster"]=>
         bool(true)
         ["maxBsonObjectSize"]=>
         int(16777216)
         ["maxMessageSizeBytes"]=>
         int(48000000)
         ["maxWriteBatchSize"]=>
         int(1000)
         ["localTime"]=>
         object(MongoDB\BSON\UTCDateTime)#4 (1) {
           ["milliseconds"]=>
           int(1447267964517)
         }
         ["maxWireVersion"]=>
         int(3)
         ["minWireVersion"]=>
         int(0)
         ["ok"]=>
         float(1)
       }
       ["round_trip_time"]=>
       int(554)
     }
   }
   
   object(stdClass)#6 (1) {
     ["x"]=>
     int(3)
   }
   object(stdClass)#7 (1) {
     ["x"]=>
     int(2)
   }

Note: The example code above will need to be modified to use your remote server hostname or IP address instead of "localhost", if you are attempting to connect to your own (remote) server.

References:

http://php.net/manual/en/

https://github.com/mongodb/mongo-php-driver

Clone this wiki locally