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
Related
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.
Just finished setting up my mongodb and found out that there is a free service called Atlas. Started up a cluster and ran a mongodump and mongorestore as explained here https://www.mongodb.com/blog/post/atlas-on-day-one-importing-data, but can't seem to get it to work.
Here is my shell commands:
mongorestore --ssl --db=infovis --host infovis-shard-00-00-nmctc.mongodb.net:27017,infovis-shard-00-01-nmctc.mongodb.net:27017,infovis-shard-00-02-nmctc.mongodb.net:27017/test?replicaSet=Infovis-shard-0" --authenticationDatabase admin --dir=dump/infovis --username danielbook --password <Password>
What am I doing wrong?
EDIT: Solved the problem by running mongoimport on the server instead.
So, I solved this by using mongoimport instead. I had just created the database in mongo, so I could just use the same csv files and then use
mongoimport -h cluster0-shard-00-00-nmctc.mongodb.net:27017 -d infovis -c flights -u <USER> -p <PASSWORD> --file march_2016.csv --type csv --headerline
for each file I want to import to the Atlas database.
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
We recently ported some data over to MongoDB and are now looking into running daily backups, preferably from a cron job, and restore one of the backups to a secondary mongo database.
Our system is set up as follows:
server 1: the development mongo database
server 2: two mongo databases, one for staging data and one for production
server 3: is where we run all of our cron jobs/batch scripts from.
I checked the mongo docs, and logged into our cron job server and tried to run the following command: (username, host, and password changed for security, I'm not actually connecting to localhost)
mongodump --host 127.0.0.1/development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
I get the following messages:
Mon Oct 7 10:03:42 starting new replica set monitor for replica set 127.0.0.1 with seed of development:27017
Mon Oct 7 10:03:42 successfully connected to seed development:27017 for replica set 127.0.0.1
Mon Oct 7 10:03:42 warning: node: development:27017 isn't a part of set: 127.0.0.1 ismaster: { ismaster: true, maxBsonObjectSize: 16777216, ok: 1.0 }
Mon Oct 7 10:03:44 replica set monitor for replica set 127.0.0.1 started, address is 127.0.0.1/
Mon Oct 7 10:03:44 [ReplicaSetMonitorWatcher] starting couldn't connect to [127.0.0.1/development:27017] connect failed to set 127.0.0.1/development:27017
I confirmed that I can connect to the mongo database using mongo -u -p ip/development
Our ultimate goal will be to dump the data from the production database and store it in the staging database. These two databases are both located on the same box, if that makes a difference, but for testing purposes I am just trying to get a backup of development test data.
mongo client can parse MongoDB connection string URI, so instead of specifying all connection parameters separately you may pass single connection string URI.
In your case you're trying to pass connection URI as a host, but 127.0.0.1/development is not a valid host name. It means you should specify database parameter separately from the host:
mongodump --host 127.0.0.1 -d development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
You can use with mongodump with --uri
mongodump --uri "mongodb://usersname:password#127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" --out "C:\Umesh"
All your collections will store inside the out folder it will create directory name as your Database name and all the collections are bson and metadata will store as json format.
For restore
mongorestore --uri "mongodb://usersname:password#127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" -d dbname mongodbumppath
Try this it will work.
This worked for me.
Reference: https://docs.mongodb.com/manual/reference/program/mongodump
Syntax 1:
mongodump --host <hostname:port> --db <database> --username <username> --password <password> --out <path>
Syntax 2:
mongodump -h <hostname:port> -d <database> -u <username> -p <password> -o <path>
Example 1:
mongodump --host 127.0.0.1:27017 --db db_app --username root --password secret --out /backup/db/app-17-03-07
Example 2:
mongodump -h 127.0.0.1:27017 -d db_app -u root -p secret -o /backup/db/app-17-03-07
mongodump --host remotehostip:port --db dbname -u username -p password
Here is an example of exporting collection from node server to local machine:
Host : xxx.xxx.xxx.xx
Port :27017
Username:”XXXX”
Password :”YYYY”
AuthDB : “admin”
“DB”: “mydb”
D:\mongodb-backup>mongodump -h xxx.xxx.xxx.xxx –port 27017 -u “XXXX” -p “YYYY” –authenticationDatabase “admin” –db “mydb”
Use this to get dump using URI:
mongodump --uri=mongodb+srv://john:xxxxxxxxxxxxxxx#cluster0-jdtjt.mongodb.net/sales
You can also use gzip for taking backup of one collection and compressing the backup on the fly
mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz
Or with a date in the file name:
mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
This worked to me like a charm for a single collection with a remote Windows Server.
mongodump --host <remote_ip> --port <mongo_port> --db <remote_db_name> --authenticationDatabase <remote_auth_db> --username <remote_mongo_username> --password <remote_db_pwd> --out <local_DB_backup_folder> --collection <remote_collection_name>
On Mac, this is what worked for me (but be sure to use your own real credentials):
brew tap mongodb/brew
brew install mongodb-community#5.0
brew services start mongodb/brew/mongodb-community
mongodump --uri "mongodb://usersname:password#127.0.0.1:27100/dbname" --out "/Users/some_username/code/mongodb_dumps/dump/"
cd /Users/some_username/code/mongodb_dumps/
mongorestore --nsInclude "*.*"
mongodump --host hostip -d dbname --port portnumber --username username --password password --authenticationDatabase admin -o ./path/of/backupfolder
note: "./path/of/backupfolder" path is in your client
This worked for me:
Step1: Export remote/local DB.
mongodump --uri "mongodb+srv://USER:PASSWORD........." --out "/Users/Hardik/Desktop/mongo_bkp"
Step2: Import
mongorestore ./mongo_bkp/
Posting this here in case it helps somebody.
It was impossible for me to connect using mongodump. I ended up installing the VS Code Mongo extension and it generated the string for me. The command looks like this:
mongodump -o dump_destination --uri "mongodb://<USERNAME>:<PASSWORD>#<HOST>:<PORT>/<DATABASENAME>?authSource=admin&readPreference=primary&ssl=true"
How can I use mongodump to move a single collection from one database to another?
How should I use the command and its options?
I think it's just:
mongodump --db=<old_db_name> --collection=<collection_name> --out=data/
mongorestore --db=<new_db_name> --collection=<collection_name> data/<db_name>/<collection_name>.bson
Also see docs here and here.
Btw, the other way to move the collection from one database to another is to use renameCollection:
db.runCommand({renameCollection:"<old_db_name>.<collection_name>",to:"<new_db_name>.<collection_name>"})
Here's some related SO threads:
How to copy a collection from one database to another in MongoDB
How to use the dumped data by mongodump?
Taking database (document) dump (backup)
mongodump --host <hostname-of-mongoserver> --db <db-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
Taking collection dump (backup)
mongodump --host <hostname-of-mongoserver> --db <db-name> --collection <collection-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
mongodump documentation
Very basic commands for dump mongodb.
Dump all collection
mongodump
Dump specific database only
mongodump --db=DB_NAME
Dump database with username & password
mongodump -u=USERNAME -p=PASSWORD --db=DB_NAME
Dump from another host
mongodump --host HOST_NAME/HOST_IP --port HOST_PORT --out {YOUR_DIRECTOTY_PATH} --db=DB_NAME
Only able to dump from another host when they allow it.
If it's a replica set and you want to use the --uri you should use it like this cause documentation states that you can't specify some options when using --uri
mongodump --uri "mongodb://user:password#mongo-en-1.example.io:27017,mongo-en-2.example.io:27017,mongo-en-3.example.io:27017/$Databasename?replicaSet=$replicasetname&authSource=admin" --collection $collectionname
Then restore it the usual way.
Here is an example of how you can export a single collection with mongodump.exe on Windows 10:
"D:\Program Files\MongoDB\Server\4.0\bin\mongodump.exe" -h localhost --port 27017 -d meteor --collection users -o meteor_users
The exported collection is users, the database is meteor, the host localhost, the port is 27017.
The output will be stored in directory meteor_users.
Restoring should use a command like this one:
"D:\Program Files\MongoDB\Server\4.0\bin\mongorestore.exe" -d meteor -c users users.bson
None of them works for me while doing dump for MongoDB atlas. Here is the minor change in the host that work for me
Dump
mongodump --uri mongodb+srv://<USERNAME>:<PASSWORD>#host.abcd.mongodb.net/db_name --collection "user_collection" --gzip --out db_backup_folder
Restore
mongorestore --uri mongodb+srv://<USERNAME>:<PASSWORD>#dbhost.abcd.mongodb.net -d db_name --gzip db_backup_folder
atlas-database-tools-backup-restore