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([]);
}
}