Programmatically connect to remote MongoDB with SSH - mongodb

I need to use terminal to connect to MongoDB. I have almost precisely same issue as this StackExchange question.
In my case I can correctly use Robo3T to connect. As well as use command
mongo --host 111.111.111.111 --port 111 --authenticationDatabase DB --username USER --password PASS locally. With same command executed remotely I receive following error:
No connection could be made because the target machine actively refused it.
I wanted to precisely recreate my Robo3T connection setup to see if SSH tunnel solves my issue

Related

How to connect to Amazon DocumentDB using a connection string?

I am attempting to connect to Amazon DocumentDB cluster via SSH port forwarding as described here. I can connect using this command ...
mongosh --host localhost:27017 --ssl --sslAllowInvalidHostnames --sslCAFile rds-combined-ca-bundle.pem --username REDACTED --password REDACTED
... but not this command ...
mongosh mongodb://REDACTED:REDACTED#localhost:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem
It fails with the error message "unable to get local issuer certificate." At least one problem is that I have not been able to find any documentation on how to translate the --sslAllowInvalidHostnames option into the connection string format. I found this, but adding &sslVerifyCertificate=false doesn't change anything so there might be something else I'm doing wrong here.
I figured it out (sort of). I never got the connection string working via mongosh, but when using it to connect in code, which was my ultimate goal, there are language-specific steps you have to take to install the CA cert. Then you can connect your app with a connection string like this.
mongodb://REDACTED:REDACTED#localhost:27017?tls=true&tlsInsecure=true

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 "auth failed" Only for Remote Connections. Local Works fine

I have a Bitnami MEAN instance running on EC2. After much finagling, I've been able to successfully connect to the DB using the local shell. I created authenticated users with all of the permissions necessary to access the data, and when I run the below code -- I am able to access the DB with no problem.
sudo mongo admin -u <USERNAME-p <PASSWORD>
That said, when I try to repeat this using a remote connection I am repeatedly given an "auth failed" error from MongoDB.
mongo <HOST>:<PORT>/<DATABASE> -u <USERNAME> -p <PASSWORD>
...
This is strange because I am using the exact same credentials as I do in running the local shell. The only difference is I'm including the host and port information. I've since also confirmed that my remote connection DOES work if I disable the auth parameter in mongodb.config.
mongo <HOST>:<PORT>/<DATABASE>
Obviously, in production I want to be able to authenticate. Do any of you have suggestions as to why there is a discrepancy between remote and local authentication?
I was facing the same issue.
The problem for me:
My local mongo shell was v2.6.10. It uses an authentication method called MONGODB-CR that has been deprecated.
My server version is v3.0.4. It uses an authentication method called SCRAM-SHA-1.
Try to check your local shell and remote server versions with:
mongo --version
mongod --version
If they are different, upgrade your local shell to v3. (I had to uninstall and install it again.)
I had previously be installing MongoDB version 3.2.12 and was able to connect to a remote instance using:
mongo -u ‘<USERNAME>’ -p ‘<PASSWORD>’ --host <REPLICA_SET>/<HOST>:<PORT> admin
I am creating a new cluster with version 3.4.2 and was not able to connect with the same command. After trying many different options I was finally able to figure out that I needed to add --authenticationDatabase before the admin database.
mongo -u ‘<USERNAME>’ -p ‘<PASSWORD>’ --host <REPLICA_SET>/<HOST>:<PORT> --authenticationDatabase admin
If you're using more recent versions of MongoDB (server version 4.2.6 / shell version v3.6.9 in my case) you don't have to force them to match like in #Alexandre's example. For instance, if you're getting this error:
[thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
You can connect with this syntax:
mongo --host mongodb://username:password#IP:PORT/ --authenticationDatabase admin
Install the same version both on the server and on the client solved the problem for me.
As #Alexandre explained above, it is probably a problem of password encryption.
MongoDB version 3.2.7
I tried successfully with the two methods:
mongo --host "your_host" --port "your_port" --username "your_user" --password "your_pass" --authenticationDatabase "your_admin_db"
mongo "your_host:your_port/your_db" --username "your_user" --password "your_pass" --authenticationDatabase "your_admin_db"
Besides, make sure that your server is available for remote accesses. See details about net.bindIp at https://docs.mongodb.com/v3.2/reference/configuration-options/
This is mainly due to security reasons.
When you have access to the local environment, it is easy to supposed that you are an administrator of the system or a developer because you have access to the machine itself.
If you don't have access to the local machine, you can't guarantee this, and since a database security is really important (in most cases), it makes sense not to enable remote access. You can, of course, disable this, but it is not recommended.
Hope I helped.
Just in case someone bumps into the same problem, the authenticationDatabase is only required if you created the user in ANOTHER database. If you create the user in the database you connect to, no problems.
So be careful : use then create user .
If you happen to create your user in the admin database then yes you need the authenticationDatabase flag.

mongodb installed in azure ubuntu cannot connect

I am trying to connect to a mongodb database I installed on an Ubuntu VM on Microsoft Azure. I did the following:
Created virtual machine.
sudo apt-get mongodb (I connected to the VM with ssh).
Created an Endpoint on the Azure Management Portal with both public and private ports set to 27017.
When connected via ssh, running the mongo command allows me to view and access the data stored in the mongodb, but when done remotely, the connection fails with:
Sat Oct 11 13:34:08.378 JavaScript execution failed: Error: couldn't connect to server xxxxxx.cloudapp.net:27017 at src/mongo/shell/mongo.js:L114
I think I am missing something pretty basic here. Hopefully someone out there can help me?
It looks like mongo has some problems when connecting with a regular string. Might be related to this JIRA.
Try to connect with the following syntax: mongo -u <user> -p <password> hostIP:port/db
make sure you unbind the IP for connecting remotely to it by editing:
sudo vi /etc/mongod.conf
Set bind_ip=0.0.0.0
sudo service mongod restart
to test, use: `mongo SERVER-IP:27017/DBNAME -u DBUser -p DBPass

mongoimport hangs when running from within firewall

I am trying to import data to my mongodb sevrer that is hosted on the cloud.
I run the following command from a linux server that is inside a corporate firewall:
mongoimport --host myhost:10081 --db mydb -u myusr -p mypass --collection imptest --file test.dat --drop --stopOnError
The import starts running, connects to the remote mongod successfully, creates one record of data (checked my db) and then simply hangs forever with no error message.
I am quite sure that this happens due to some firewall settings which block communications back from the mongo server - when I do the same thing from outside the firewall it works perfectly.
Can I make mongoimport work with more optimistic WriteConcern, and not wait for acks? Or better yet, how can I find out which port being blocked is causing me the trouble?
I assume there are some ports which are most certainly open, like 22 for SSH. You could try setting up an SSH tunnel from within your firewall to the cloud based server. Then you need to forward connections on the mongoDB ports through the SSH tunnel.