7
votes

I have a MongoDb hosted locally in my machine and runs successfully in port localhost:27017. The database has a user name and password with a collection named, "testDb". In the code, I am able to access the database successfully using localhost.

I am trying to access this MongoDb from a remote desktop using ngrok. I hace implemented the port forwarding and the following response is shown in the command prompt.

Forwarding https://5e825c82.ngrok.io -> http://localhost:27017

I also tried changing the port => Forwarding https://5e825c82.ngrok.io -> http://localhost:28017

Both ports failed with the following Error message: The connection to http://5e825c82.ngrok.io was successfully tunneled to your ngrok client, but the client failed to establish a connection to the local address localhost:28017. Make sure that a web service is running on localhost:28017 and that it is a valid address. The error encountered was: dial tcp [::1]:28017: connectex: No connection could be made because the target machine actively refused it.

// Works fine
MongoClient client = new MongoClient("mongodb://admin:admin@localhost:27017/testDb");

// Fails:
MongoClient client = new MongoClient("mongodb://admin:[email protected]/testDb");

I would like to know how to establish a connection to the MongoDb with ngrok.

1
According to the error message ngrok tries to establish the local connection in IPv6. Make sure it is enabled in your localhost or configure ngrok to use IPv4 only.marekful
I am not sure how to make that change. Any quicklink will help.deeepss
I think this would be related to mongodb being it's own protocol, so there's no http layer. Try creating a TCP tunnel using ngrok. I think that will work for you.kapad

1 Answers

6
votes

MongoDB uses TCP not HTTP.

Try following command :

ngrok tcp 27017

(note the tcp, not http which I think is what you used)


There are a couple of extra steps you need to do for some reason when you use TCP, and ngrok will prompt you and tell you what you need to do when you try the above command.

  1. Sign up for an ngrok account at https://dashboard.ngrok.com/get-started
  2. Run locally the command shown on this page in the box 3. Connect your account (eg. ngrok authtoken 123ABC456ETC)

enter image description here

  1. Now try that command again (ngrok tcp 27017)