2014년 12월 16일 화요일

Copying database from local to remote server

I need to copy database from local pc to remote server. What's the best practice for doing it? I saw examples with mongodump and mongorestore while there is also db.copyDatabase(). Related question - what's the best way of doing it if I need to do it often?

I saw generic example for copyDatabase as follows:
db.copyDatabase(<from_db>, <to_db>, <from_hostname>, <username>, <password>);

Is there specific example that would should in detail how it's done?



For a one-time full copy in 2.6, using mongodump/mongorestore works well. If the source database is being written to as the dump is created, make sure to use --oplog and --oplogReplay in mongodump and mongorestore, respectively. If you can shut off the source mongod for a time, just copying the actual database files is very efficient. db.copyDatabase essentially reads all the data from the source and inserts it into the target (running on the target), but if the source or target database are being written to during the copy then the final source and target datasets can diverge.

For a specific example of the db.copyDatabase function:

db.copyDatabase("your_db", "my_db", "yourserver.yourdomain.com", "guest", "password")

and remember that the "<to_hostname>" is implicit in the fact that db.copyDatabase runs on the target server. In this case, the hostname of the target server would be something like "myserver.mydomain.com". The user guest/password is a user on the source machine with sufficient privileges.

Why do you need to frequently copy a database from one server to another? Would using a replica set to keep the data in sync not work?



Thank you for explanation - now I understand that replication is the better way to do it.

I read that I need at least three instances for replication. What if I have just local pc with database and remote server on VPS. I don't plan to add another server at this point. Is it possible with two machines? How would I do that with just two machines?

I am also not very clear on mechanics of replication process. In my case database on local pc will be primary and local machine is not always online. Server would host secondary. How would replication work in such a setting? Do I need to always manually set replication when I start local pc?

What would be bind_ip for my local machine?

Database on a server would be used for reads, no writes.


댓글 없음:

댓글 쓰기