Search code examples
node.jsmongodbmongoosesshmongodb-atlas

connecting to mongo srv string using mongoose nestjs with ssh tunneling


I am trying to do ssh tunneling and connect to my localhost which would forward my request to atlas cluster. Note: My mongo connection string is srv string Steps I followed.

1. ssh -N -L 27017:cluster0.acsacasc.mongodb.net:27017 -i /Users/cvrg/.ssh/id_rsa [email protected]
2 a. connect to string mongodb+srv://cd-cdd:cadcdacaca@localhost/dev?retryWrites=true&w=majority using mongoose/nestjs
2 b. connect to string mongodb://cd-cdd:cadcdacaca@localhost:27017/dev?retryWrites=true&w=majority using mongoose/nestjs
2 c. connect to string mongodb+srv://cd-cdd:cadcdacaca@localhost:27017/dev?retryWrites=true&w=majority using mongoose/nestjs

Nothing out of the above seems working and i didn't found anything which shows how to do tunneling for srv cluster string. Please help

Note: I tried same with compass, and successfully able to connect but getting issues with terminal and nodejs


Solution

  • Anyone else having hard time doing this, i end up using https://github.com/sshuttle/sshuttle for tunneling/vpn. Steps to use this:

    1. connect to your jumper/bastion server using ssh.

    2. sudo sshuttle -r user@jumper_ip 0/0 -vv

    Voilla now you can use the same connection string provided by atlas cluster and would be able to connect with it.