1

Description:

I'm trying to drop a mongo collection in my Symfony application, however I get an "authentication fails" when I try to drop a collection. I'm able to select a database and I'm able to select a collection.

The mongoUrl connection string that I inject into my MongoService uses the same credentials specified in my docker compose:

mongodb://root:XXX@redaph_mongo_1:27017

Note: That even when I put in the wrong credentials I'm still able to select a database and collection without any error being thrown.

Question:

Why am I getting "Authenticaion Fails" when I try to drop a collection when I'm using root credentials? How do I correctly authenticate with the root username and password?

Error:

[2018-06-21 08:45:41] request.CRITICAL: Uncaught PHP Exception MongoDB\Driver\Exception\AuthenticationException: "Authentication failed." at /var/www/redaph/vendor/mongodb/mongodb/src/Operation/DropCollection.php line 108 {"exception":"[object] (MongoDB\Driver\Exception\AuthenticationException(code: 11): Authentication failed. at /var/www/redaph/vendor/mongodb/mongodb/src/Operation/DropCollection.php:108)"} []

Relevant code:

docker-compose.yml snippet

version '3':
  services:
    mongo:
      image: mongo
      restart: always
      environment:
        MONGO_INITDB_ROOT_USERNAME: root
        MONGO_INITDB_ROOT_PASSWORD: "XXX"
      volumes:
        - mdb_data:/data/db

MongoService.php snippet:

class MongoService
{
    const QUESTION_RESPONSE_COLLECTION = "question_response";
    const DATABASE_NAME = "redaph";
    private $mongoDB;

    public function __construct($mongoUrl)
    {
        $mongoClient = new MongoClient($mongoUrl);

        $this->mongoDB = $mongoClient->selectDatabase(self::DATABASE_NAME);

    }

    private function getQuestionResponseCollection(){
        return $this->mongoDB->selectCollection(self::QUESTION_RESPONSE_COLLECTION);
    }


    public function generateQuestionResponses(){

        /** code here to populate $questionResponses **/

        $collection = $this->getQuestionResponseCollection();
        #-> Authentication Fails Here
        $collection->drop();
        $results = $collection->insertMany($questionResponses);
        return $results->getInsertedCount();
    }

    public function getQuestionResponses(){
        return $this->getQuestionResponseCollection()->find([]);
    }
}

2 Answers 2

2

this issue is occur due to using the wrong username or password. please check both but remember the one thing do not use a special character in passwords because they used encoding so that maybe create an issue for you

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

2 Comments

I have faced the same issue today, I just remove a special character from the password, and now it's up. Thank you.
yeah maybe when mongo encoding method generate issue
1

After a thorough investigation on the matter, I changed how my MongoService worked and I emptied my mongo volume.

The main issue was that I had changed the password a while back after creating the container.

MongoService.php

class MongoService
{
    const QUESTION_RESPONSE_COLLECTION = "question_response";
    const DATABASE_NAME = "redaph";
    private $mongoDB;

    public function __construct($mongoUrl, $mongoUsername, $mongoPassword)
    {
        $mongoClient = new MongoClient($mongoUrl, ["authMechanism" => "SCRAM-SHA-1", "username" => $mongoUsername, "password" => $mongoPassword]);
        $this->mongoDB = $mongoClient->selectDatabase(self::DATABASE_NAME);
    }
}

services.yml

parameters:
    locale: 'en'
    env(MONGO_URL): 'mongodb://127.0.0.1:27107'
    env(MONGO_USERNAME): ''
    env(MONGO_PASSWORD): ''
services:
    App\Service\MongoService:
        arguments:
            $mongoUrl: '%env(resolve:MONGO_URL)%'
            $mongoUsername: '%env(resolve:MONGO_USERNAME)%'
            $mongoPassword: '%env(resolve:MONGO_PASSWORD)%'

1 Comment

Even I faced the issue today, I had changed the password and assumed that restarting the service would do the charm but, it was not that case. I had to stop the service and then start it again.

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.