In this post I will walk you through migration of a database running old 2.6 version of MongoDB, to a more recent 3.4 version. We will not only upgrade the MongoDB version, but also change the storage engine from MMAPv1 to WiredTiger. The database is hosted within a Docker container.

The database is a standalone instance, so the migration process will be quite straightforward. However this also means the database will be offline during the process. In the scenario described here it was not a big problem.

So let's start with the initial setup of the database. For the sake of simplicity, let's assume the db container was created with following command:

docker run -d --name=mymongo -p 27017:27017 -v /mongodb:/data/db mongo:2.6

Step 1

The data volume of the mongo container is mapped to the host machine at /mongodb.

In order to upgrade mongo and change the storage engine, we'll have to

  1. Do a full export of the data
  2. Shut down the old instance of database, remove its data directory
  3. Start new instance, running new version of MongoDB. It will use WiredTiger by default
  4. Import all the data

Please note the export and import is only required when you want to change the storage engine. If you would rather keep using MMAPv1, you can just upgrade mongo and point it to the same data directory as the old version.

Also: make sure you know what you are doing. The operations mentioned in this post might be destructive for your database. Don't forget about making a backup of your database before attempting upgrade.

Exporting the data

In order to export the data, we'll use a temporary container that will link to the database container. Within this temp container we'll run mongodump tool, that will export all the data to a specified location - in our case a volume mapped to host.

docker run -i --rm --link mymongo -v /tmp/mongoexport:/tmp mongo:2.6 bash -c 'mongodump -v --host mymongo:27017 --out=/tmp'

Step 2

We're using container linking here due to old version of the Docker engine. In more recent versions you'd attach the temp container to a Docker network.

Upgrading mongo

With Docker, that task is quite simple. Just remove the old container and start a new one, based on the image with desired mongo version.

docker stop mymongo
docker rm mymongo

# rename the data directory - keep it as a backup if something fails
sudo mv /mongodb /mongodb-2-6-backup

Step 3

Now let's use 3.4 mongo version and map the data volume to /mongodb. Since this directory does not exits, it will created and mongo will default to WiredTiger storage engine. This is the behavior you can expect since version 3.2.

docker run -d --name=mymongo -p 27017:27017 -v /mongodb:/data/db mongo:3.4

Importing the data

Last but not least, we need to restore the data using the mongorestore tool.

docker run -i --rm --link mymongo -v /tmp/mongoexport:/tmp mongo:3.4 bash -c 'mongorestore -v --host my:27017 /tmp'

Step 4

After the temporary container completes the import, we'll be left with a working Mongo DB 3.4 instance.

Step 5

Bonus

We could also run the old version of the database side by side with the new one. That could be useful in order to verify if export - import worked as expected. Just make sure to use different port number.

docker run -d --name=myoldmongo -p 27018:27017 -v /mongodb-2-6-backup:/data/db mongo:2.6

Summary

In this short post we upgraded dockerized Mongo DB from version 2.6 to 3.4, also changing the storage engine.

About the author:

Marcin Budny

Team lead of R&D at BT Skyrise. Works with Intelligent Transport Systems (ITS), where he faces problems in terms of equipment and software. He is passionate about the architecture of applications, cloudcomputing and IT systems quality. He constantly searches for novelties on software development. Marcin specializes in .NET technology, however he looks for inspiration in other languages and platforms.

Next Post Previous Post

blog comments powered by Disqus