I have added mongodb.so in both /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini as shown below
extension=/usr/lib/php5/20100525/mongodb.so
But still I am getting the below error Class 'MongoDate' not found.
Php Version - Php 5.4.45
Below is the information about mongodb extension from php cli
php -i | grep -i mongodb
mongodb
MongoDB support => enabled
MongoDB extension version => 1.2.5
MongoDB extension stability => stable
mongodb.debug => no value => no value
Below is the output of phpinfo() for php running under apache2
Can someone let me know how can I get rid of this error?
mongo is old driver, mongodb is new driver, and mongo has been superseded, see https://pecl.php.net/package/mongo
mongodb (new): https://pecl.php.net/package/mongodb
The class MongoDate is mongo's class. In mongodb, you should be use MongoDB\BSON\UTCDateTime to replace MongoDate, see http://php.net/manual/en/class.mongodb-bson-utcdatetime.php
If you want to keep use class MongoDate, you can use pecl or manually to install old driver.
Related
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.
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.
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,
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)
I've installed the PHP-MCRYPT package, so it automatically updated my PHP from 5.3 to 5.4 and now the Mongo driver is not loading.
Sometimes this error appears:
PHP startup mongo: Unable to initialize module
Should I have to update the mongo driver? How? I'm running a VPS CentOS 6 in Godaddy.
You might need to reinstall the PHP mongo driver from PECL. Try that and let me know how you get on.