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.
Related
I have restore-63bbdc996664dc2524cce24d.tar file, I am restoring it in the MongoDB database
tried these commands
mongodump --db test --gzip --archive=restore-63bbdc996664dc2524cce24d.tar
mongodump --db test --archive=restore-63bbdc996664dc2524cce24d.tar
mongodump --host localhost --port 27017 --db test --gzip --archive=restore-63bbdc996664dc2524cce24d.tar
mongorestore --drop C:\Users\rustam\restore-63bbdc996664dc2524cce24d.tar.gz -v
error => Failed: file C:\Users\rustam\restore-63bbdc996664dc2524cce24d.tar.gz does not have
.bson extension
I also tried to convert this file to bson by the command
bsondump --outFile=C:\Users\rustam\restore-63bbdc996664dc2524cce24d.tar.gz coll.bson
Can anyone please tell me what should I do to restore the database?
Thanks in advance for any comment or answer.
Extract the archive file using tar -xvf restore-63bbdc996664dc2524cce24d.tar
Use mongorestore --db test --drop <path/to/extracted/bson/files> to restore the dump.
Keep in mind that this command will drop any existing collections in that database before restoring, so make sure you have taken necessary backups.
After spending a lot of time on it finally I got the solution
1. Extract the tar file
2. Run the below command
mongod --port your_port --dbpath your_extracted_tar_folder
note: port is optional, it will run 27027 default port
3. Go and connect on the same port that you pass
I am trying to export my local mongodb data to an atlas cluster and i created a dump and now i am using the command,
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-qwo7v.mongodb.net:27017,cluster0-shard-00-01-qwo7v.mongodb.net:27017,cluster0-shard-00-02-qwo7v.mongodb.net:27017 --ssl --username username --password <PASSWORD> --authenticationDatabase admin
to try to restore it but is giving me the system cannot find the file specified.
But if i type mongorestore then it works but it doesn't restore to the atlas cloud server whatever.
What am i doing wrong?
Edit: The path i have used is C:\Program Files\MongoDB\Server\4.2\bin\dump\gfg and dump\gfg but it is still not working.
Usage of MongoRestore.exe can be found here.
Standalone MongoDB
When you use mongorestore, you need to provide the location of the file/dump you need to restore
mongorestore --host=mongodb1.example.net --port=27017 --username=user --authenticationDatabase=admin /opt/backup/mongodump-2011-10-24
last portion of the command is missing that defines the location of the data that needs to be restored.
ReplicaSet
To Restore MongoDB on a Replica, You have to stop the mongod and replace the files for mongodb. Please read here for restoring database on replica set.
For restore from standalone to replica,
mongorestore --host myReplSet/mongo0.example.com:27020,mongo1.example.com:27012 --db <dbname> <folder_location>
If this still does not work, check path and ensure its properly escaped (spaces in name) or quoted.
Your code with quotes and path:
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-qwo7v.mongodb.net:27017,cluster0-shard-00-01-qwo7v.mongodb.net:27017,cluster0-shard-00-02-qwo7v.mongodb.net:27017 --ssl --username username --password <PASSWORD> --authenticationDatabase admin "C:\Program Files\MongoDB\Server\4.2\bin\dump\gfg"
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.
I want to migrate my mongodb from 2.0 to 3.0. So I followed the official doc to use mongodump to backup my dbs and use mongorestore to restore the dbs to mongodb 3.0.
But when I use mongorestore, it tells me "don't know what to do with file "db/collection.bson", skipping...".
Nothing to do. How could I migrate my dbs?
Thanks.
EDIT: Here is my steps.
Use mongodump in mongodb 2.0
mongodump
tree dump
db
├── collection-1.bson
├── collection-2.bson
├── collection-3.bson
├── ...
Copy db directory to mongodb 3.0 server.
On the mongodb 3.0 server calls mongorestore db
But I get this error:
mongorestore db
2015-03-10T09:36:26.237+0800 building a list of dbs and collections to restore from db dir
2015-03-10T09:36:26.237+0800 don't know what to do with file "db/collection-1.bson", skipping...
2015-03-10T09:36:26.237+0800 don't know what to do with file "db/collection-2.bson", skipping...
2015-03-10T09:36:26.237+0800 don't know what to do with file "db/collection-3.bson", skipping...
...
2015-03-10T09:36:26.237+0800 done
It seems one must also specify -d in 3.0 like this:
mongorestore -d db db
This answer isn't directly related to your issue case, but the same error output you will receive trying to restore mongo data, dumped with --archive option, available since MongoDB 3.2 version. To resolve this you need to specify --gzip parameter in your mongorestore command. Example:
mongorestore -d destination_db --gzip /path/to/source/db
If you have lost your mongoDb just try running this command:
mongorestore -d destination-db --gzip source-db
Where:
destination-db is the name of the destination database
source-db is the name of the source database.
Following command worked for me in mongoDB 6.0 -
mongorestore --host localhost --port 27017 --db DB-NAME DB-BACKUP-PATH
Please replace DB-NAME and DB-BACKUP-PATH with your actual values. For ex.
mongorestore --host localhost --port 27017 --db demo_db /home/ubuntu/db-backup
Where "/home/ubuntu/db-backup" contains all collections in bson and json format.
Try this one.
mongorestore -db dataBaseName dataBasePath
In order to import data to your mongodb ,You need to specify --db configuration in mongorestore function with name and path, this works for me for bson and json files as well:
mongorestore --db <dbName> <dbPath>
check the data was imported properly using mongo cli
mongo
show databases
use <dbName>
show collections
With mongorestore, the path of those dump files is a required parameter; you've got that right so far, by indicating db.
It is also a good idea as Peter has said to indicate a database to restore the dump files into collections with a /d switch.
Something I didn't realize while struggling with this is that a mongod must be running to consume the restoration. If you have more than 1 mongod running, you should definitely indicate a port with the --port switch. The code that worked for me was:
mongod --dbpath config --port 27019 --logpath log.config --logappend
And in another CLI:
mongorestore --port 27019 /d config config
followed by
mongo localhost:27019/config
to verify that the collections were filled properly.
You want to run mongorestore on the dump directory, not the db/ directory.
mongorestore dump/
Please Follow these intstructions it worked for me
First go to command prompt .
run cd C:\Program Files\MongoDB\Server\3.0\bin .
now ,For suppose your data is in a folder called trading and it contains json files .
and the path for that might be c:\Users\documents\trading.
(mongorestore -d dbName dbPath).
now run mongorestore -d trading c:\Users\documents\trading.
your files will be restored.
Not directly related to your situation but someone could stumble upon this post with similar error for this reason
In my case, the data was provided to me and had been dumped using
mongodump --db ${DB_NAME} --gzip -o ${BACKUP_FILE_GZ}
To restore, you must use following format:
mongorestore --gzip --db "${DB_NAME_RESTORE}" ${BACKUP_FILE_GZ}/${DB_NAME}
mongorestore --uri "mongodb+srv://<user>:<password>#<host>" -d <dbname> <dump dir. address ./something/like/this>
This work fine for me
I used docker to start my mongo db instance & I had default username and password
auth set for database.
Below is how my restore looked like.
mongorestore --authenticationDatabase admin -u root -p example -d test-db /data/test-db
So in-short something of this sort should work.
mongorestore --authenticationDatabase admin -u {USERNAME} -p {PASSWORD} -d {DB_NAME} {/DUMP_PATH}
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.