Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

- mongodb/mongodb 1.2.0 requires ext-mongodb ^1.3.0 -> the requested PHP extension mongodb is missing from your system

On Debian Jessie, Apache2 --version "0.91-ubuntu1", PHP 5.6.30.

I have tried many the questions already answered when searching for the topic.

1) I did the manual install for MongoDB PHP Driver http://php.net/manual/en/mongodb.installation.manual.php

2) To check if it works I created a file testMongo.php and added

< ?php phpinfo(); ?>

3) Running testMongo.php in the browser yield that configuration file can be found in.

/etc/php5/apache2/php.ini

4) My mongo.so file can be located using the command

php -i | grep extension_dir

5) Output is:

extension_dir => /usr/lib/php5/20131226 => /usr/lib/php5/20131226

6) In this file I added right after "Dynamic Extensions"

extension=/usr/lib/php5/20131226/mongo.so

7) I then went over to Composer and did the Command-line installation

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

8) Once Composer is installed I tried:

php composer.phar require mongodb/mongodb

However this results in the following message:

  Problem 1
   - mongodb/mongodb 1.2.0 requires ext-mongodb ^1.3.0 -> the requested PHP extension mongodb is missing from your system.
   - mongodb/mongodb 1.2.0 requires ext-mongodb ^1.3.0 -> the requested PHP extension mongodb is missing from your system.
   - mongodb/mongodb 1.2.0 requires ext-mongodb ^1.3.0 -> the requested PHP extension mongodb is missing from your system.
   - Installation request for mongodb/mongodb ^1.2 -> satisfiable by mongodb/mongodb[1.2.0].

Some fellows mentioned in a post that I could use --ignore-platform-reqs It works!

When i run:

# php5 index.php

$client = new MongoDB\Client(< connection to mongo-atlas>) //Works with python

I get the following error:

PHP Fatal error:  Class 'MongoDB\Driver\Manager' 
not found in /var/www/vendor/mongodb/mongodb/src/Client.php on line 83
like image 653
Mattis Asp Avatar asked Nov 13 '17 20:11

Mattis Asp


3 Answers

Try this

sudo apt-get install php-mongodb
like image 56
Ayman Elshehawy Avatar answered Oct 19 '22 21:10

Ayman Elshehawy


Run

pecl upgrade mongodb

If it complains about phpize, run

sudo apt install php7.x-dev

Replace 7.x with your PHP version. E.g. -

sudo apt install php7.3-dev
like image 26
M_R_K Avatar answered Oct 19 '22 21:10

M_R_K


Composer is giving you the right answer there, you are using the wrong library. Don't use --ignore-platform-reqs, or you can, which to force install it. What you need to do now is check which library fits your php version. Write

php --version

if you don't know which one you are using. Output should be something like:

PHP 5.6.30-0+deb8u1 (cli) (built: Feb  8 2017 09:49:20)
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

Find your version in the list below.

See the description in mongodb: PHP-Driver for mongodb

Driver compatibility

Head over to pecl and download whichever version fits your php version number.

in your case (PHP5.6):

$ wget https://pecl.php.net/get/mongodb-1.2.11.tgz
$ tar -xvzf mongodb-1.2.11.tgz
$ cd mongodb-1.2.11/
$ phpize
$ ./configure
$ make all -j 5
$ sudo make install

now it should work.

You can test the php mongodb connection with this code (finding an ObjectId) :

<?php
# filename ConnectMongo.php
require_once __DIR__ . "/vendor/autoload.php";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// connect to mongodb
$manager = new MongoDB\Driver\Manager('mongodb://username:password@host');

$id = new \MongoDB\BSON\ObjectId("5a0c8e2362eb6404c2f10032");
$filter = ['_id' => $id];
$options = [];

$query = new \MongoDB\Driver\Query($filter, $options);
$rows   = $manager->executeQuery('db.collection', $query);
foreach ($rows as $document) {
    var_dump($document);
}
?>

In terminal write this to test the connection:

$ php ConnectMongo.php

Make sure that you also install using composer, you should not get that same error anymore.

like image 21
Mattis Asp Avatar answered Oct 19 '22 22:10

Mattis Asp