0
votes

Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.

In the following code, the connection string follows the pattern mongodb://user:password@mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.

mongo connection code

However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179

mongo find()

Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.

Environment:

  • Windows 10
  • Wamp
  • PHP 5.5.12
  • Mongodb driver 1.1.4
  • Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
  • Mongo instance version 2.4.6
2

2 Answers

2
votes

So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.

1
votes

I just had the same error and found out that I had to place the database-name in the connection string. The documentation here says:

If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.

And the user I'm using has no rights to the admin-database, so this is why I received the authentication error. I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.