Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

Connect to mongodb / documentdb throught ssh tunnel #11105

Closed shadoworion closed 2 years ago

shadoworion commented 2 years ago

Do you want to request a feature or report a bug? bug

What is the current behavior?

I am getting connection timeout when create connection throught ssh tunnel:

MongooseServerSelectionError: Server selection timed out after 30000 ms at NativeConnection.Connection.openUri (/Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/node_modules/mongoose/lib/connection.js:797:32) at /Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/node_modules/mongoose/lib/index.js:332:10 at /Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5 at new Promise (<anonymous>) at promiseOrCallback (/Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (/Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/node_modules/mongoose/lib/index.js:1158:10) at Mongoose.connect (/Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/node_modules/mongoose/lib/index.js:331:20) at /Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/configuration/mongo.ts:32:27 at Generator.next (<anonymous>) at /Users/daviddakhovich/Documents/Projects/Bloxtax/bloxtax3/configuration/mongo.ts:8:71 { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(1) { 'bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'rs0', commonWireVersion: 7, logicalSessionTimeoutMinutes: undefined } }

What is the expected behavior? It should not throw error cos connection exists and it works with mongoose 5.13.3 for example

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Node - 16.13.0 Mongoose - 6.1.2

IslandRhythms commented 2 years ago

Could you provide a script?

shadoworion commented 2 years ago

Could you provide a script?

Open tunnel throught the terminal:

ssh -i "key.pem" -L 27017:bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017 ubuntu@ec2-34-229-221-164.compute-1.amazonaws.com -N 

Connection script to database:

import Mongoose from "mongoose";

export const mongoConnect = async () => {
    const uri = "mongodb://username:password@localhost:27017/test?retryWrites=false";

    return await Mongoose.connect(uri, {
      ssl: true,
      sslValidate: false,
      autoIndex: true,
      connectTimeoutMS: 100000,
      keepAlive: true,
    })
      .then(() => {
        console.log("MongoDB Connected!");
        return true;
      })
      .catch((err) => {
        console.log("MongoDB isn't connected!", err);
        return false;
      });
};
IslandRhythms commented 2 years ago

try lowercase mongoose => import mongoose from "mongoose" return await mongoose.connect() if that doesn't work comment out ssl: true

shadoworion commented 2 years ago

try lowercase mongoose => import mongoose from "mongoose" return await mongoose.connect() if that doesn't work comment out ssl: true

No, nothing of that

vkarpov15 commented 2 years ago

Can you connect without the ssh tunnel, if you're connecting from a machine that's whitelisted?

shadoworion commented 2 years ago

Can you connect without the ssh tunnel, if you're connecting from a machine that's whitelisted?

Yes, It works without tunnel

vkarpov15 commented 2 years ago

Can you print out what error.reason.servers is? The [ServerDescription] part of { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(1) { 'bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017' => [ServerDescription] } may provide more info about why this is happening.

Also worth noting - Mongoose does not officially support documentdb. We answer questions and fix documentdb related bugs where possible, but we don't run tests against documentdb so there may be compatibility issues.

shadoworion commented 2 years ago

Can you print out what error.reason.servers is? The [ServerDescription] part of { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(1) { 'bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017' => [ServerDescription] } may provide more info about why this is happening.

Also worth noting - Mongoose does not officially support documentdb. We answer questions and fix documentdb related bugs where possible, but we don't run tests against documentdb so there may be compatibility issues.

Sorry for the late answer:

Map(1) {
  'bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017' => ServerDescription {
    _hostAddress: new HostAddress('bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017'),
    address: 'bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017',
    type: 'Unknown',
    hosts: [],
    passives: [],
    arbiters: [],
    tags: {},
    minWireVersion: 0,
    maxWireVersion: 0,
    roundTripTime: -1,
    lastUpdateTime: 1542349,
    lastWriteDate: 0
  }
}

I understand that mongoose does not officially support documentdb, but it worked in previous major release 5.* correctly - so it's strange

vkarpov15 commented 2 years ago

The only thing that this error message tells us is that Mongoose can't reach "bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017". Are you able to connect using the MongoDB shell via the ssh tunnel?

shadoworion commented 2 years ago

The only thing that this error message tells us is that Mongoose can't reach "bloxtax-dev.co2kajlocir5.eu-west-1.docdb.amazonaws.com:27017". Are you able to connect using the MongoDB shell via the ssh tunnel?

Yes, I can connect using MongoDB shell, MongoDB Compass, Dbeaver, Mongoose 5.13.13 via ssh tunnel. All works except mongoose 6+

Code42Cate commented 2 years ago

I have the exact same setup and issue, downgrading mongoose to 5.13.8 fixed it for me. Happy to provide more debug data if needed

vkarpov15 commented 2 years ago

@Code42Cate more data would be helpful, we haven't been able to repro this.

shadoworion commented 2 years ago

@Code42Cate more data would be helpful, we haven't been able to repro this.

I resolved the problem.

Need to add this parameter in connection string - "&directConnection=true". And add some parameters to tunnel connection:

ssh -N -i key.pem -L localhost:27017:example.eu-west-1.docdb.amazonaws.com:27017 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -o ServerAliveInterval=15 -p 22 ubuntu@server.eu-west-1.compute.amazonaws.com
toverux commented 2 years ago

@shadoworion Thanks god, I've been blocked on this problem for hours. Had the same use case of a connection over a SSH tunnel, MongoDB clients worked but not mongoose. Simply adding this fragment in the connection string made it work. Thank you very much for sharing!

pdemro commented 10 months ago

about 6 hours in the 🚽 until I found this. Thanks for the help @shadoworion . In my case, &directConnection=true was all I needed.

faerylay commented 10 months ago

@pdemro how to solved it ? can u tell me more detail please ? i'm try to connect with documentdb and nestjs in localhost

Nitesh-BB commented 6 months ago

Adding &directConnection=true in connection worked for me.

e.g mongodb://host:password@localhost:27017/test?directConnection=true

setting inside mongoose.

image
rakshitweb commented 6 months ago

I am using mongoose version "^8.0.2"

I am not able to connect to DocumentDB although i am able to connect with the same through mongo shell, compass. Can you please help me through this.

G3etting this error: Failed to connect with Mongo DB MongooseServerSelectionError: connect ETIMEDOU

vkarpov15 commented 6 months ago

@rakshitweb please open a new issue and follow the issue template. It is worth noting that mongoose doesn't officially support documentdb, but we will try to resolve your issue if we can