MongoDB on OpenBSD6.6 with PHP-FPM…

Okay, for a new project I want to try and move to MongoDB, I am really looking forward to a schema free database where I can simply just store my ‘documents’ and do with them what I want!

So let’s log into our server and get some stuff installed:

$ doas pkg_add mongodb
$ doas rcctl enable mongod
$ doas rcctl start mongod
$ mongo
MongoDB shell version: 3.2.22
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] 
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] ** NOTE: your operating system version does not support the method that MongoDB
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] **       uses to detect impending page faults.
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] **       This may result in slower performance for certain use cases
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] 
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 128, should be at least 1000
2020-03-20T23:01:00.737+0100 I CONTROL  [initandlisten] 

For what it’s worth Mongo is showing some startup warnings, I’m going to ignore these for now. Let’s first insert some data so we can test our db connection later when we are in PHP:

$ mongo testdb
> db.users.insert({ 'name': 'Remy Blom', 'email': 'r@aodw.nl' })

Thing is we have a working MongoDB, kinda cool, but I want to be able to talk to it from PHP (or do I? I might have to rethink that… PHP is waaaay old and stuff?)

First we have to prepare our OpenBSD to be able to run the manual installation. (I still think this should also be able to work in such a way that you could just use the pecl install method, but I kept running into the error mentioning AUTOCONF_VERSION was not set…)

In order to get phpize running I had to do:

$ doas pkg_add pear autoconf
$ cd /usr/local/bin
$ ln -s phpize-7.3 phpize
$ ln -s php-config-7.3 php-config

Furthermore I had to add to my ./profile:

export AUTOCONF_VERSION=2.69

Then I had to re-login for the profile to take effect and then I could run:

$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule update --init
$ phpize
$ ./configure
$ make all
$ sudo make install

Restart php-fpm and ready to go:

$ doas rcctl restart php73_fpm

Let’s write a little program that returns that user we inserted in the beginning:

<?php

    $m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
    $query = new MongoDB\Driver\Query([]); 
     
    $results = $m->executeQuery("testdb.users", $query);
    echo $results;

    foreach ($results as $result) {
        print_r($result);
    }

Running this code will print:

stdClass Object
(
    [_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5e79c3a3c7f38ba9c7943ffd
        )

    [name] => Remy Blom
    [email] => r@aodw.nl
)

Please note that in the $results object you don’t see any of your results, that had me looking puzzled and scratching my head for a while….