MongoDB (from “humongous”) is a scalable, high-performance, open source NoSQL database. Written in C++.
A while ago I had to move some servers with MongoDB replica set from our current colocation to new provider Hetzner. There are few different ways to get this done:
- Get one member of replica set removed, create new MongoDB server on new location, add it to replicaset and watch it getting replicated. Repeat procedure for all other instances of MongoDB until you have whole replicaset moved to new location.
- Create another replicaset on new location, set current one to read only mode, dump database and then restore it on the new location, making sure all instances get replicated instantly thru lan and not thru internet (which is usually slower).
First option is quite amusing and probably best solution in case you want to have fully working database during migration. In our case response of MongoDB has to be really fast, so migrating step by step and serving MongoDB responses from remote location would mean our frontend servers would get really slow and clumsy. So that was not an option.
We decided to simply use second way, that doesn’t require that much database fiddling. MongoDB by itself knows 2 ways of dumping database. One is with .json and one is with .bson. If you simply export database to .json, MongoDB simply removes some attributes that can’t be represented in .json and we didn’t want that. To properly dump database, we had to dump it in .bson format.
mongodump did the trick:
mongodump --database database_name
tar -zcvf dump.tgz dump/
We then copied this dump directory made by MongoDB to new replicaset location and restored it:
tar -zxvf dump.tgz
And voila, all data restored. Now you just need to wait for all other members to replicate and you have successfully migrated MongoDB database.