Skip to content

Building PHP MongoDB Driver

linuxonz edited this page Apr 12, 2024 · 47 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 23.10 has 1.15.0
  • RHEL (9.0, 9.2, 9.3) has 1.16.2

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

  • RHEL (7.8, 7.9, 8.6, 8.8, 8.9, 9.0, 9.2, 9.3)
  • SLES (12 SP5, 15 SP5)
  • Ubuntu (20.04, 22.04, 23.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 : Build and Install PHP driver for MongoDB

1.1) Install dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL (7.8, 7.9)

    sudo yum install -y cyrus-sasl-devel gcc make openssl-devel pkgconfig wget
  • RHEL (8.6, 8.8, 8.9)

    sudo yum install -y cyrus-sasl-devel gcc make openssl-devel pkgconfig compat-openssl10 wget
  • RHEL (9.0, 9.2, 9.3)

    sudo yum install -y which autoconf automake cyrus-sasl-devel gcc libtool libxml2-devel diffutils make openssl-devel pkgconfig tar wget php php-devel php-json php-pear pcre2
  • SLES 12 SP5

    sudo zypper install -y cyrus-sasl-devel gcc libopenssl-devel libopenssl1_0_0 make pkg-config which gawk wget
  • SLES 15 SP5

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

    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 7.x, RHEL 8.x and SLES 12 SP5)

cd $SOURCE_ROOT
wget https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/PHP/8.3.2/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.18.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.18.0.tgz
    sudo pecl install --offline ./mongodb-1.18.0.tgz
  • The MongoDB PHP driver requires libbson and libmongoc, so it will use bundled versions (1.23.3) 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 MongoDB and MongoDB Shell (mongosh) can be found on their official website here.

2.1) Start MongoDB

To run this test, MongoDB must be running on the default port, 27017. The following commands are an example of how to start a MongoDB server and then connect to it with the client shell.

Issue the following command to start mongod and to check if it is up, connect to it with the MongoDB shell.

mongosh --host <hostname or IP address> --port 27017

The output should be similar to:

Current Mongosh Log ID:	660e7ae39d8aeccec34614c5
Connecting to:		mongodb://<hostname or IP address>:27017/?directConnection=true
Using MongoDB:		5.0.2
Using Mongosh:		1.0.6
...
test>

2.2) 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