GithubHelp home page GithubHelp logo

Comments (6)

jmikola avatar jmikola commented on June 23, 2024 1

I cloned https://github.com/pavelsc/mongodb-php-test to attempt reproduction.

I extracted /volumes/db-data.tar.zst to the /volumes/db-data directory. I found it necessary to remove the external: true option from the volumes.db-data configuration in docker-compose.yaml.

I started both containers with docker-compose up and then opened a shell in the mongo-php-app container to run index.php. The script appeared to connect successfully to the server in the mongo-db container.

If you have a mongo cluster you can change config config/mongo.php...

I then created an Atlas M0 cluster (server version 6.0.5) and obtained the non-SRV connection string so I could enter each of the three hosts and replica set name in the config/mongo.php file. The modified file resembled the following:

@@ -1,11 +1,13 @@
 <?php
 
 return [
-  "username"    => "root",
-  "password"    => "example",
+  "username"    => "<username>",
+  "password"    => "<password>",
   "database"    => "dev",
-  "replica_set" => "",
+  "replica_set" => 'atlas-r3glsi-shard-0',
   "host"        => [
-    "mongo-db:27017"
+    'ac-tkwmcmr-shard-00-00.07pgo2r.mongodb.net:27017',
+    'ac-tkwmcmr-shard-00-01.07pgo2r.mongodb.net:27017',
+    'ac-tkwmcmr-shard-00-02.07pgo2r.mongodb.net:27017',
   ]
 ];

I then modified src/MongoDBTest/MongoHelper.php to add the necessary ssl=true URI option (required for Atlas) and dump the result of a ping command immediately after Client construction. Diff as follows:

@@ -62,11 +62,12 @@ class MongoHelper
     }
 
     try {
-      $connStr = "mongodb://{$mongoDbAuth}{$host}/?{$replicaSet}";
+      $connStr = "mongodb://{$mongoDbAuth}{$host}/?ssl=true&{$replicaSet}";
       echo $connStr . "\r\n";
       $client                   = new Client($connStr);
       $this->contactsDb         = $client->{$this->database};
       $this->contactsCollection = $this->contactsDb->{$this->contactsCollectionName};
+      var_dump($client->admin->command(['ping' => 1])->toArray()[0]);
     } catch (Exception $e) {
       echo $e->getMessage();
       exit();

After modifying both files, I rebuilt the containers using docker-compose up --build. This also launched the mongo-db container, although it is not used in this example. I then opened a shell in the mongo-php-app container and ran index.php:

$ docker exec -it mongodb-php-test_mongo-php-app_1 bash
root@36c99abffa01:/var/www/mongodb-php-test# php index.php 
mongodb://<username>:<password>@ac-tkwmcmr-shard-00-00.07pgo2r.mongodb.net:27017,ac-tkwmcmr-shard-00-01.07pgo2r.mongodb.net:27017,ac-tkwmcmr-shard-00-02.07pgo2r.mongodb.net:27017/?ssl=true&replicaSet=atlas-r3glsi-shard-0
object(MongoDB\Model\BSONDocument)#17 (1) {
  ["storage":"ArrayObject":private]=>
  array(1) {
    ["ok"]=>
    int(1)
  }
}
Start...
Total time: 0.032139961s
root@36c99abffa01:/var/www/mongodb-php-test# 

The script had no trouble connecting to and pinging the Atlas M0 cluster.

from mongo-php-library.

alcaeus avatar alcaeus commented on June 23, 2024

Looking at the trace you provided, I can see a legacy hello being sent to the server, but I can't see a response received from the server. This indicates that while the driver was able to establish a connection to the server (ruling out SSL issues etc.), the handshake does not complete successfully. However, I'm not sure what exactly is causing this.

As an alternative to strace, could you please try to run the small ping example with the mongodb.debug ini setting enabled? You can enable this by calling php with php -d mongodb.debug=stderr ... to have the debug log printed to your error output. This limits debug information to the PHP driver and would make it a little easier to trace messages to and from the server without having a number of unrelated log messages bloat the log.

One more thing, did you try downgrading to an older MongoDB version? I've been running 6.0.3 without having this issue, and it surprises me that it would hang like this when other tools connect just fine. Figuring out whether it happens only on 6.0.3 or on older versions as well would help us in trying to locate the source of the problem.

from mongo-php-library.

pavelsc avatar pavelsc commented on June 23, 2024

Thanks for your help.
I downgraded MongoDb works just fine with the same example.

As an alternative to strace, could you please try to run the small ping example with the mongodb.debug ini setting enabled?

Custom error log remains empty as well as /var/log/php/php-errors.log

from mongo-php-library.

alcaeus avatar alcaeus commented on June 23, 2024

Custom error log remains empty as well as /var/log/php/php-errors.log

That is odd. Running the code as above should definitely produce some output. Here is an example from my machine:

$ php -d mongodb.debug=stderr -r "(new MongoDB\Driver\Manager)->executeCommand('test', new MongoDB\Driver\Command(['ping' => 1]));"
[2023-02-20T11:45:58.820582+00:00]     PHONGO: DEBUG   > Connection string: 'mongodb://127.0.0.1/'
[...]

Even without running a command there should be some output, as instantiating a Manager instance will create log output even when it doesn't connect to a cluster.

from mongo-php-library.

pavelsc avatar pavelsc commented on June 23, 2024

FInally I sorted it out. It doesn't connect when the connection string is for cluster, I mean it looks like this:

mongodb://10.60.75.73:27017,10.60.75.74:27017,10.60.75.75:27017/?replicaSet=rs0

But it's ok when I connect to a single server of a cluster:

mongodb://10.60.75.73:27017

I created a repo https://github.com/pavelsc/mongodb-php-test to reproduce. It consists of two docker containers: mongo-db and mongo-php-app. mongo-php-app connects to a mongo-db single instance by default and works correctly. If you have a mongo cluster you can change config config/mongo.php to something like this:

<?php

return [
  "username"    => "",
  "password"    => "",
  "database"    => "dev", // dev should exist or pick your own, also there is a volume db-data for mongo in the repo
  "replica_set" => "rs0",
  "host"        => [
    "your-server-1:27017",
    "your-server-2:27017",
    "your-server-3:27017"
  ]
];

then build mongo-php-app and run php index.php in it - nothing happens.
If you leave only one server in the config and rerun php index.php - it will work (of course you need to remove replica_set).
And the weirdest thing - the same config with 3 servers connects to the same mongo cluster if I run php on Windows system.

from mongo-php-library.

pavelsc avatar pavelsc commented on June 23, 2024

@jmikola wow, thanks, that was unexpected. Seems I gotta check my cluster setup

from mongo-php-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.