1

Thought it should be straight forward but I have a very hard time figuring out the following:

I got mongodb connection string: mongodb://user:[email protected]:27017/?authSource=admin

I want to be able to connect to mongo from localhost at port 1234 by doing: mongo localhost:1234 The solution is to create a tunnel, but nothing I do works for me.

I tried the following command:

ssh -L 1234:localhost:27017 user:[email protected] -p 27017

Please help me understand what I am doing wrong.

1
  • Mongo uses connection pools. When you attempt to establish a connection via the default port 27017 the pool will redirect to a different unused port. This allows multiple concurrent connections. This is probably causing you grief in your efforts to tunnel. Commented Oct 13, 2021 at 15:40

3 Answers 3

2

You need to have a unix user on 123.123.123.111

ssh -L 1234:localhost:27017 [email protected]

Then your local mongodb connection string is : mongodb://user:password@localhost:1234/?authSource=admin

Sign up to request clarification or add additional context in comments.

Comments

1

MongoDB and ssh use different protocols, so you can't use ssh to connect directly to a mongod process.

If you want to use an ssh tunnel you will first need to have an account on the destination machine, and use that account's credentials with ssh to connect to port 22 (assuming default port). The mongod username/password will probably not be valid for ssh.

Once the tunnel is established, you would connect to the local port using a driver or mongo shell using the connection string:

mongodb://user:[email protected]:1234/?authSource=admin

Comments

0

You can use SSH tunneling to access your MongoDB.

Linux/mac

ssh [email protected] -L 27018:127.0.0.1:27017 -N

Connection String:

mongodb://localhost:27018/betterdatabase?authSource=admin&retryWrites=true&w=majority&directConnection=true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.