fj123x
fj123x

Reputation: 7512

php7 mongodb authentication fails

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!

Upvotes: 1

Views: 1640

Answers (1)

Wan B.
Wan B.

Reputation: 18845

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

Upvotes: 2

Related Questions