I have mongod running with auth=true on my server.
If I log in to my administrator (from the admin database), there is no problem getting the data.
<?php $connection = new Mongo("mongodb://admin: adminpass@127.0.0.1 "); $db = $connection->selectDB( "mydb" ); $collection = $db->selectCollection( "user" ); var_dump($collection->findOne()); ?>
but if I replaced the first line with
$connection = new Mongo("mongodb://mydbadmin: dbadminpass@127.0.0.1 :27017");
It cannot connect and receive an error message:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Couldn't authenticate with database admin: username [mydbadmin]' in .....
So the problem is that new Mongo() trying to connect my user to the admin database instead of the "mydb" database. How to choose the database that I want to connect?
EDIT:
according to http://php.net/manual/fr/mongo.construct.php
I tried this
$login = array("username" => "mydbadmin", "password" => "dbadminpass", "db" => "mydb", "connect" => true ); $connection = new Mongo("mongodb://localhost", $login);
but
Couldn't authenticate with database mydb: username [mydbadmin]' in .....
source share