2

I have an error saying MongoClient class is not found.

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

Upon php -v there is

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

with 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 the manual installation.

When I run phpize

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

At 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 removed php 7 entirely

sudo apt-get purge php7.*
Community
  • 1
  • 1
Michael Millar
  • 1,364
  • 2
  • 24
  • 46

1 Answers1

0

I guess you had installed the wrong extension for your system.

I suggest to start fresh and use pecl to install and compile mongo extension properly.

  • Install pecl (assuming that you are using Debian-based distribution with correct apt repos, pecl comes bundled with php-pear)

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

    sudo pecl install mongo
    
  • Add extension=mongo.so on 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 the legacy driver is not compatible with PHP 7+ and the lastest version of MongoDB one can use with this driver is 3.0. See the compatibility chart from MongoDB Docs.

Community
  • 1
  • 1
gmsantos
  • 1,412
  • 22
  • 30