phpinfo() and pecl search mongo shows different version of mongodb - php

I have installed MongoDB on my local pc referring below site
http://linuxforever.info/2017/04/13/how-to-install-mongodb-3-4-in-linux-mint-18-ubuntu-16-04/
I made a simple demo of MongoDB and when I ran the project I got an error like
Fatal error: Call to undefined method
MongoDB\Driver\WriteConcern::isDefault()
I have checked MongoDB version on both side (phpinfo.php and terminal) it's different.
Terminal => pecl search mongo command
Matched packages, channel pecl.php.net:
Package Stable/(Latest) Local
mongo 1.6.16 (stable) MongoDB database driver
mongodb 1.4.3 (stable) 1.4.3 MongoDB driver for PHP
Phpinfo =>
MongoDB extension version 1.2.9
MongoDB extension stability stable
libbson bundled version 1.5.5
libmongoc bundled version 1.5.5
Please help me to clear this issue.
Thanks in advance

<?php
require_once './vendor/autoload.php';
$con = new MongoDB\Client("mongodb://localhost:27017");
// Creating Database
$db = $con->yourdbname;
// Creating Document
$collection = $db->employee;
// Insering Record
$collection->insertOne( [ 'name' =>'Peter', 'email' =>'peter#abc.com'] );
// Fetching Record
$record = $collection->find( [ 'name' =>'Peter'] );
foreach ($record as $employe) {
echo $employe['name'], ': ', $employe['email']."<br>";
}
?>

please check this screen short

Try Installing using following command :
pecl install mongodb
Add extension to your php.ini file at the end
extension = mongodb.so
Restert your php and apache server
Use composer to load mongo db packages
composer require mongodb/mongodb
Run following code after above process
<?php
require 'vendor/autoload.php';
// Creating Connection
$con = new MongoDB\Client("mongodb://localhost:27017");
// Creating Database
$db = $con->yourdbname;
// Creating Document
$collection = $db->employee;
// Insering Record
$collection->insertOne( [ 'name' =>'Peter', 'email' =>'peter#abc.com' ] );
// Fetching Record
$record = $collection->find( [ 'name' =>'Peter'] );
foreach ($record as $employe) {
echo $employe['name'], ': ', $employe['email']."<br>";
}

MongoDB\Driver\WriteConcern::isDefault() was introduced in ext-mongodb 1.3
You have MongoDB extension version 1.2.9 as you shared output of phpinfo().
You need to update the extension.
UPDATE
You can download latest extension from following link
https://pecl.php.net/package/mongodb
Linux Mint is Debian based and you can install following package on it to upgrade
https://ubuntu.pkgs.org/18.04/ubuntu-universe-amd64/php-mongodb_1.3.4-1build1_amd64.deb.html

I had a similar issue which I solved after several hours. Hopefully this saves others some time. I'm on macOS Sierra.
Background
I ran sudo pecl install mongodb to update mongo driver. I didn't need to add the extension to php.ini because it was there from an older version.
PHP refused to recognize the updated mongo driver. I would also receive the Fatal error: Call to undefined ... isDefault() error when trying to execute queries.
The fix
Step 1
Find the full path of the new mongodb driver extension.
There are a few ways to do this.
Updating the driver (i.e. sudo pecl install mongodb) will output something like:
Installing
'/usr/local/Cellar/php71/7.1.2_13/lib/php/extensions/no-debug-non-zts-20160303/mongodb.so'
Alternatively, you could search for it: find / -name "mongodb.so" on Mac or Linux.
Step 2
Locate your php.ini file.
Again there are a few different ways.
Echoing phpinfo() in php will tell you where it is.
Alternatively you could run find / -name php.ini.
Mine was located at /usr/local/etc/php/7.1/php.ini
Step 3
Edit your php.ini file, and replace
extension="mongodb.so"
with the path to the new extension:
extension="/usr/local/Cellar/php71/7.1.2_13/lib/php/extensions/no-debug-non-zts-20160303/mongodb.so"
Step 4
Save and retry your query.
More info
In my case, php.ini was actually including the mongo extension from /usr/local/etc/php/7.1/conf.d/ext-mongodb.ini, so this was the file that I edited. However I successfully tested both solutions.

Related

moadmin fails to find mongodb extension

moadmin.php reports it can't access mongodb and that I must install the mongo extension for php. Moadmin version is 1.1.5 and github reports the latest edit was in 2017. The extension was installed on my ubuntu 18.04 lts server as instructed in php.net with the command:
pecl install mongo
However, the extension has been installed and phpinfo confirms it. I have also tested the extension with the code provided by php.net.
<?php
require 'vendor/autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>
This test code resides in the same folder as moadmin.php and the Vork Enterprise Framework has likewise been installed in that folder.
I have also confirmed mongo is running on the server and can be accessed via
mongo
and reports
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
I've also tried connecting with dbKoda but it reports
Error: All configured authentication methods failed
even though its URI is
mongodb://127.0.0.1:27017
Not sure where to go or what to try from here to get a working connection to mongo from my win10 machine. As I indicated the test code works in the browser on the win10 machine but not moadmin nor dbKoda.
Suggestions please?

MongoDb Extension for Yii 2 throwing error of extension not installed

I'm trying to install Yii2 mongo extension, I have already installed the mongo php driver and it showing in phpinfo. Please refer the screenshot for error. I have tried with both mongo and mongodb drivers, none are working.
I have tried the composer command
composer require "mongodb/mongodb=^1.0.0
from
http://us1.php.net/manual/en/mongodb.tutorial.library.php
its still the same output.
Check if there is a second php.ini for CLI version - probably mongodb is not added there (or just commented out). php --ini in console should help you find it. Fix configuration, restart apache and it should be fine.
Mongo driver is deprecated, in place of that mongodb drivers are added for newer version of PHP. Try installing Mongodb driver.
Steps for installation
http://php.net/manual/en/set.mongodb.php
Try with Xamp, I had issue with wamp and mongodb in windows.
refer this link for procedure.

MongoDB installation on Mac OS X 10.11, with MacPorts

I am trying to install MongoDB on my computer running Mac OS X 10.11 (El Capitan).
My Apache + PHP configuration was already working correctly.
I installed MongoDB and PHP drivers for MongoDB through MacPorts:
sudo port install mongodb
sudo port install php55-mongodb
Installation ran correctly.
I added mongodb.so file to the extensions loaded in the php.ini file:
extension=/opt/local/lib/php55/extensions/no-debug-non-zts-20121212/mongodb.so
When I run phpinfo() function in php file, mongodb extension seems to be loaded correctly:
I tried to test my connexion by initializing a MongoClient instance:
<?php
$mongoDB = new MongoClient();
var_dump($mongoDB);
?>
Unfortunately, I got a HTTP ERROR 500, and I got the following line in my Apache logs:
PHP Fatal error: Class 'MongoClient' not found in ...
Did I make something wrong ?
On most of site I saw you need to add the extension mongo.so and not mongodb.so in your php.ini.
If it's not solved your problem, I recommand you to install MongoDB Drive with PELC.
Run command line:
$ sudo pecl install mongo
And add this line to your php.ini:
extension=mongo.so
Doc php.net Mongo Installation
Finally found a solution to my problem.
As #neverpanic and #Zagonine made me understand, php-mongodb and php-mongo are not the same extension.
If you want to use old PHP driving classes, you should use php-mongo package instead of the very new php-mongodb.
Trying to install latest php-mongo extension, I had an other problem related to OpenSSL. To bypass it, I installed an old version of php-mongo extension:
sudo /usr/local/pear/bin/pecl install mongo-1.5.8
Hopefully it will help someone else.

MongoClient not found in PHP on Mac OSX using brew

I am trying to setup mongodb + php mongo driver using homebrew
I am trying to setup a composer package which requires mongodb.
The extension seems to be installed, as it appear in both cli and web version as below. But when I hit the webroot I get the error
Fatal error: Class 'MongoClient' not found in
/Users/sakhunzai/Sites/xhgui/public/src/Xhgui/ServiceContainer.php on
line 77
I am able to connect to mongodb and create a database etc. So mongodb is running fine. But It seems there is issue with php extension. To setup xhgui I have brewed as follow:
brew tap tideways/homebrew-profiler
brew install php56-tideways
brew install mongodb php56-mongodb
MongoDB
mongo --version
MongoDB shell version: 3.2.4
Extension
php -i|grep mongo
/usr/local/etc/php/5.6/conf.d/ext-mongodb.ini,
mongodb
mongodb support => enabled
mongodb version => 1.1.6
mongodb stability => stable
libmongoc version => 1.3.5
mongodb.debug => no value => no value
cat /usr/local/etc/mongod.conf
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
There are two kinds of drivers available at pecl
you need to install the mongoDB database driver to access mongoClient
use the following command to install the mongo
brew install php56-mongo
as explained here
You will get something like this in your phpinfo();
I hope this helps. Thanks,

Mac OS X MongoDB and MONGO PHP DRIVER

i try to install MongoDB and Mongo PHP Driver for 5.6.x for MAMP PRO , but i finishing installation (also i add extension on php.ini) but still doesn't work. Any one help me ?
it would be useful if you could give more information about versions (MAMPP version, MongoDB server version, Mongo driver version...).
However the most common problem with Mongo and php is that there are two different drivers, both can be installed through PECL.
mongo-php : Legacy driver, not recommended anymore.
sudo pecl install mongo
Add to php.ini -> extension=mongo.so
mongodb-php : New driver, recommended.
sudo pecl install mongodb
Add to php.ini -> extension=mongodb.so
While mongo-driver works with PHP versions < 7.0 mongodb driver works with PHP versions > 5.5. But some libraries are still using old mongo driver (i.e.doctrine) and could need an adapter if you want to work with the new driver.
Old driver repo: https://github.com/mongodb/mongo-php-driver-legacy
New driver repo: https://github.com/mongodb/mongo-php-library
To be sure which driver is working in your system, you can perform this easy test. Create two PHP scripts with the following initialization:
If this works then you have old driver installed
$connection = new MongoClient({here your conn data})
If this works then you have the new driver installed
$connection = new MongoDB\Driver\Manager({here your conn data})
Hope this will be useful for any user with the same problem on any platform.
(Please note the extension for windows user will be .dll instead of .so)

Categories