How to install MongoDB PHP extension from source manually

Let’s start with the advantages of MongoDB before we install MongoDB PHP extension. Compared to a traditional relational database, MongoDB has the following advantages.

  1. Weak consistency (ultimately consistent), which can better ensure the client’s access speed.
  2. The storage method which is structured by document makes it easier for what you want.
  3. Built-in GridFS, supporting high capacity storage.
  4. Provide AutoSharding mechanism based on Range.
  5. Better query performance

Installing PHP extension from source is common, there is no big difference between MongoDB and Redis.

Step1: Download and install.

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

When see this below, is means that you have installed it successfully.

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20180731/

Step2: Modify php.ini and make it work

find your php.ini and add this:

extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/mongodb.so

then restart your fpm or apache.

STEP 3: Testing

Here is a test php script, run it in your linux server. If it print “success”, it means every goes well.

<?php
// replace the ip and port to your mongo server address
$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
try {
    echo 'success';
} catch (\Exception $e) {
    echo 'fail';
}

That’s it, feel free if you have any problem.

RSS