Comments (6)
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.
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.
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.
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.
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.
@jmikola wow, thanks, that was unexpected. Seems I gotta check my cluster setup
from mongo-php-library.
Related Issues (20)
- PHP+MongoDB: "Return value must be of type MongoDB\Driver\Server, null returned" HOT 3
- Can we drop `jean85/pretty-package-versions:1.2`? HOT 4
- Decision about `mongo-orchestration` folder and `Makefile` HOT 3
- [Documentation] Syntax highlighting in the method reference is not ideal HOT 1
- Memory leak on updateOne in the loop HOT 1
- Deprecated: Return type of MongoDB\Model\BSONDocument::bsonSerialize() HOT 2
- The StreamWrapper class is currently unusable without the bucket HOT 4
- Performance issue with PHP 8.2 HOT 2
- Extract mongodb-1.6.2.tgz error HOT 1
- MacOs M1 Cannot connect to Atlas After upgrade driver HOT 7
- This is a test
- $unset not properly work on multiple field? HOT 1
- Docs: missing `]` character in database->aggregate documentation HOT 1
- Question about fieldPaths (feature request?) HOT 1
- Non blocking io HOT 1
- $where is not allowed in this context HOT 2
- TLS connection with mongo cluster failed (while single host works) HOT 2
- getting result from mongodb with toArray() HOT 3
- Getting the error in driver version 1.14 HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongo-php-library.