-
Notifications
You must be signed in to change notification settings - Fork 56
Building PHP MongoDB Driver
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.04 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.17.2 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.
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
-
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 pkg-config 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
-
MongoDB Driver PHP requires PHP 7.4.0 or higher.
cd $SOURCE_ROOT wget https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/PHP/8.2.12/build_php.sh bash build_php.sh
- Install PEAR
cd $SOURCE_ROOT wget --no-check-certificate https://pear.php.net/go-pear.phar sudo php go-pear.phar export PATH=/$SOURCE_ROOT/usr/local/lib:$PATH
-
RHEL
sudo env PATH=$PATH pecl install mongodb-1.17.2
-
SLES and Ubuntu
sudo pecl install mongodb-1.17.2
Note:
-
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.17.2.tgz pecl install --offline ./mongodb-1.17.2.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.
-
echo -e "\n; MongoDB PHP driver\nextension=mongodb.so" | sudo tee -a `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
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.
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: 61a4e5f5f2d1071b57593ed7
Connecting to: mongodb://<hostname or IP address>:27017/?directConnection=true
Using MongoDB: 5.0.2
Using Mongosh: 1.0.6
...
test>
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)
}
- MongoDB PHP Driver Overview - https://docs.mongodb.com/ecosystem/drivers/php/
- MongoDB PHP Driver Documentation - https://secure.php.net/manual/en/set.mongodb.php
- MongoDB PHP Driver GitHub - https://github.com/mongodb/mongo-php-driver
The information provided in this article is accurate at the time of writing, but on-going development in the open-source projects involved may make the information incorrect or obsolete. Please open issue or contact us on IBM Z Community if you have any questions or feedback.