I have installed a php7 + mongodb 3.2 in an ubuntu stack:
pecl install mongodb (this is the new driver for > 5.99.99)
I'm also using the last php package mongodb/mongodb as wrapper.
but I have problems to authenticate the user.
new \MongoDB\Client('mongodb://root:123456#somehost:27017');
it fails due the authentication mechanism, the driver is trying to authenticate as MONGODB-CR (deprecated in > 3.0) instead of SCRAM-SHA-1
Of course, the authentication works well with a shell mongo client:
mongo someip:27017/admin -u root -p "123456"
The question is, how can I specify the authentication mechanism in the php driver? (The \MongoDB\Client constructor accepts some array $driverOptions = []), is there any option to specify it?
Thanks!
Make sure that you are using the latest driver. As the new default should have been SCRAM-SHA-1.
I ran a test under environment: php7, ubuntu14, MongoDB v3.2.x, mongo-php-library =^1.0.0 and mongodb php driver v1.1.5. Which works as expected.
require_once __DIR__ . "/vendor/autoload.php";
$client = new MongoDB\Client("mongodb://user:pwd#host:port/admin");
$collection = $client->selectCollection("databaseName", "collection");
$cursor = $collection->find();
foreach ($cursor as $document) { var_dump($document); }
I've also tested authMechanism option in the URI, for example:
$client = new MongoDB\Client("mongodb://user:pwd#host:port/admin?authMechanism=SCRAM-SHA-1");
Which also works, although you shouldn't need to specify SCRAM-SHA-1 if you are using the new PHP driver. If you run php --ri mongodb you should be seeing something similar to (for v1.1.5):
mongodb
mongodb support => enabled
mongodb version => 1.1.5
mongodb stability => stable
libmongoc version => 1.3.3
libbson version => 1.3.3
Related
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?
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 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.
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)