0

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps you want to amend your question showing this (fake) $conStr to illustrate the problem, and include what you had to do in your answer too. That makes it easier for future visitors to see what went wrong.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.