0

Hi Can anyone help with me for Configuring the mongodb in PHP with below details,

I have tried connecting with below MongoServer details and get the results from the collections, But I am getting error as shown below

Server : test.server.com
Port : 27017
UserName : userdb
Password : passworddb!
Authentication DB : test-db
Collection Name : MESSAGES

I have tried below PHP Code to get the details of the collection

   <?
        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);
    }
    ?>

But I am getting error like

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

Line 22 Referes to foreach($cursor as $value){

Mahesh G
  • 1,226
  • 4
  • 30
  • 57

1 Answers1

0

I found the solution by changing the code as below. Now I am able to fetch all the records in the Collection.

<?
$mongo=new MongoClient("mongodb://xhomuser:passworddb!@test.server.com:27017/test-db?readPreference=primary");
echo "<pre>";

$db = $mongo->selectDB("test-db");

$collection = $db->selectCollection("MESSAGES");

$cursor = $collection->find();
foreach ($cursor as $document) {
    echo '"_id": '.$document["_id"]."<br />";
    echo '"accountnumber": '.$document["ACCOUNTNUMBER"]."<br />";
    echo '"STATUS": '.$document["STATUS"]."<br />";
} 

?>
Mahesh G
  • 1,226
  • 4
  • 30
  • 57