Migrating MongoDB databases

images

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:

  1. 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.
  2. 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.

Simple mongodump did the trick:

mkdir /mnt/tmp_mongo
cd /mnt/tmp_mongo
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:

cd /mnt/tmp_mongo_restore/
tar -zxvf dump.tgz
mongorestore dump

And voila, all data restored. Now you just need to wait for all other members to replicate and you have successfully migrated MongoDB database.

Comments

  1. Reply

Leave a Reply

help-hint.png
Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)
 

Your email address will not be published. Required fields are marked *