Connect to AWS over SSH portforward - mongodb

I'd like to connect to my AWS instance and connect to my MongoDB database over localhost. The SSH tunnel seems to work, but when I try to connect with mongo I get a connection failed error.
SSH tunnel command:
ssh -i <path to key> -N -L 27017:++++++++++.us-west-2.compute.amazonaws.com:27017 ++++++#++++++++++.us-west-2.compute.amazonaws.com
Mongo client:
mongo -u +++++++ -p ++++++++++ mongodb://localhost:27017/+++++
MongoDB shell version v3.4.7
connecting to: mongodb://localhost:27017/++++++
2018-01-22T15:32:07.125+0100 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host 'localhost:27017' :
connect#src/mongo/shell/mongo.js:237:13
#(connect):1:6
exception: connect failed
On the tunnel side I then get:
channel 2: open failed: connect failed: Connection refused
I have tried the mongo command directly on the server then there it works fine.
On my Robo T3 client there is an option to connect over SSH and there the connection works just fine.
Any suggestions are greatly appreciated.
EDIT
When I start the mongo client, no new lines are added to mongdb log file. This indicates that the tunnel is not doing it's job...

According to this documentation, this error happens when you try to connect to a MongoDB server without ssl.
The solution then would be to connect using SSL, which can be done by adding --ssl to your connection query (so that it looks like this: mongo --ssl -u +++++++ -p ++++++++++ mongodb://localhost:27017/+++++).
Let me know if this works for you!

Related

Atlas MongoDB SSH Tunnel hostname mismatch

I am trying to setup an SSH tunnel to a mongoDB cluster hosted In Atlas. I setup the tunnel with the below command
ssh -i <key_file> -N -L <localport>:<atlas_node_hostname>:<atlas_port> <remote_user>#<remote_ip>
And added the atlas_node_hostname in
/etc/hosts
From the machine where I created the SSH tunnel, I can connect to atlas db with the atlas_node_hostname mentioned in the string.
mongo --ssl "mongodb://<username>:<password>#<atlas_node_hostname>:<ssh_tunnel_local_port>/<db_name>?authSource=admin"
But I want to connect using 127.0.0.1 instead of the atlas_node_hostname, this is so that I can give devs access only to the machine where the tunnel is running and they can connect to the DB.
mongo --ssl "mongodb://<username>:<password>#<127.0.0.1:<ssh_tunnel_local_port>/<db_name>?authSource=admin"
But with the above attempt I get the below error
Error: couldn't connect to server 127.0.0.1:27779, connection attempt failed: SSLHandshakeFailed: The server certificate does not match the host name. Hostname: 127.0.0.1 does not match SAN(s): *.345ed5412fd76fb84cd13794.utkqs.mongodb.net, *.utkqs.mesh.mongodb.net, *.utkqs.mongodb.net, CN: *.utkqs.mongodb.net :
Is there any way I can get it working with the 127.0.0.1 instead of giving the atlas_node_hostname? Or any tool that can proxy Atlas MongoDB?

Error when connecting to Mongo DB Hosted in a container

Ive installed Docker locally (mac) and tried to run a docker container using the following command.
docker run -it -p 27017:27017 mongoImageId
But when I tried to login in to the container via the mongo terminal client, Im getting the following error.
mongo --port 27017 -u "userName" -p "password" --authenticationDatabase "sampleDBName"
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017/
2018-07-23T13:41:53.423+0530 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017' :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
But if I try to build the same image in an aws ec2 instance, it connects without a hazel. Does any one else also encountered the same issue and was able to find the root cause ?
Ref.
[1] - https://github.com/aashreys/docker-mongo-auth

How do I access a remote aws lightsail mongodb over ssh tunnel

I have a Lightsail AWS instance up and running with a MEAN stack. I have an existing MEAN stack running on a different network. At the moment the node server.js connects to localhost for the mongo bit (on same machine) and all I want to do is replace the localhost with a connection to my mongo running on my AWS remote server.
I understand, that for security reasons, it is best to ssh tunnel this connection, which I think I am familiar with.
What I have done so far is this:
In a console on the machine hosting the node server (remote to the db) I have run:
ssh -L 8181:127.0.0.1:80 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
I can then browse to the RockMongo UI from the local machine using localhost:8181/rockmongo ...yay.
If I then run the following:
ssh -L 8181:127.0.0.1:27017 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
(27017 being the mongo port)
Then try and access the db from my remote machine using:
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
I get the following error:
~]
2017-12-28T22:11:09.791+0000 Error: couldn't connect to server 31.16.56.125:8181 (31.16.56.125), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
Am I doing this wrong? i.e. is the tunnel only for http connections and not mongo command line use? Do I need to test the connection some other way?
I've Googled all over the place for this and not had much luck (a lot of the AWS docs suggest punching a hole in the firewall - which one can no longer do!)
OK I've (partially) solved this, there were a few things wrong.
1) The mongo client was 2.6 and mongo running on AWS was 3.4. Upgrading this solved some issues - in that I was getting a more meaningful error message.
One thing I did have trouble with is that apt-get seemed to perform an update, yet the version reported when issuing the mongo command was still 2.6.
To solve this I had to run sudo apt-get purge mongodb-org* (note the asterisk). Then perform the update.... If you need to do this then follow these instructions:
https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
2) This command
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
won't work as I've omitted 'admin' from it and not specified localhost!
but
mongo admin --username XXXXXX --password XXXXXX localhost:8181/testdata
doesn't work either and gives the following output.
2018-01-03T22:00:42.380+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2018-01-03T22:00:42.380+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
The only command I could get to work is:
mongo admin --username XXXXXX --password XXXXXX --port 8181
The default host is localhost, so in this case it uses the tunnel, this will also just connect to the test db, you can then admin from there.
What I haven't got to the bottom of is the specification of the host:port/db as an argument as per the output from running mongo --help
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999

mongo - couldn't connect to 127.0.0.1 - ip changed

I installed mongoDB and changed the IP to 10.3.Y.XX.
When i want to start mongo from terminal (> mongo), this error occurs:
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1),
connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Any idea how to solve this?
If you want to connect to something other than the default host (which is "localhost"), you need to tell your Mongo client application about it.
For example, on the command line you can do
mongo --host 10.3.x.y db_name

Error dumping data from a local db for a meteor server

I want to upload data from a local db to a meteor server. After running mongodump -h 127.0.0.1:3002 -d meteor -o meteor the terminal returns the following error:
2015-05-28T20:47:14.125-0700 warning: Failed to connect to 127.0.0.1:3002, reason: errno:61 Connection refused couldn't connect to [127.0.0.1:3002] couldn't connect to server 127.0.0.1:3002 (127.0.0.1), connection attempt failed
Is there any way to fix the problem? No similar post provides a workaround.
The port is no longer port 3002 (port of your meteor app 3000 + 2). Now it is the port + 1. So if you run Meteor on port 3000, you need to connect to port 3001:
mongodump -h 127.0.0.1:3001 -d meteor -o meteor