Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to connect to a single member replica set through PHP library #1557

Open
alterphp opened this issue Dec 17, 2024 · 5 comments
Open

Comments

@alterphp
Copy link

alterphp commented Dec 17, 2024

Bug Report

I'm using a single member replica set MongoDB cluster in my development environment (and also in production in some cases), but replica set is well configured. With mongosh, I can connect to the single member by spefying the replicaSet URI parameter and it works well.

Using PHP driver, if I set a replicaSet option, I get an error No suitable servers found (serverSelectionTryOnce set): [connection refused calling hello on '127.0.0.1:27017']. Topology type: ReplicaSetNoPrimary. But through mongosh, I can check that my single member is obviously seen as PRIMARY :

{
  set: 'rs_dev',
  date: ISODate('2024-12-17T17:09:57.780Z'),
  myState: 1,
  term: Long('16'),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 1,
  writeMajorityCount: 1,
  votingMembersCount: 1,
  writableVotingMembersCount: 1,
  optimes: { ... },
  lastStableRecoveryTimestamp: Timestamp({ t: 1734455354, i: 1 }),
  electionCandidateMetrics: { ... },
  members: [
    {
      _id: 0,
      name: '127.0.0.1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 3284,
      optime: { ts: Timestamp({ t: 1734455394, i: 1 }), t: Long('16') },
      optimeDate: ISODate('2024-12-17T17:09:54.000Z'),
      lastAppliedWallTime: ISODate('2024-12-17T17:09:54.533Z'),
      lastDurableWallTime: ISODate('2024-12-17T17:09:54.533Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1734452114, i: 1 }),
      electionDate: ISODate('2024-12-17T16:15:14.000Z'),
      configVersion: 1,
      configTerm: 16,
      self: true,
      lastHeartbeatMessage: ''
    }
  ],
  ok: 1,
  '$clusterTime': {...},
  operationTime: Timestamp({ t: 1734455394, i: 1 })
}

Environment

I'm not using TLS in dev environment, MongoDB is a Docker container (version 7.0) configured with configured rs_dev ReplicaSet.

Library mongodb/mongodb is 1.19.1
PHP is 8.1
PHP extension is 1.20.1

It seems to be handled the same way by NodeJS driver (same issue described here : https://stackoverflow.com/questions/69531947/replicasetnoprimary-error-although-primary-exists).

Please tell me if this is expected behavior, although mongosh seems OK with single member replica set ?

@alcaeus
Copy link
Member

alcaeus commented Dec 18, 2024

I just tested one of the example scripts with a single-member replica set and was able to connect to it without an issue. Since you mentioned this is in docker, would you be able to share the setup you were using so we can try to reproduce it?

@alterphp
Copy link
Author

alterphp commented Dec 18, 2024

Hello @alcaeus,

And thank you for taking a look !

Here is the Dockerfile of the built image :

FROM mongo:7.0

# Generates a keyfile, mandatory to run as a replica set
RUN openssl rand -base64 756 > /etc/mongo-keyfile
RUN chmod 400 /etc/mongo-keyfile
RUN chown mongodb:mongodb /etc/mongo-keyfile

# Any JS or Shell file found in docker-entrypoint-initdb.d directory is executed
# when container is run the first time
# @see https://hub.docker.com/_/mongo
COPY initiateReplSet.js ./docker-entrypoint-initdb.d/

CMD ["mongod", "--replSet", "rs_dev", "--keyFile", "/etc/mongo-keyfile", "--bind_ip", "0.0.0.0"]

the file initiateReplSet.js contains only one instruction rs.initiate().

The container is served in a Docker Compose stack, with this service definition :

    mongo:
        image: registry.gitlab.com/mycompany/tools/docker-images/mongo/dev:7.0
        ports:
            - '27017:27017'
        volumes:
            - mongo_data:/data/db
        networks:
            - database
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: root

The connection string configured in my local web app MONGODB_URL=mongodb://root:root@mongo:27017 and the replicaSet option in the doctrine_mongodb configuration. If I ommit the replica set option in connection configuration, it works fine (but it does not connect to the replica set, it's a direct connection).

And the ConnectionTimeoutError on read action, through Symfony with Doctrine Mongo ODM :
An exception has been thrown during the rendering of a template ("No suitable servers found (serverSelectionTryOnce set): [connection refused calling hello on '127.0.0.1:27017']").

with the head of stack trace

MongoDB\Driver\Exception\ConnectionTimeoutException:
No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling hello on '127.0.0.1:27017']

  at vendor/mongodb/mongodb/src/functions.php:599
  at MongoDB\Driver\Manager->selectServer(object(ReadPreference))
     (vendor/mongodb/mongodb/src/functions.php:599)
  at MongoDB\select_server(object(Manager), array('sort' => array('createdAt' => -1), 'limit' => 100, 'skip' => 0, 'readConcern' => object(ReadConcern), 'readPreference' => object(ReadPreference), 'typeMap' => array('root' => 'array', 'document' => 'array')))
     (vendor/mongodb/mongodb/src/Collection.php:646)
  at MongoDB\Collection->find(array('origin' => 'mobile-app'), array('sort' => array('createdAt' => -1), 'limit' => 100, 'skip' => 0, 'readConcern' => object(ReadConcern), 'readPreference' => object(ReadPreference), 'typeMap' => array('root' => 'array', 'document' => 'array')))
     (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Query/Query.php:454)
  at Doctrine\ODM\MongoDB\Query\Query->runQuery()
     (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Query/Query.php:205)
  at Doctrine\ODM\MongoDB\Query\Query->execute()
     (vendor/pagerfanta/pagerfanta/lib/Adapter/Doctrine/MongoDBODM/QueryAdapter.php:56)
  at Pagerfanta\Doctrine\MongoDBODM\QueryAdapter->getSlice(0, 100)
     (vendor/pagerfanta/pagerfanta/lib/Core/Pagerfanta.php:337)
  at Pagerfanta\Pagerfanta->getCurrentPageResultsFromAdapter()
     (vendor/pagerfanta/pagerfanta/lib/Core/Pagerfanta.php:323)
  at Pagerfanta\Pagerfanta->getCurrentPageResults()
     (vendor/twig/twig/src/Extension/CoreExtension.php:1754)
  at Twig\Extension\CoreExtension::getAttribute(object(Environment), object(Source), object(Pagerfanta), 'currentPageResults', array(), 'any', false, false, false, 116)

@jmikola
Copy link
Member

jmikola commented Dec 20, 2024

The connection string configured in my local web app MONGODB_URL=mongodb://root:root@mongo:27017 and the replicaSet option in the doctrine_mongodb configuration. If I ommit the replica set option in connection configuration, it works fine (but it does not connect to the replica set, it's a direct connection).

I know you mentioned the "rs_dev" is used as the replica set name, but given the behavior you reported I expect the correct name is not actually being passed to the driver.

You can also test this locally using SDAM logging and tweaking the connection string. Below are two snippets of running examples/sdam_logger.php on a local single-member replica set (named "rs0") with and without the correct replicaSet connection string value. In both cases we observe the host in the seed list added to the topology, but in the example with the incorrect replicaSet value we see it removed since it does not match the setName reported in the host's hello monitoring response.

Correct replicaSet parameter

$ MONGODB_URI='mongodb://127.0.0.1:27080/?replicaSet=rs0' php sdam_logger.php 
topologyOpening: 6765919ee6bcb9197e0a6130 was opened

topologyChanged: 6765919ee6bcb9197e0a6130 changed from Unknown to ReplicaSetNoPrimary

serverOpening: 127.0.0.1:27080 was added to topology 6765919ee6bcb9197e0a6130

serverHeartbeatStarted: 127.0.0.1:27080 heartbeat started

serverHeartbeatSucceeded: 127.0.0.1:27080 heartbeat succeeded after 492µs
reply: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "6765904e0fd39853caba155d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27080" ], "setName" : "rs0", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27080", "me" : "127.0.0.1:27080", "electionId" : { "$oid" : "7fffffff0000000000000001" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } }, "t" : 1 }, "lastWriteDate" : { "$date" : "2024-12-20T15:47:38Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } }, "t" : 1 }, "majorityWriteDate" : { "$date" : "2024-12-20T15:47:38Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2024-12-20T15:47:42.917Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 9, "minWireVersion" : 0, "maxWireVersion" : 25, "readOnly" : false, "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType" : "00" } }, "keyId" : 0 } }, "operationTime" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } } }

serverChanged: 127.0.0.1:27080 changed from Unknown to RSPrimary
previous hello response: { }
new hello response: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "6765904e0fd39853caba155d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27080" ], "setName" : "rs0", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27080", "me" : "127.0.0.1:27080", "electionId" : { "$oid" : "7fffffff0000000000000001" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } }, "t" : 1 }, "lastWriteDate" : { "$date" : "2024-12-20T15:47:38Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } }, "t" : 1 }, "majorityWriteDate" : { "$date" : "2024-12-20T15:47:38Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2024-12-20T15:47:42.917Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 9, "minWireVersion" : 0, "maxWireVersion" : 25, "readOnly" : false, "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType" : "00" } }, "keyId" : 0 } }, "operationTime" : { "$timestamp" : { "t" : 1734709658, "i" : 1 } } }

topologyChanged: 6765919ee6bcb9197e0a6130 changed from ReplicaSetNoPrimary to ReplicaSetWithPrimary

topologyClosed: 6765919ee6bcb9197e0a6130 was closed

Incorrect replicaSet parameter

$ MONGODB_URI='mongodb://127.0.0.1:27080/?replicaSet=rs1' php sdam_logger.php 
topologyOpening: 6765908f89a783b4a8029a40 was opened

topologyChanged: 6765908f89a783b4a8029a40 changed from Unknown to ReplicaSetNoPrimary

serverOpening: 127.0.0.1:27080 was added to topology 6765908f89a783b4a8029a40

serverHeartbeatStarted: 127.0.0.1:27080 heartbeat started

serverHeartbeatSucceeded: 127.0.0.1:27080 heartbeat succeeded after 511µs
reply: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "6765904e0fd39853caba155d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27080" ], "setName" : "rs0", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27080", "me" : "127.0.0.1:27080", "electionId" : { "$oid" : "7fffffff0000000000000001" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } }, "t" : 1 }, "lastWriteDate" : { "$date" : "2024-12-20T15:43:08Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } }, "t" : 1 }, "majorityWriteDate" : { "$date" : "2024-12-20T15:43:08Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2024-12-20T15:43:11.969Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 8, "minWireVersion" : 0, "maxWireVersion" : 25, "readOnly" : false, "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType" : "00" } }, "keyId" : 0 } }, "operationTime" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } } }

serverChanged: 127.0.0.1:27080 changed from Unknown to RSPrimary
previous hello response: { }
new hello response: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "6765904e0fd39853caba155d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27080" ], "setName" : "rs0", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27080", "me" : "127.0.0.1:27080", "electionId" : { "$oid" : "7fffffff0000000000000001" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } }, "t" : 1 }, "lastWriteDate" : { "$date" : "2024-12-20T15:43:08Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } }, "t" : 1 }, "majorityWriteDate" : { "$date" : "2024-12-20T15:43:08Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2024-12-20T15:43:11.969Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 8, "minWireVersion" : 0, "maxWireVersion" : 25, "readOnly" : false, "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType" : "00" } }, "keyId" : 0 } }, "operationTime" : { "$timestamp" : { "t" : 1734709388, "i" : 1 } } }

serverClosed: 127.0.0.1:27080 was removed from topology 6765908f89a783b4a8029a40

topologyChanged: 6765908f89a783b4a8029a40 changed from ReplicaSetNoPrimary to ReplicaSetNoPrimary

PHP Fatal error:  Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set). Topology type: ReplicaSetNoPrimary in...

@pece-kiplin
Copy link

pece-kiplin commented Jan 14, 2025

Hello @jmikola and sorry for late response, I missed your response notification.

I tested the sdam_logger.php, trying to connect to my docker MongoDB container set with replica set named rs_dev.

Without specifying replicaSet name in the connection string dcrun -e MONGODB_URI='mongodb://root:root@mongo:27017' php php public/sdam_logger.php

topologyOpening: 67867021dba3a41f5c0b96e0 was opened

topologyChanged: 67867021dba3a41f5c0b96e0 changed from Unknown to Single

serverOpening: mongo:27017 was added to topology 67867021dba3a41f5c0b96e0

serverHeartbeatStarted: mongo:27017 heartbeat started

serverHeartbeatSucceeded: mongo:27017 heartbeat succeeded after 492µs
reply: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "67866cb96ddf766440c3bd3d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27017" ], "setName" : "rs_dev", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27017", "me" : "127.0.0.1:27017", "electionId" : { "$oid" : "7fffffff0000000000000018" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } }, "t" : 24 }, "lastWriteDate" : { "$date" : "2025-01-14T14:09:36Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } }, "t" : 24 }, "majorityWriteDate" : { "$date" : "2025-01-14T14:09:36Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2025-01-14T14:09:37.853Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 13, "minWireVersion" : 0, "maxWireVersion" : 21, "readOnly" : false, "saslSupportedMechs" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ], "speculativeAuthenticate" : { "conversationId" : 1, "done" : false, "payload" : { "$binary" : { "base64" : "cj16bW9rM0ZwQndyOWRxdkh1M0p5UURWL1ViM01Id3M0S2l2UXRlTTZsYzE0OHJZWjBubDJWOVR3OG5BcTI2b2I4LHM9TW15WmZvWmVxNjRqSW9sYVh6bE5IazhVSWRDSEdCK29Wdk5GTUE9PSxpPTE1MDAw", "subType" : "00" } } }, "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "uDtMIrP67LjbTSDjlBG/NeYeFNY=", "subType" : "00" } }, "keyId" : 7449007904553697281 } }, "operationTime" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } } }

serverChanged: mongo:27017 changed from Unknown to RSPrimary
previous hello response: { }
new hello response: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "67866cb96ddf766440c3bd3d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27017" ], "setName" : "rs_dev", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27017", "me" : "127.0.0.1:27017", "electionId" : { "$oid" : "7fffffff0000000000000018" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } }, "t" : 24 }, "lastWriteDate" : { "$date" : "2025-01-14T14:09:36Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } }, "t" : 24 }, "majorityWriteDate" : { "$date" : "2025-01-14T14:09:36Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2025-01-14T14:09:37.853Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 13, "minWireVersion" : 0, "maxWireVersion" : 21, "readOnly" : false, "saslSupportedMechs" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ], "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "uDtMIrP67LjbTSDjlBG/NeYeFNY=", "subType" : "00" } }, "keyId" : 7449007904553697281 } }, "operationTime" : { "$timestamp" : { "t" : 1736863776, "i" : 1 } } }

topologyChanged: 67867021dba3a41f5c0b96e0 changed from Single to Single

topologyClosed: 67867021dba3a41f5c0b96e0 was closed

And by specifying the replicaSet name in the connection string : dcrun -e MONGODB_URI='mongodb://root:root@mongo:27017?replicaSet=rs_dev' php php public/sdam_logger.php

topologyOpening: 67867102e4a9b2151108c700 was opened

topologyChanged: 67867102e4a9b2151108c700 changed from Unknown to ReplicaSetNoPrimary

serverOpening: mongo:27017 was added to topology 67867102e4a9b2151108c700

serverHeartbeatStarted: mongo:27017 heartbeat started

serverHeartbeatSucceeded: mongo:27017 heartbeat succeeded after 623µs
reply: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "67866cb96ddf766440c3bd3d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27017" ], "setName" : "rs_dev", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27017", "me" : "127.0.0.1:27017", "electionId" : { "$oid" : "7fffffff0000000000000018" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } }, "t" : 24 }, "lastWriteDate" : { "$date" : "2025-01-14T14:13:16Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } }, "t" : 24 }, "majorityWriteDate" : { "$date" : "2025-01-14T14:13:16Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2025-01-14T14:13:22.224Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 14, "minWireVersion" : 0, "maxWireVersion" : 21, "readOnly" : false, "saslSupportedMechs" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ], "speculativeAuthenticate" : { "conversationId" : 1, "done" : false, "payload" : { "$binary" : { "base64" : "cj1Jb0xyWjF5SHQ0SGNCTXcvTGlSQ3lSOG1xQ2FLSjhsZ1F5NS92eUtsZHluWmtEdnc5ZkMrZ29OK3BWQ0xDUi80LHM9TW15WmZvWmVxNjRqSW9sYVh6bE5IazhVSWRDSEdCK29Wdk5GTUE9PSxpPTE1MDAw", "subType" : "00" } } }, "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "vBQW5Nr8Drw/w5p1V0JsNoYi6O8=", "subType" : "00" } }, "keyId" : 7449007904553697281 } }, "operationTime" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } } }

serverChanged: mongo:27017 changed from Unknown to RSPrimary
previous hello response: { }
new hello response: { "helloOk" : true, "topologyVersion" : { "processId" : { "$oid" : "67866cb96ddf766440c3bd3d" }, "counter" : 6 }, "hosts" : [ "127.0.0.1:27017" ], "setName" : "rs_dev", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "127.0.0.1:27017", "me" : "127.0.0.1:27017", "electionId" : { "$oid" : "7fffffff0000000000000018" }, "lastWrite" : { "opTime" : { "ts" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } }, "t" : 24 }, "lastWriteDate" : { "$date" : "2025-01-14T14:13:16Z" }, "majorityOpTime" : { "ts" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } }, "t" : 24 }, "majorityWriteDate" : { "$date" : "2025-01-14T14:13:16Z" } }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : { "$date" : "2025-01-14T14:13:22.224Z" }, "logicalSessionTimeoutMinutes" : 30, "connectionId" : 14, "minWireVersion" : 0, "maxWireVersion" : 21, "readOnly" : false, "saslSupportedMechs" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ], "ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "vBQW5Nr8Drw/w5p1V0JsNoYi6O8=", "subType" : "00" } }, "keyId" : 7449007904553697281 } }, "operationTime" : { "$timestamp" : { "t" : 1736863996, "i" : 1 } } }

serverOpening: 127.0.0.1:27017 was added to topology 67867102e4a9b2151108c700

serverClosed: mongo:27017 was removed from topology 67867102e4a9b2151108c700

topologyChanged: 67867102e4a9b2151108c700 changed from ReplicaSetNoPrimary to ReplicaSetNoPrimary

serverHeartbeatStarted: 127.0.0.1:27017 heartbeat started

serverHeartbeatFailed: 127.0.0.1:27017 heartbeat failed after 86727435661µs
error: MongoDB\Driver\Exception\RuntimeException(5): connection refused calling hello on '127.0.0.1:27017'

serverChanged: 127.0.0.1:27017 changed from Unknown to Unknown
previous hello response: { }
new hello response: { }

topologyChanged: 67867102e4a9b2151108c700 changed from ReplicaSetNoPrimary to ReplicaSetNoPrimary


Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling hello on '127.0.0.1:27017']. Topology type: ReplicaSetNoPrimary in /var/www/symfony/vendor/mongodb/mongodb/src/functions.php:599
Stack trace:
#0 /var/www/symfony/vendor/mongodb/mongodb/src/functions.php(599): MongoDB\Driver\Manager->selectServer(NULL)
#1 /var/www/symfony/vendor/mongodb/mongodb/src/Database.php(247): MongoDB\select_server(Object(MongoDB\Driver\Manager), Array)
#2 /var/www/symfony/public/sdam_logger.php(137): MongoDB\Database->command(Array)
#3 {main}
  thrown in /var/www/symfony/vendor/mongodb/mongodb/src/functions.php on line 599

I get the same issue.

I looks like, the Driver get a response from the hello handshake that tells "primary is not mongo:27017" (the mapped service name in the Docker compose config), primary is located at 127.0.0.1:27017 (Docker compose config does not know this IP mapping). What do you think ?

@jmikola
Copy link
Member

jmikola commented Jan 15, 2025

The error seems to be caused by disagreement between host names used in the connection string and those in the replica set configuration (which is used in hello responses). As part of the validation for replica set connections (when the replicaSet URI option is specified), drivers require each node to report the same replica set name and expect to connect using host names found in the replica set configuration. This is why your second log snippet reports the original connection being closed in favor of a new connection using the hostname found in the hello response. This is discussed in more detail in Clients use the hostnames listed in the replica set config, not the seed list within the Server Discovery and Monitoring spec.

You should be able to resolve this with additional hostname definitions on the driver side, which will allow the connection string to use the same hostnames found in the RS configuration. https://stackoverflow.com/a/68674898 outlines one approach for doing so.


Note: the reason your first example ("Without specifying replicaSet name") succeeds is because the driver establishes a direct connection to the replica set member by virtue of the seed list including only one host and there being no replicaSet option specified. That avoids any validation for hostname and replica set name checks. The directConnection URI option discussed in the SDAM spec affords more explicit control over this if you're curious, and the spec also talks about how that interacts with replicaSet if both options are used together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants