Skip to content

Building PHP MongoDB Driver

aborkar-ibm edited this page May 17, 2017 · 61 revisions

Building the PHP Driver for MongoDB

The instructions provided below specify the steps to build PHP Driver for MongoDB version 1.2.9 on IBM z Systems for following distributions:

  • RHEL (6.9, 7.1, 7.2, 7.3)
  • SLES (11 SP4, 12, 12 SP1, 12 SP2)
  • Ubuntu (16.04, 16.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

    sudo yum install -y gcc make openssl-devel  tar wget git  autoconf libxml2 libxml2-devel 

• RHEL (7.1, 7.1, 7.3)

    sudo yum install -y  gcc make openssl-devel php-devel php-pear tar wget git

• SLES 11 SP4

     sudo zypper install -y gcc make tar wget git libxml2 libxml2-devel awk autoconf  pkg-config openssl-devel

• SLES (12, 12 SP1, 12 SP2)

    sudo zypper install -y  gcc make openssl-devel php5-devel php5-pear php5-json tar wget git

• Ubuntu (16.04, 16.10)

    sudo apt-get update -y
    sudo apt-get install -y gcc make tar wget git autoconf pkg-config openssl php7.0 php7.0-json php7.0-dev 

1.2) Build and Install PHP (RHEL 6.9 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) Download source code and Install PHP driver for MongoDB

git clone https://github.com/mongodb/mongo-php-driver.git
cd mongo-php-driver/
git checkout 1.2.9
sudo pecl install mongodb

1.4) Enable PHP extension

• RHEL 6.9 and SLES 11 SP4

       
     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a  /usr/local/php/php.ini

• RHEL (7.1, 7.1, 7.3)

     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php.ini
		   

• SLES (12, 12 SP1, 12 SP2)

       
      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, 16.10)

       echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php/7.0/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 obviously 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

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