Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-2966

Unified topology: server selection fails when trying to connect to a remote replica set with a member whose 'host' attribute resolves to 'localhost'

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.4
    • Affects Version/s: 3.6.3
    • Component/s: None
    • Needed

      db.isMaster() result:

       

      {
          "hosts" : [ 
              "127.0.0.1:27017"
          ],
          "setName" : "rs0",
          "setVersion" : 4,
          "ismaster" : true,
          "secondary" : false,
          "primary" : "127.0.0.1:27017",
          "me" : "127.0.0.1:27017",
          "electionId" : ObjectId("7fffffff0000000000000002"),
          "lastWrite" : {
              "opTime" : {
                  "ts" : Timestamp(1608045184, 1),
                  "t" : NumberLong(2)
              },
              "lastWriteDate" : ISODate("2020-12-15T15:13:04.000Z"),
              "majorityOpTime" : {
                  "ts" : Timestamp(1608045184, 1),
                  "t" : NumberLong(2)
              },
              "majorityWriteDate" : ISODate("2020-12-15T15:13:04.000Z")
          },
          "maxBsonObjectSize" : 16777216,
          "maxMessageSizeBytes" : 48000000,
          "maxWriteBatchSize" : 100000,
          "localTime" : ISODate("2020-12-15T15:13:09.754Z"),
          "logicalSessionTimeoutMinutes" : 30,
          "connectionId" : 20,
          "minWireVersion" : 0,
          "maxWireVersion" : 8,
          "readOnly" : false,
          "ok" : 1.0,
          "$clusterTime" : {
              "clusterTime" : Timestamp(1608045184, 1),
              "signature" : {
                  "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
                  "keyId" : NumberLong(0)
              }
          },
          "operationTime" : Timestamp(1608045184, 1)
      }

       

      To reproduce:

      1. Set up a MongoDB instance listening to all IPs and running as a replica set:

       

      mongod --bind_ip_all --replSet rs0

       

      2. Configure the replica set with a single member whose host attribute is 'localhost';

       

      rs.initiate({
        _id: 'rs0',
        members: [
        { _id: 0, host: '127.0.0.1:27017' }
        ]
      });
      

      or

       

      rs.reconfig({
        _id: 'rs0',
        protocolVersion: 1,
        members: [
        { _id: 0, host: '127.0.0.1:27017' }
        ]
      });
      

      3. On a SEPARATE machine which IS NOT running a MongoDB instance, execute the following Node.js script:

       

      const { MongoClient } = require('mongodb');
      const test = async () => {
        try {
          const client = await MongoClient.connect('mongodb://<my-mongodb>', {
            useNewUrlParser: true,
            useUnifiedTopology: true
          });
          await client.close();
          console.log('Done.');
        } catch (err) {
          console.log(err);
        }
      };
      test();
      

      where <my-mongodb> is the IP address or host name of the server you set up earlier.

      The connection attempt fails with the following error:

       

      MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
          at Timeout._onTimeout (/home/ivan/Code/mongodb-test/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
          at listOnTimeout (internal/timers.js:554:17)
          at processTimers (internal/timers.js:497:7) {
        reason: TopologyDescription {
          type: 'ReplicaSetNoPrimary',
          setName: 'rs0',
          maxSetVersion: 2,
          maxElectionId: 7fffffff0000000000000002,
          servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
          stale: false,
          compatible: true,
          compatibilityError: null,
          logicalSessionTimeoutMinutes: null,
          heartbeatFrequencyMS: 10000,
          localThresholdMS: 15,
          commonWireVersion: 8
        }
      }
      

      NOTE: if 'useUnifiedTopology' is set to false, the connection attempt succeeds.

      Workaround: change the replica set configuration so the member resolves to the server's IP:

       

      rs.reconfig({
        _id: 'rs0',
        protocolVersion: 1,
        members: [
        { _id: 0, host: '<my-mongodb>:27017' }
        ]
      });

       

            Assignee:
            eric.adum@mongodb.com Eric Adum (Inactive)
            Reporter:
            ibelenski@gmail.com Ivan Belenski
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: