Mongoexport "ERROR PARSING URI" error - MONGODB - 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

Related

Mongodump doesn't work as expected

I am trying to host my database online and based on what I have found online, I need to use mongodump to export my database first.
The way to use mongodump if am not wrong is:
mongodump -d <db-name> -o <directory>
when I use the above command, I get the following error in the terminal:
Failed: error connecting to db server: no reachable servers
I tried to add --host=127.0.0.1 after mongodump as follows:
mongodump --host=127.0.0.1 -d <db-name> -o <directory>
But I still get the same result. What am I doing wrong here?
Update:
I managed to overcome the error by starting the MongoDB service with the following command:
brew services start mongodb
Now when I run mongodump, it seems to be working but I can't find it when I navigate to the directory where it supposed to be located!
NOTE: I am using Meteor technology, and I am accessing my database with meteor mongo command
If you are doing mongodump from a remote server it can happen that versions are incompatible. This results in no documents being dump without any warnings. (At least from my tests, mongodump 2.6.10 won't be able to dump from mongod 3.2.13)
Also, make sure bash special characters are not breaking up your query.
Example:
mongodump --db DB_name --collection colname --query "{$or: [something1, something]}"
The previous query won't work as you need to escape the $ with \.
mongodump --db DB_name --collection colname --query "{\$or: [something1, something]}"
You could try:
Run mongodump --db <database> --port 3001 from the directory you want the output files to be created (it will create a dump dir with the files)
The port is 3001, as it seems meteor doesn't use Mongodb default port
Also, if nothing appears, try running with the -v flag for verbose mode, this will help you to find out why your files are not being created. Also, be sure yout database name is correct.

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.

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

Copying MongoDB Database into Local Machine

I have a MongoDB database that resides on a remote server machine whose IP address is 192.168.1.20 on a local network. For development and testing purposes, and since I am not allowed to modify or delete the database on the server for security purposes, I want to copy the database on my local machine for my personal use.
Can anyone please tell me, how do I achieve this?
I do this by creating a dump of the remote db to my local machine, which I then restore:
Make sure you have a mongo instance up and running (eg. run mongod.exe from your bin folder in a terminal window. On my windows computer that's C:\mongodb\bin)
Make a dump from remote db: Open a new terminal window, move to the bin folder again, run:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass
(Change the parameters to suit your own situation.)
Restore the dumped database: Once the dump has been made, run the following command so that you have a local db:
mongorestore -d theNameYouWantForYourLocalDB dump\nameOfRemoteDB
(replace nameOfRemoteDB with the name of the remote db, the same as in previous command, and replace theNameYouWantForYourLocalDB with the name that you want your new local db to have)
There is copy database command which I guess should be good fit for your need.
db.copyDatabase("DATABASENAME", "DATABASENAME", "localhost:27018");
Alternatively, you can just stop MongoDb, copy the database files to another server and run an instance of MongoDb there.
EDIT 2020-04-25
Quote from MongoDB documentation
MongoDB 4.0 deprecates the copydb and the clone commands and their mongo shell helpers db.copyDatabase() and db.cloneDatabase().
As alternatives, users can use mongodump and mongorestore (with the mongorestore options --nsFrom and --nsTo) or write a script using the drivers.
Reference here
This should be a comment to the answer of #malla, but I don't have enough reputation to comment so I'm posting it here for other's reference.
In step 2, When you are trying to dump file from a remote server, remember to add out option so that you can restore locally later: (in my first try, I didn't add it and it failed, saying dump\db_name was not found).I'm not sure whether my way efficient or not. But it worked for me.
Step 2:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass --out <path_you_want_to_dump>
Step 3:
mongorestore -d theNameYouWantForYourLocalDB \<path_you_want_to_dump> + nameOfRemoteDB
The mongoexport command:
http://docs.mongodb.org/manual/core/import-export/
Or, mongodump command:
http://docs.mongodb.org/manual/reference/program/mongodump/
mongodb has commandline tools for importing and exporting. Take a look at mongodump --collection collection --db test and mongorestore --collection people --db accounts dump/accounts/
http://docs.mongodb.org/v2.2/reference/mongodump/
http://docs.mongodb.org/v2.2/reference/mongorestore/
this even works over the network
You can use the mongoexport command to copy the database to your local machine.