connecting to mongo srv string using mongoose nestjs with ssh tunneling - node.js

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 ubuntu#2.1.5.2
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

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.

Related

SSH tunnle to mongodb using mongodb connection string

Thought it should be straight forward but I have a very hard time figuring out the following:
I got mongodb connection string: mongodb://user:password#123.123.123.111: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:password#123.123.123.111 -p 27017
Please help me understand what I am doing wrong.
You need to have a unix user on 123.123.123.111
ssh -L 1234:localhost:27017 UNIXuser#123.123.123.111
Then your local mongodb connection string is : mongodb://user:password#localhost:1234/?authSource=admin
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:password#127.0.0.1:1234/?authSource=admin

Cloud9 DB to mLab DB "Failed: error connecting to db server: no reachable servers"

I've looked at the similar questions but all are running local mongodb instances and the solutions are over my head.
I'm a newbie trying to port my database info from my Cloud 9 mongodb installation to an mLab database. I'm using the exact pre-loaded string that mLab provides you, and getting "Failed: error connecting to db server: no reachable servers"
mongoimport -h ds113892.mlab.com:13692 -d yelpcamp -c campgrounds -u <myusername> -p <mypassword> --file ~/workspace/YelpCamp/FinalDeployed/campexport.json
I can connect to the server with a mongo shell, but above command doesn't find a server. I've tried it with and without mongod running in the background. Super confused! Would appreciate any help.

Need to establish an SSH tunnel in Node.js or do it on the server

I'm going to connect to a remote database using an SSH tunnel. Currently, I'm not sure how long the server is alive, but I will check with my provider.
However, is it safe to assume that I will be able to connect to the database by running this command: ssh -fNg -L 1337:database-name:3306 user#ssh-server.com once on the client? Or do I need to establish a connection all the time? I know there exist some npm modules to connect via SSH.
Thanks in advance!

PostgreSQL version in connection string

I have two versions of postgresql installed, 9.4 and 9.5. I am using npm's pg to connect to the server using a postgres connection string. From CLI, I can connect to 9.5 by using the --cluter option like so: psql --cluster 9.5/main. In postgres' documentation for the connection string, they show one of the querystring parameters you can use is options:
options - Adds command-line options to send to the server at run-time
I realize this means configuration parameters for the server, not the client. Nonetheless, I tried constructing the connection string and including the cluster option to connect to 9.5:
postgres://user#localhost/db_name?options=--cluster%3D9.5%2Fmain
And sure enough I get: psql: FATAL: unrecognized configuration parameter "cluster"
Okay, no surprise there. However, I can't seem to connect to 9.5 no matter what I do. I even have the environment variable PGCLUSTER set to the appropriate version string, however I think this is only read from CLI (ie: not when the server is connected to via unix socket or host).
What is the easiest way to connect to the 9.5 server instead of 9.4 from a script connecting to the server via localhost?
The best approach is to find the port that the 9.5 server is running on. In my case, 9.4 was running on port 5432 whereas 9.5 was running on 5433. This is a simple change to the connection string:
postgres://user:pass#localhost:5433/db_name

Accessing MongoDB from Windows & Mac Client Machines

I have MongoDB 3.2 installed on my Linux Red Hat server.
I am starting to access it and looking at the mongo Shell instructions.
For a Windows machine, the instructions want me to get to the command prompt and change dirs to the installation directory. The problem is, MongoDB is installed on my web server and not my local windows machine.
Question: does Mongo Shell apply to me then? How do I start using, connecting and accessing Mongo from my Windows and Mac machines?
[Note: I am a traditional MySQL / phpMyAdmin developer looking to advance to MongoDB]
Amendments:
(1) With the help of #AlexBlex I am progressing to trying to connect to my MongoDB on my server from Robomongo on my windows client. I get the following error when trying to setup my connection. I tried the address with just my server ip and with http://{my server ip}. Neither worked. See screen shot of error
(2) This is what I have in my current mongod.conf file:
#port=27017
bind_ip=127.0.0.1
(3) here is what my connection settings look like. Oddly, #AlexBlex's solution below shows an SSH tab on his Mac version. The Windows and Mac versions I just installed lacks that tab.
If you install MongoDB on your local machine, you can use the Mongo shell like below to connect to your remote server
mongo yourserver:27017/database
You will have to configure your Mongo server to allow remote connections. In order to achieve this you need to have the following line in your /etc/mongodb.conf file. You need to replace 10.0.0.2 with the ip address of your client machine.
bind_ip = 127.0.0.1,10.0.0.2
You need either ssh to the server where mongodb is installed, or install mongodb on local machine.
For robomongo to connect to remote host you need to ssh to the server, and check it listens on external interface:
lsof -i | grep 27017
In case it is bound to localhost only, you need to edit a line with bind_ip in /etc/mongodb.conf and restart the service.
I would recommend to keep it listening on the local interface only for security reasons, and use ssh tunnelling to connect:
I found the answer. #ShahNewasKhan is brilliant. See How to connect Robomongo to MongoDB
All you need to do is SSH to server and edit mongod.conf file:
uncomment #port=27017 to port=27017
comment bind_ip=127.0.0.1 to #bind_ip=127.0.0.1
restart mongodb via service mongod restart
Then create a mongo connection via your server ip in the address field and 27017 in the port field
Hope this helps mongo newbies and start-ups like me :) Good luck.
Now I just need to figure out how to make this secure. My concern is that anyone who knows my server ip can hack into my MongoDB

Resources