mongoimport on localhost:3000 not working properly - mongodb

I get an error when I try to create the database on localhost:3000 (on which I run my app ) using :
mongoimport -h localhost:3000 etc...
the error : "Server unreachable"
I don't get that error if I only use localhost and not localhost:3000 in my mongoimport, but it doesn't look like the database is accessible from my app.

A temporary workaround is to add the host param to your mongoimport call, letting mongo know that the host is your own machine (127.0.0.1):
mongoimport --host=127.0.0.1

Please try with below command. Replace the below values accordingly
dbname - Database Name
collectioname - Collection Name
filename - File Name (Full Path)
mongoimport --host=127.0.0.1 --port=3000 --db dbname --collection collectioname --file=filename.json

Edit: I wrote this assuming you were using meteor. But I realize you don't mention it. Anyways, here is my input.
You can see the port you need to use by typing this
meteor mongo
Then you see this message on startup
MongoDB shell version: 3.2.6
connecting to:
127.0.0.1:3001/meteor
Port is usually 3001.
Then the import shouldn't be done in the Mongo Shell. If you do it in Mongo Shell you receive an error like that
SyntaxError: missing ; before statement #(shell):1:15
Instead type this in your terminal
mongoimport -h localhost --port 3001 -d meteor -c collectioname --type csv --file /path/to/file.csv --headerline

Related

Mongo + seeder in Docker

As part of a big project, I need add to this project docker, with mongo and a kind of seeder for this mongo, I was following this post How do I seed a mongo database using docker-compose? but everytime when I try to launch I always get the same error Failed: error connecting to DB server: no reachable servers I put all the files for this situation here https://github.com/aasanchez/mongodb-seeder ONLY the referent to this bug... someone have any idea about how to connect, I think my problem is in the connection URL but I don't know how to fix it
First problem, you url in your CMD is wrong, do like
CMD mongoimport --host mongodb -p 27017 --db reach-engine --collection users --type json --file ./data.json --jsonArray
Second, when launched by docker-compose, the dependency implied by links (which is deprecated) only ensures start up order, not your MongoDB's readiness. So you need to wait a few seconds before mongoimport.
All in all
CMD sleep 10 && mongoimport --host mongodb -p 27017 --db reach-engine --collection users --type json --file ./data.json --jsonArray
Note sleep 10 is only a simple hack, consider use wait-for-it.sh for complexe dependency.

mongoexport with read preference containing tags

I'm trying to export a collection from my MongoDB but I would like the export to be from a specific secondary server using tags in read preference.
My MongoDB consists of one replica set: primary-secondary-secondary.
One secondary is tagged as "tgg":"tgg1" and the second one is tagged as "tgg":"tgg2".
This replica set is configured to be a single shard. With mongos client running on port localhost:47010.
I'm using mongoexport 3.4
The mongoexport command I'm running:
mongoexport --port 47010 --db test --collection test_collection --out ~/tmp/mongo/test.json --readPreference '{"mode":"secondary", "tags":[{"tgg":"tgg2"}]}' --verbose
I'm getting the following error:
Failed: Failed to call say, no good nodes in replset1
But, if I don't specify the tags but only that the mode is "secondary" everything is working.
Am I missing something here?
EDIT
So I found what was wrong, I had to go to their source code in github for that but still. I must say that the error messaging is not sufficient.
It seems I supplied a list of tags and not just a Json object.
So the following command worked:
mongoexport --port 47010 --db test --collection test_collection --out ~/tmp/mongo/test.json --readPreference '{mode:"secondary", tags:{"tgg":"tgg2"}}' --verbose
Try to connect to the desired secondary with --host parameter in the mongoexport command itself:
mongoexport --port 47010 --db test --collection test_collection --out ~/tmp/mongo/test.json --host <ip-or-hostname-of-secondary-tagged as tgg2>

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.

How can I mongoimport with Meteor?

I made a mongoexport of some local data that I now want to mongoimport into production. I'm trying the following command:
mongoimport -h production-db-b2.meteor.io --port 27017 --username client --password password_that_expires_fast --collection collection_name --db prod_meteor_com --file ./mongo_import.json
This appears to be quite similar to the solution posted here: how to mongoimport data to deployed meteor app?
However, it's not working with the error couldn't connect to [production-db-b2.meteor.io:27017] couldn't connect to server production-db-b2.meteor.io:27017
How do I get around this?
(Note: I also tried concatenating the port and host into -h production-db-b2.meteor.io:27017 to no avail)
This is what worked for me in the past. I'm not exactly sure of why it works and your solution doesn't, but I think it has something to do with not specifying the collection or the file in a certain way.
mongoimport -u client -h production-db-b2.meteor.io:27017 -d myapp_meteor_com -p passwordthatexpiresreallyfast /pathtofile
check your port. the local meteor mongodb uses 3001 not 27017...
i'm using the following line successfully
mongoimport --host localhost:3001 -d meteor -c TestCollection --type csv --file /home/ubuntu/meteorMongoImportTest/results1.txt --headerline

Mongodb's "mongodump" command, javascript execution error

Perhaps I have a complete misunderstanding of how mongodump is supposed to work, but I can't seem to get it to do anything besides returning a JavaScript execution failed: SyntaxError: Unexpected identifier error.
Here's what I'm doing:
Mongod is running
I want to backup a database called "mydb"
I'm inside the mongo shell
I tried the command mongodump --db mydb and get the above error
I've tried both mongodump and mongoexport, both have the same issue
What am I doing wrong here?
Try the following it will work
i.Open the terminal
ii. Enter mongodump --collection collectionname --db dbname (Don't go inside mongo shell);
iii.If default port is different(other than 27017) then go for the following command
mongodump --host mongodb1.example.net --port 37017 --username user --password pass --out /opt/backup/mongodump-2011-10-24
mongodump,mongorestore is not commands of mongodb shell. It is separate mongodb utlity. You can find it under mongodb bin folder.
Usually you will need to add all mongodb utilities to the system Path variable and after this easy backup/restore databases from any place in the command line or in the terminal.
Your command looks mongodump --db mydb good if your databases in on default port(27017).
I faced the problem in taking mongo dump and I also wanted to store the dump to S3. Finally I ended up with a bash script to take mongo dump and store it to S3. I used mongodump to take backup.
mongodump -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
Where $MONGO_HOST,$MONGO_PORT and $MONGO_DATABASE are bash variables for host, port and database-name respectively.
You can also use --username user --password pass option for mongodump command if you have username and password setup on the database.
Here is the script to take mongodb dump and store it to S3 with a cron.