嗨,任何人都可以帮助我在 PHP 中配置 mongodb,并提供以下详细信息,
我已尝试连接以下 MongoServer 详细信息并从集合中获取结果,但出现错误,如下所示
Server : test.server.com
Port : 27017
UserName : userdb
Password : passworddb!
Authentication DB : test-db
Collection Name : MESSAGES
我试过下面的 PHP 代码来获取集合的详细信息
<?
echo "<pre>";
$mongo = new MongoClient("mongodb://userdb:passworddb!@test.server.com:27017/test-db?readPreference=primary");
$dbname = "test-db";
var_dump($mongo);
$db = $mongo->$dbname;
var_dump($db);
$cursor = $db->MESSAGES->find();
foreach($cursor as $value){
var_dump($value);
}
?>
但我收到了类似的错误
object(MongoClient)#1 (4) {
["connected"]=>
bool(true)
["status"]=>
NULL
["server":protected]=>
NULL
["persistent":protected]=>
NULL
}
object(MongoDB)#3 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: test.server.com:27017: SASL Authentication failed on database 'test-db': Authentication failed.' in C:\xampp\htdocs\angular\mongo.php:22
Stack trace:
#0 C:\xampp\htdocs\angular\mongo.php(22): MongoCursor->rewind()
#1 {main}
thrown in C:\xampp\htdocs\angular\mongo.php on line 22
第 22 行引用foreach($cursor as $value){