Could I use MongoDB to attach DB in this way? - mongodb

We found a way to attach a db to another mongo instance,
that is, we copy db files like test.* from orgin mongo instance to dest mongo instance.
This way is worked if we restart mongo instance correct,
but once we found that this works even we keep mongo instance running without restart it.
So question: Can I just copy db to dest mongo instance without a restart step?

Just copying the database files might not be a good idea, even though it appears to work.
A much cleaner method is to use the db.copyDatabase() shell method.
Another clean method would be to use mongodump to export the source database to a file and then use mongorestore to import it on the destination database.

Related

How to backup Backup mongo db in meteorjs as mongodump is not working

I am new in Meteorjs and i am using mongo db which comes with the meteor package.
I have made one small meteor application using mongodb and now i want to take backup of the mongo db database. I have seen many web sites and still i am not able to backup my data base. Everyone explained the same thing that in mongo db folder use mongodump and mongostore but when i use mongodump and mongostore on my terminal then it displays something like 'mongodump' is not an internal or external source command. Can u please help me in finding out the solution.
I have found that the easiest way to backup your database (or move it between meteor installations for that matter) is to simple copy the files in .meteor\local\db
Then you don't have to worry about all that mongodb dump stuff. Simple.

MongoDb - copy one database to another keeping the users also

I want to copy from one mongo db to another db on the same server. Mongo version is 2.6.3 on Win 2008 64bit.
I ran the command:
mongo localhost:27017/admin -u <> -p <> --eval "db.copyDatabase('db_master','db_copy1')"
This worked and created db_copy1 with all the users in it. I did db.getUsers() on db_copy1 and it returned all users. All was fine.
Then I went on to copy the database db_copy1 to db_copy2 using the same command above (with different database names obviously). But the resultant db_copy2 had no users in it.
Fairly new to mongo, so quite possible I have missed something.
Thanks in advance for all your help!
Vikram
One of the things I love about Mongodb is that rather than mess about with commands like that you can just copy the files.
Just go to the directory with the data files in it and copy them to the dbpath for your new database. If you don't want a certain database, don't copy the files with that database name!

How to import data into MongoDB from another MongoDB?

I've installed new MongoDB server and now I want to import data from the old one. My MongoDB stores monitoring data and it's a bit problematic to export the data from old database (it's over 10Gb), so I though it might be possible to import directly from DB, but haven't found how to do that with mongoimport.
The export/import would be the fastest option.
But if you really want to bypass it you can use the new server as a replica of the old one, and wait for full replication.
It takes longer but it's an easy way to set up a full copy without impact on the first one.
Follow this:
http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/
And then, once it's done, change configuration again.
It's easiest than it seems, but I recommend you to do a dry run with a sample database before doing it...
Note that another benefit is that the new replica will be probably smaller in size than the initial database, because MongoDb is not very good at freeing space of deleted members
mongoimport/mongoexport is per collection operating, so it's not proper for this kind of operation.
Instead to use mongodump/mongorestore.
If the old MongoDB instance can be shutdown to do this task, you can shut down it then copy all data files to the new server as its own data. And run the new instance.
Also db.cloneDatabase() can handle it to copy data directly from old instance to new one. It should be slower against copying data files directly.
You can use mongodump and pipe directly to the new database with mongorestore like:
mongodump --archive --db=test | mongorestore --archive --nsFrom='test.*' --nsTo='examples.*'
add --host --port and --username to mongorestore to connect to the remote db.
db.cloneDatabase() has been deprecated for a while.
You can use the copydb command discribed here.
Copies a database from a remote host to the current host or copies a database to another database within the current host.
copydb runs on the destination mongod instance, i.e. the host receiving the copied data.

How to perform one-time DB sync to another DB in MongoDB?

I have separate development and production MongoDB servers and I want to keep actual data in development server for sometime. What I should use for it: mongodump, mongoimport or something else?
Clarification: I want to copy data from production to development.
If it's a one time-thing
and you want fine control over parameters such as which collections to sync, you should use:
mongodump to dump bson files of your Production DB to your local machine
mongorestore to then, retrieve the dumped BSON files in your Local DB
Otherwise you should check out mongo-sync
It's a script I wrote for my self when I had to constantly copy my Local MongoDB database to and from my Production DB for a Project (I know it's stupid).
Once you put your DB details in config.yml, you can start syncing using two simple commands:
./mongo-sync push # Push DB to Remote
./mongo-sync pull # Pull DB to Local
If you use it inside some project, it's a good idea to add config.yml to .gitignore
You can use the db.copyDatabase(...) or db.cloneDatabase(...) commands:
http://www.mongodb.org/display/DOCS/Copy+Database+Commands
This is faster than mongodump / mongorestore because it skips creating the bson representation on disk.
When you want the dev database to look exactly like the production database, you can just copy the files. I am currently running a setup where I synchronize my MongoDB database between my desktop and my notebook with dropbox - even that works flawless.

Copying mongodb

I have a web service application built using NodeJS. I am using mongodb as database.
The whole thing is in another system(A) in the network. Now I need to copy all the data to the mongodb database in my system(B).
Can anyone tell me the whole procedure in detail, like even from where I should execute the commands also. I am a beginner and have very little knowledge about mongodb.
You can use the db.cloneDatabase() command for that (docs).
Start the mongo shell from the command line on the destination server by running: mongo
Identify the name of the database to copy: use name
Copy the database from the source server: db.cloneDatabase("1.2.3.4")
Use mongodump and mongorestore utility.