Reputation: 17616
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.
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?
[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)"} []
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([]);
}
}
Upvotes: 1
Views: 13585
Reputation: 41
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
Upvotes: 2
Reputation: 17616
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)%'
Upvotes: 1