Why doesn't mongoexport generate the output file - mongodb

I have a Mongo database on my server and I can connect to it using docker.
I run the following command:
docker exec -it mongodb mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTest/output.json
It seems that the command works without any problem and I get the message:
connected to: mongodb://localhost:27017/
exported 16 records
However, when I use FileZilla and look at the directory /var/www/myTest, there is no file there. The folder is empty (I had created the folder myself earlier. If I use a new folder name, the folder is also not generated).
What have I done wrong? Is the file somewhere else?
Thanks in advance,

That directory and the file is inside the mongoexport container.
UPDATE:
You could also bind mount a directory from your host to the containers:
docker run --rm -v /var/www/myTestOnHost:/var/www/myTestOnContainer -it mongodb mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTestOnContainer/output.json
docs: https://docs.docker.com/storage/bind-mounts/
--rm flag for docker run will remove the container when it finished running, that is executing the mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTestOnContainer/output.json command

Related

Mongorestore in docker failed: Failed: gzip: invalid header

I created a mongo dump with commands (as suggested in this answer)
docker exec -it mongodb bash
mongodump --host $cluster --ssl --username $username --authenticationDatabase admin --db $dbname --gzip --archive > dumpname.gz
Now when I'm trying to restore the dump with
docker exec mongodb bash -c 'mongorestore --gzip --archive=dumpname.gz'
I get
Failed: gzip: invalid header
It seems like there is some bug with using redirection (>). So when I changed the first command to not use it, mongorestore started to work:
mongodump --host $cluster --ssl --username $username --authenticationDatabase admin --db $dbname --gzip --archive=dumpname.gz
Some similar problems could be found here

MongoDB with Docker container, not able to restore database with different name using mongorestore

This is the setup i have
Created mongodb instance with docker
sudo docker run -p 27017:27017 -e MONGODB_DATABASE=DEV -e MONGODB_USER=dev -e MONGODB_PASSWORD=dev123 -e MONGODB _ADMIN_PASSWORD=dev123 -e MONGODB_ROLE=readWriteAnyDatabase --name mymongo -v testdb:/var/lib/mongodb/data -d mongo
Entered container using
sudo docker exec -it container-id /bin/bash
Executed command
mongodump -d DEV -u dev -p dev123 ( works perfectly )
Now the ISSUE happens while restoring to different database
mongorestore --db test ./dump/DEV -- throws below error
Failed: test.duke: error reading database: not authorized on test to execute command { listCollections: 1, cursor: { batchSize: 0 } }
Stuck for 3 days now any help would be appreciated ( beginner to both docker and mongodb)
If your other mongo database has authentication then you should use :
mongorestore -u <username> -p <password> --authenticationDatabase=<database name> --db=test ./dump/DEV
Other advice would be to create dumps like :
mongodump --port 55555 -d testdb --gzip --archive=testdb.tar
and then restore like:
mongorestore --port 55555 --gzip --archive=testdb.tar

Restore data to postgresql database using commandprompt

I have a .backup file in D:\test; I need to restore this backup using commandline. I used below command. but it is not working;
psql.exe -U username -d dbname -f "d:\backup\myfile.backup"
also I used below too
psql -h hostname -U username -d databasename -f "D:\backup\myfil.backup"
I guess, you are using Postgresql under Windows.
Then you have to switch to the directory where your pg_restore.exe lies.
e.g. create a batch file:
set DBname=myDatabaseName
set filename=myfile.backup
cd PostgreSQL\9.5\bin\
pg_restore.exe --host localhost --port 5432 --username "postgres" --dbname "%DBname%" --verbose "F:\Backup_DB\%filename%"

Mongodump not working on AWS EC2 instance

I tried Mongodump on AWS EC2 instance. There is no error, but the files are not dumped.
[ec2-user ~]$ sudo mongodump --host localhost:27017 --db test--out /var/backups/
connected to: localhost:27017
2017-01-19T01:56:05.608+0000 DATABASE: test to /var/backups/test
How to take a dump inside AWS EC2? The database is in data/db folder.
In my mongodb folder first I made a folder for backups:
sudo mkdir backups
Then used 777 just in case, later I changed it to 766
sudo chmod 777 -R backups
sudo mongodump -h ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com --port 27017 --db your_db_name_here -u your_username_here -p your_password_here --out backups/
Of course change your host also
and if you want backup zipped then add this
--oplog --gzip
Hope that helps
First you should set required permission for out directory
sudo chmod 777 -R /var/backups
then
sudo mongodump --port 27017 --db test --out /var/backups/
Take Mongodb backup (Aws DocumentDB)
mongodump --host="Documentdb endpoint" --port=27017 -u "username" -p "" -d "dbname" --authenticationDatabase "admin" --gzip --archive > /path/dbname.gz
enter password
Note
--gzip: zip used in linux.
path: where you want to download the db zip file.

Import collection to non-local address with Mongo restore

I'm trying to copy over a collection from an instance of a mongoDB on my local machine to a collection hosted by mongoLabs.
I'm able to dump the collection into a dump directory, but when I try to import with the command below I get a: No such file or directory: "/dump/my_db/my_coll.bson" error. This is the command I use:
mongorestore -h ds047057.mongolab.com:47057 -d main_db -c main_coll -u xxxx -p xxxx /dump/my_db/my_coll.bson
I still get the same error if I use the full pathname.
Thanks
I believe you want to point mongorestore at the directory containing your db rather than the file containing the specific collection you're targeting. So:
mongorestore -h ds047057.mongolab.com:47057 -d main_db -c main_coll -u xxxx -p xxxx /dump/my_db
Yes! Thanks jared!
The --directoryperdb can not work when authorized below.
mongorestore -u xxx_production -p -h 127.0.0.1 --directoryperdb rongyoudao_production_mongodb
While the -d works below.
mongorestore -h 127.0.0.1:27017 -d xxx_production -u xxx -p /root/backups/2014-06-19/xxx_production_mongodb/xxx_production