PHP – MongoClient on 32-bit Linux and PHP < 7 without refactoring

MongoClient on 32-bit Linux and PHP < 7 without refactoring... here is a solution to the problem.

MongoClient on 32-bit Linux and PHP < 7 without refactoring

I have an error saying MongoClient class not found.

I’ve tried both instructions and on the MongoDB website these answers .

On php -v on

PHP Startup: Unable to load dynamic library '/usr/lib/php/20131226/mongo.so' 
PHP 5.6.30-1+deb.sury.org~trusty+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

Use sudo grep -rnw/etc -e "mongo.so"

I have

/etc/php/5.6/fpm/php.ini:2033:extension=mongo.so
/etc/php/5.6/cli/php.ini:2032:extension=mongo.so
/etc/php/5.6/apache2/php.ini:2033:extension=mongo.so

Then I tried manual installation

When I run phpize

PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212

In the make test output I get

PHP Warning:  PHP Startup: mongo: Unable to initialize module
Module compiled with module API=20121212
PHP    compiled with module API=20131226

How do I compile the correct module?

I’ve completely removed php 7

sudo apt-get purge php7.*

Solution

I guess you installed the wrong extension for your system.

I recommend starting over and using pecl to properly install and compile the mongo extension.

  • Install pecl (assuming you are using Debian-based.) and the correct apt repos, PECL bundled with PHP-PEAR)

    sudo apt-get install php5-dev php-pear
    
  • Install legacy mongo driver from pecl

    sudo pecl install mongo
    
  • Add extension=mongo.so to the php .ini

    sudo su
    echo "extension=mongo.so" >> /etc/php5/mods-available/mongo.ini
    ln -rs /etc/php5/mods-available/mongo.ini /etc/php5/cli/conf.d/30-mongo.ini
    ln -rs /etc/php5/mods-available/mongo.ini /etc/php5/fpm/conf.d/30-mongo.ini
    ln -rs /etc/php5/mods-available/mongo.ini /etc/php5/apache2/conf.d/30-mongo.ini
    

Note that older drivers are not compatible with PHP 7+ and the latest version of MongoDB that can be used with this driver is 3.0. See compatibility chart from MongoDB Docs

Related Problems and Solutions