Skip to content

Building PHP MongoDB Driver

linuxonz edited this page Oct 24, 2024 · 53 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 20.04 has 1.6.1
  • Ubuntu 22.04 has 1.12.0
  • Ubuntu (24.04) have 1.15.0
  • RHEL (9.2, 9.4) have 1.20.0

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

  • RHEL (8.8, 8.10)
  • SLES (15 SP5, 15 SP6)
  • Ubuntu (20.04, 22.04, 24.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.

1. Build and Install PHP driver for MongoDB

1.1. Install dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL (8.8, 8.10)

    sudo yum install -y cyrus-sasl-devel gcc make openssl-devel pkgconfig compat-openssl10 wget diffutils
  • SLES (15 SP5, 15 SP6)

    sudo zypper install -y cyrus-sasl-devel gcc libopenssl-devel make php7 php7-devel php7-json php7-openssl pkg-config which gawk
  • Ubuntu (20.04, 22.04, 24.04)

    sudo apt-get update
    sudo apt-get install -y libsasl2-dev libssl-dev php php-dev pkg-config

1.2. Build and Install PHP (Only for RHEL 8.x)

cd $SOURCE_ROOT
wget https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/PHP/8.3.9/build_php.sh
bash build_php.sh -y
wget --no-check-certificate https://pear.php.net/go-pear.phar
php go-pear.phar
export PATH=/usr/local/lib:$SOURCE_ROOT/pear/bin:$PATH

1.3. Install MongoDB PHP driver

sudo pecl install mongodb-1.20.0

Note:

  • You may also use sudo env PATH=$PATH <command> if any command fails with command not found error.

  • If you encounter this error: Connection to 'pecl.php.net:443' failed: Unable to find the socket transport "ssl", please run this command prior to running pecl:

    sudo sed -i 's|$PHP -C -n -q |$PHP -C -q |' `which pecl`
  • If you encounter this error: Cannot download "pecl/mongodb"``(Connection to ssl://pecl.php.net:443 failed), please use this command to install MongoDB PHP driver:

    wget --no-check-certificate https://pecl.php.net/get/mongodb-1.20.0.tgz
    sudo pecl install --offline ./mongodb-1.20.0.tgz
  • The MongoDB PHP driver requires libbson and libmongoc, so it will use bundled versions (1.27.0) of the libraries, if they aren't present.

1.4) Enable PHP extension

echo -e "\n; MongoDB PHP driver\nextension=mongodb.so" | sudo tee -a `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

Step 2: Testing (Optional)

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. Instructions to install and start MongoDB and MongoDB Shell (mongosh) can be found on their official website here.

The test code

Create a file named test.php with the content shown below. If you are connecting to a remote server then you need to substitute the localhost with the hostname or IP address of the MongoDB server.

<?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);
?>

Execute the test script by:

php test.php

Executing the script should produce output similar to this:

array(1) {
  [0]=>
  object(MongoDB\Driver\Server)#4 (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_hello_response"]=>
  array(13) {
	["helloOk"]=>
	bool(true)
	["ismaster"]=>
	bool(true)
	["topologyVersion"]=>
	array(2) {
	  ["processId"]=>
	  object(MongoDB\BSON\ObjectId)#3 (1) {
		["oid"]=>
		string(24) "63f5adf3f94b0bbbfa6bfd53"
	  }
	  ["counter"]=>
	  int(0)
	}
	["maxBsonObjectSize"]=>
	int(16777216)
	["maxMessageSizeBytes"]=>
	int(48000000)
	["maxWriteBatchSize"]=>
	int(100000)
	["localTime"]=>
	object(MongoDB\BSON\UTCDateTime)#5 (1) {
	  ["milliseconds"]=>
	  string(13) "1677072321437"
	}
	["logicalSessionTimeoutMinutes"]=>
	int(30)
	["connectionId"]=>
	int(196)
	["minWireVersion"]=>
	int(0)
	["maxWireVersion"]=>
	int(13)
	["readOnly"]=>
	bool(false)
	["ok"]=>
	float(1)
  }
    ["round_trip_time"]=>
    int(0)
  }
}
object(stdClass)#7 (1) {
  ["x"]=>
  int(3)
}
object(stdClass)#8 (1) {
  ["x"]=>
  int(2)
}
object(stdClass)#9 (1) {
  ["ok"]=>
  float(1)
}

References:

Clone this wiki locally