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

Connect to mongodb / documentdb throught ssh tunnel #11105

Closed
shadoworion opened this issue Dec 15, 2021 · 19 comments
Closed

Connect to mongodb / documentdb throught ssh tunnel #11105

shadoworion opened this issue Dec 15, 2021 · 19 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@shadoworion
Copy link

shadoworion commented Dec 15, 2021

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

@shadoworion shadoworion changed the title Connect to mongodb / documentdb throw ssh tunnel Connect to mongodb / documentdb throught ssh tunnel Dec 15, 2021
@IslandRhythms
Copy link
Collaborator

Could you provide a script?

@shadoworion
Copy link
Author

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
Copy link
Collaborator

IslandRhythms commented Dec 17, 2021

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

@shadoworion
Copy link
Author

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
Copy link
Collaborator

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

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Dec 20, 2021
@shadoworion
Copy link
Author

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

Yes, It works without tunnel

@vkarpov15
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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+

@vkarpov15 vkarpov15 reopened this Jan 18, 2022
@vkarpov15 vkarpov15 added this to the 6.1.10 milestone Jan 18, 2022
@Code42Cate
Copy link

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
Copy link
Collaborator

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

@vkarpov15 vkarpov15 modified the milestones: 6.2.3, 6.2.6 Feb 16, 2022
@shadoworion
Copy link
Author

@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

@vkarpov15 vkarpov15 removed this from the 6.2.6 milestone Mar 2, 2022
@toverux
Copy link

toverux commented Jul 22, 2022

@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
Copy link

pdemro commented Dec 6, 2023

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

@faerylay
Copy link

@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
Copy link

Nitesh-BB commented Apr 23, 2024

Adding &directConnection=true in connection worked for me.

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

setting inside mongoose.
image

@rakshitweb
Copy link

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
Copy link
Collaborator

@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

@Automattic Automattic locked and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

9 participants