我正在尝试在我的Symfony应用程序中删除mongo集合,但是当我尝试删除集合时,出现“身份验证失败”的情况。我可以选择一个数据库,也可以选择一个集合。
我注入MongoService的mongoUrl连接字符串使用在docker compose中指定的相同凭据:
mongodb://root:XXX@redaph_mongo_1:27017
注意:即使我输入了错误的凭据,我仍然能够选择数据库和集合,而不会引发任何错误。
当我使用根凭据尝试删除集合时,为什么会出现“ Authenticaion Fails”问题? 如何使用根用户名和密码正确进行身份验证?
[2018-06-21 08:45:41]请求。关键:未捕获的PHP异常 MongoDB \ Driver \ Exception \ AuthenticationException:“身份验证 失败。” /var/www/redaph/vendor/mongodb/mongodb/src/Operation/DropCollection.php 第108行{“ exception”:“ [object] (MongoDB \ Driver \ Exception \ AuthenticationException(代码:11): 验证失败。在 /var/www/redaph/vendor/mongodb/mongodb/src/Operation/DropCollection.php:108)“} []
docker-compose.yml代码段
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代码段:
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([]);
}
}
答案 0 :(得分:0)
在对此问题进行彻底调查之后,我更改了MongoService的工作方式,并清空了mongo的卷。
主要问题是我在创建容器后不久更改了密码。
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)%'