0

I'm trying to install the MongoDB PHP driver so I can connect to a MongoDB Server on another machine. Using PHP 5.6 on AWS Linux.

I used sudo pecl install mongodb. Initially there were several errors regarding dependencies but I added them all and it then seems to have installed without any errors.

However, when loading phpinfo() I don't see any reference to Mongo. When I try to run:

$connection = new MongoClient(); I get

PHP Fatal error:  Class 'MongoClient' not found in /var/www/html/mongo.php on line 3

I did put in extension=mongodb.so into my php.ini but that results in another error:

PHP Startup: Unable to load dynamic library '/usr/lib64/php/5.6/modules/mongodb.so' - /usr/lib64/php/5.6/modules/mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0

It seems this error has to do with the order in which json loads relative to MongoDB. But as in my case it doesn't appear MongoDB is loading at all I'm not sure if that applies here.

I'd appreciate assistance as to why this isn't working.

4
  • make sure extension=mongodb.so is after extension=json.so Commented Jan 6, 2017 at 5:48
  • I have extension=mongodb.so as the very last line in php.ini. I don't even see extension=json.so Commented Jan 6, 2017 at 5:50
  • do you see JSON section in phpinfo()? Commented Jan 6, 2017 at 5:51
  • Yes. JSON section is there. Commented Jan 6, 2017 at 5:54

3 Answers 3

1

For me, after following the sage advice above, I still couldn't see mongo in phpinfo. I finally resorted to uninstalling and reinstalling mongodb using pecl and noticed at the end of the output from make, it said:

configuration option "php_ini" is not set to php.ini location

Followed this: pecl config-set php_ini /etc/php.ini

Then:

sudo pecl uninstall mongodb
sudo pecl install mongodb
systemctl restart php-fpm
systemctl restart httpd

After that, mongo showed in both php -i:

 php -i|grep mongo
/etc/php.d/50-mongodb.ini
mongodb
libmongoc bundled version => 1.9.4
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => disabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
mongodb.debug => no value => no value

And phpinfo:

http://1.2.3.4/phpinfo.php/

mongodb
MongoDB support enabled
MongoDB extension version   1.4.3
MongoDB extension stability stable
libbson bundled version 1.9.4
libmongoc bundled version   1.9.4
libmongoc SSL   enabled
libmongoc SSL library   OpenSSL
libmongoc crypto    enabled
libmongoc crypto library    libcrypto
libmongoc crypto system profile disabled
libmongoc SASL  disabled
libmongoc compression   enabled
libmongoc compression snappy    disabled
libmongoc compression zlib  enabled
Directive   Local Value Master Value
mongodb.debug   no value    no value
Sign up to request clarification or add additional context in comments.

1 Comment

The two commands that really fixed it for me systemctl restart php-fpm and systemctl restart httpd
0

I had faced the same situation and i got this solution from mongo developers You don't need to put mongodb.so in php.ini instead make it a separate module If you are using debian

cat << EOF > /etc/php5/mods-available/mongodb.ini
; priority=99
extension=mongodb.so
EOF
php5enmod mongodb

if you are using fedora

echo "extension=mongodb.so" > /etc/php.d/50-mongodb.ini

Then restart your apache And it will work fine.

1 Comment

That did it! Thanks!
0

Long story short, use of MongoClient for PHP is deprecated. ( https://github.com/mongodb/mongo-php-driver/issues/300#issuecomment-210820288 )

Instead of $connection = new MongoClient(); You'll want to use $connection = new MongoDB\Driver\Manager();

Hope this helps.

7 Comments

Tried that and got PHP Fatal error: Class 'MongoDB\Driver\Manager' not found in /var/www/html/mongo.php
Have you restarted all relevant services? (apache etc)
Yes, several times. Even rebooted server.
Ah, apparently because you have the mongo.so line in your old config file that might be throwing it. Try removing that and using the new method. gl
No difference. It seems like there is some fundamental issue here. Its almost like I didn't install Mongo at all even though I did.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.