coinbase / mongobetween

Apache License 2.0
107 stars 45 forks source link

Cannot connect to mongobetween #30

Closed rshov closed 2 years ago

rshov commented 3 years ago

Hello, thank you for open sourcing this project, it seems to be just what I need for my Next.js application which deploys my API as AWS Lambda functions, causing way too many database connections.

Unfortunately I can't seem to get mongobetween working. It connects to my database ok, but my application is not able to connect to mongobetween. I'm hoping to get some help.

I'm attempting to use mongobetween to proxy to a MongoDB Atlas database at version 4.4.6 (it's a 3-node replica set with no sharding). My application uses Mongoose 5.11.8 (which uses the MongoDB Node.js driver, I'm assuming v3.6 but I couldn't confirm this). I spun up a basic server on DigitalOcean and installed Go v1.16.5 with the latest mongobetween.

Command used to start mongobetween: ./go/bin/mongobetween -pretty -loglevel debug ":27016=mongodb+srv://username:password@server.mongodb.net/database?maxpoolsize=50&label=cluster0"

MongoBetween seems to connect to my Atlas database ok, but my application receives the following error:

MongoServerSelectionError: The client metadata document may only be sent in the first isMaster
    at Timeout._onTimeout (C:\Users\user\Documents\GitHub\app\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: 'Unknown',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) {
      '134.209.211.213:27016' => ServerDescription {
        address: '134.209.211.213:27016',
        error: MongoError: The client metadata document may only be sent in the first isMaster
            at MessageStream.messageHandler (C:\Users\user\Documents\GitHub\app\node_modules\mongodb\lib\cmap\connection.js
            at MessageStream.emit (events.js:315:20)
            at processIncomingData (C:\Users\user\Documents\GitHub\app\node_modules\mongodb\lib\cmap\message_stream.js:144:
            at MessageStream._write (C:\Users\user\Documents\GitHub\app\node_modules\mongodb\lib\cmap\message_stream.js:42:
            at writeOrBuffer (internal/streams/writable.js:358:12)
            at MessageStream.Writable.write (internal/streams/writable.js:303:10)
            at Socket.ondata (internal/streams/readable.js:719:22)
            at Socket.emit (events.js:315:20)
            at addChunk (internal/streams/readable.js:309:12)
            at readableAddChunk (internal/streams/readable.js:284:9)
            at Socket.Readable.push (internal/streams/readable.js:223:10)
            at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
          operationTime: Timestamp {
            _bsontype: 'Timestamp',
            low_: 1,
            high_: 1623700272
          },
          ok: 0,
          code: 186,
          codeName: 'ClientMetadataCannotBeMutated',
          '$clusterTime': { clusterTime: [Timestamp], signature: [Object] }
        },
        roundTripTime: -1,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      }
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

I then tried connecting directly from the mongo shell and got Authentication failed:

MongoDB shell version v4.4.4
connecting to: mongodb://mongobetween_server:27016/database?compressors=disabled&gssapiServiceName=mongodb
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

Could this be from using too new of a Mongo client version? Thank you so much for any help you can provide.

Raymond

rdeavilafloqast commented 2 years ago

I'm seeing the same issue but with the node driver and not the shell.

"The client metadata document may only be sent in the first isMaster"

rdeavilafloqast commented 2 years ago

@rshov I've opened a PR that I believe will fix your issue: https://github.com/coinbase/mongobetween/pull/41

mdehoog commented 2 years ago

@rshov #41 has been merged, let us know if that fixes your issue

rshov commented 2 years ago

Sorry for not getting to this sooner, but I've been trying with the update and still cannot get it to work.

I start mongobetween with the following command:

./go/bin/mongobetween ":27017=mongodb+srv://username:password@prod.pips3.gcp.mongodb.net/dbname?maxpoolsize=10&label=cluster0"

That seems to connect ok to my Atlas database.

{"level":"info","ts":1640282695.3266177,"caller":"mongo/mongo.go:61","message":"Connect"}
{"level":"info","ts":1640282695.3398523,"caller":"mongobetween@v0.0.13/mongobetween.go:79","message":"Running"}
{"level":"info","ts":1640282700.3402007,"caller":"mongo/topology_monitor.go:19","message":"Topology changed","old_kind":"ReplicaSetNoPrimary","new_kind":"ReplicaSetWithPrimary","old_address_0":"prod-shard-00-00.pips3.gcp.mongodb.net:27017","old_kind_0":"Unknown","old_address_1":"prod-shard-00-01.pips3.gcp.mongodb.net:27017","old_kind_1":"Unknown","old_address_2":"prod-shard-00-02.pips3.gcp.mongodb.net:27017","old_kind_2":"Unknown","new_address_0":"prod-shard-00-00.pips3.gcp.mongodb.net:27017","new_kind_0":"RSSecondary","new_address_1":"prod-shard-00-01.pips3.gcp.mongodb.net:27017","new_kind_1":"RSSecondary","new_address_2":"prod-shard-00-02.pips3.gcp.mongodb.net:27017","new_kind_2":"RSPrimary"}

Then I run my application which tries to connect to mongobetween with this url (I've changed the actual IP here):

mongodb+srv://username:password@159.223.150.30/dbname

But my application gets this error:

Error: querySrv ENOTFOUND _mongodb._tcp.159.223.150.30
    at QueryReqWrap.onresolve [as oncomplete] (node:dns:213:19) {
  errno: undefined,
  code: 'ENOTFOUND',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.159.223.150.30'
}
rshov commented 2 years ago

If I change the url that my app uses to connect to mongobetween, and take away the "+srv" part so it is:

mongodb://username:password@159.223.150.30/dbname

Then I see log output in mongobetween:

{"level":"info","ts":1640282935.0947332,"caller":"proxy/proxy.go:164","message":"Accept","cluster":"cluster0","remote_address":"40.200.3.16:54671"}
{"level":"info","ts":1640282935.2335684,"caller":"proxy/proxy.go:164","message":"Accept","cluster":"cluster0","remote_address":"40.200.3.16:54673"}
{"level":"info","ts":1640282935.5051587,"caller":"proxy/proxy.go:168","message":"Close","cluster":"cluster0","remote_address":"40.200.3.16:54671"}
{"level":"info","ts":1640282935.5087996,"caller":"proxy/proxy.go:168","message":"Close","cluster":"cluster0","remote_address":"40.200.3.16:54673"}

But my application shows an error:

MongoError: Authentication failed.
    at MessageStream.messageHandler (/Users/raymond/Documents/GitHub/app/node_modules/mongodb/lib/cmap/connection.js:272:20)
    at MessageStream.emit (node:events:390:28)
    at processIncomingData (/Users/raymond/Documents/GitHub/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/Users/raymond/Documents/GitHub/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1640283316 },
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1640283316 },
    signature: { hash: [Binary], keyId: [Long] }
  }
}

Any ideas? I would really love to use mongobetween, but am really struggling to get it up and running.

Thanks for any help!

boredland commented 5 months ago

@rshov did you ever manage to use it?

rshov commented 5 months ago

@rshov did you ever manage to use it?

No, I never did get it working unfortunately