mongoimport hangs when running from within firewall - mongodb

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.

Related

Mongoexport "ERROR PARSING URI" error - MONGODB

Facing below error while executing mongoexport command.Connection string below.
MongoDB shell version v4.2.0
OS - Mac OS Catalina
mongoexport --uri="mongodb+srv://m001-student:m001-****#sandbox.*****.mongodb.net/sample_supplies" --collection=sales --out=sales.json
Error:
2021-01-14T20:27:59.584+0000 error parsing command line options: error parsing uri: lookup _mongodb._tcp.sandbox.*****.mongodb.net on 192.#.#.#:#:# no such host <br/>
2021-01-14T20:27:59.585+0000 try 'mongoexport --help' for more information
I have provided ACCESS FROM ANYWHERE IP details in network access tab too. In that case, there should not be any issues while connecting. I have installed home-brew in my MacOS and installed mongodb database tools, since my terminal didn't recognise mongoexport command initially. Please let me know if I'm missing any detail in connection string.
delete "+srv" from uri...
If you want to take a backup from a remote database and restore it on your local machine.
Get your local machine IP by opening the terminal and running this command dig +short myip.opendns.com #resolver1.opendns.com
Add your machine IP address in the mongo atlas and provide access for this IP to limited hours only. In-network access tab.
Run this command mongodump --uri 'PUT_DB_HERE_URI' --out $(pwd) which takes a backup from your remote db you will find collection files with extension .bson.
To restore this data in your local database run this command mongorestore --host 127.0.0.1 --port 27017 -d database_name ./exported_folder_which_contains_.bson files/ .
As a reference, you can find the details here
I had the same issue with mongorestore and turns out it was just a mongorestore version problem, just updgraded it and everything works fine now

Programmatically connect to remote MongoDB with SSH

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

Take mongdb dump from amazon awz from local

I am trying to take a mongodb dump from amazon aws server.
Kinldy share the command
From Local it is working
sudo mongodump -d db** -o /opt/backup/
How to do it from server
sudo mongodump -d db** -i /opt/x.pem ubuntu#ip:/
There are three things you need to do in order to make sure remote mongodump is possible -
Make sure the security group allows for communications between your
computer and port 27017 (or any other port mongo is running on your
server)
Check if mongodb is configured to bind a specific IP (by default it
is binded to 127.0.0.1 which allows for local communications only)
Change your mongodump command to something like this - mongodump
-d <db**> -u <username> -p <password> --host <server_ip/dns>
Having said that, it is often better to ssh into the server and dump the data locally, then zip it and copy it to your local machine in order to minimize network load. If you have ssh access to the server this would be a much better (and more secure) approach for dumping your data.

Initialize a Mongodb on server

I want to use a script to initialise a MongoDB on a production server from my mongodb developpement instance.
I tried this method on local and it works
https://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/
#jeorfevre you told me about the import, Can you please give me a small exemple with the import, I found something you talk about this? docs.mongodb.com/manual/reference/program/mongoimport
Thanks in advance :)
once you have your database dev database ready to be installed in production.
Launch a command line
run mongodump
mongodump --host mongodb.example.net --port 27017 --out /path/backup/
transfert the exported file to the server (this depends on server envs)
run mongorestore on the server
mongorestore --port 27017 /path/backup/

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.