Skip to content

Building PHP MongoDB Driver

aborkar-ibm edited this page Feb 12, 2019 · 61 revisions

Building the PHP Driver for MongoDB

Below versions of PHP Driver for MongoDB are available in respective distributions at the time of creation of these build instructions:

  • Ubuntu 16.04 has 1.1.5-1
  • Ubuntu 18.04 has 1.3.4-1

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

  • RHEL (6.10, 7.4, 7.5, 7.6)
  • SLES (12 SP3, 15)
  • Ubuntu (16.04, 18.04)

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.10, 7.4, 7.5, 7.6)
    sudo yum install -y  gcc make openssl-devel wget tar autoconf libxml2 libxml2-devel 
  • SLES 12 SP3
    sudo zypper install -y  gcc make openssl-devel php5-devel php5-pear php5-json tar wget 
  • SLES 15
    sudo zypper install -y  gcc make openssl-devel php7-devel php7-pear php7-json tar wget 
  • Ubuntu (16.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 (18.04)
    sudo apt-get update -y
    sudo apt-get install -y gcc make autoconf pkg-config openssl php7.2 php7.2-json php7.2-dev 

1.2) Build and Install PHP (only RHEL)

cd /<source_root>/
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.4.2

1.4) Enable PHP extension

  • RHEL
     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a  /usr/local/php/php.ini
  • SLES 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
  • SLES 15
     echo -e "\n; PHP driver for MongoDB\nextension=json.so" | sudo tee -a /etc/php7/cli/php.ini
     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php7/cli/php.ini
  • Ubuntu (16.04)
     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php/7.0/cli/php.ini
  • Ubuntu (18.04)
     echo -e "\n; PHP driver for MongoDB\nextension=mongodb.so" | sudo tee -a /etc/php/7.2/cli/php.ini

Step 2: Testing

The example code section given below is used to perform a basic test to ensure that the MongoDB PHP 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