Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating MongoDB instances with no down-time

We are using MongoDB in production environment and now, due to some issues of current servers, I'm going to change the server and start a new MongoDB instance.

We have a replica set and a single mongod instance (two different MongoDB networks for different purposes). Now, first I should migrate the single mongod instance and then the whole replica set to the new server.

What I want to know is, how can I migrate both instances with no down-time? I don't want to shutdown the server or stop write operations.

Thanks in advance.

like image 856
Afshin Mehrabani Avatar asked May 15 '14 20:05

Afshin Mehrabani


People also ask

How do I migrate with zero downtime?

Zero Downtime Migration requires that you use Object Storage Service (OSS), Zero Data Loss Recovery Appliance (ZDLRA), or a Network File System (NFS) as the intermediate backup medium to migrate on-premises databases to Oracle Exadata Cloud at Customer environments.

How do I move MongoDB from one server to another?

You can use Robocopy to migrate your data from one server to another and FYI Robocopy is a CLI-based tool.

What is Mongomirror?

mongomirror is a tool for manually migrating data from an existing MongoDB replica set to a MongoDB Atlas replica set. mongomirror does not require you to shut down your existing replica set or applications, and does not import user or role data or copy the config database.

Do we need migration in MongoDB?

This is why you need a migration tool for MongoDB Also, when we need to perform several updates that have a special order, it is pretty common to mix those up and broke the data. This way we will have an incremental, reversible, and version-controlled way to handle database schema migrations.


2 Answers

So first of all you should never run mongodb as a single instance for production. At a minimum you should have 1 primary, 1 secondary and 1 arbiter.

Second, even with a replica set you will always have a bit of write downtime when you switch primaries, as writes are not possible during the election process. From the docs:

IMPORTANT Elections are essential for independent operation of a replica set; however, elections take time to complete. While an election is in process, the replica set has no primary and cannot accept writes. MongoDB avoids elections unless necessary.

Elections are going to occur when for example you bring down the primary to move it to a new server or virtual instance, or upgrade the database version (like going from 2.4 to 2.6).

You can keep downtime to a minimum with an existing replica set by setting the appropriate options to allow queries to run against secondaries. Again from the docs:

Maintaining availability during a failover. Use primaryPreferred if you want an application to read from the primary under normal circumstances, but to allow stale reads from secondaries in an emergency. This provides a “read-only mode” for your application during a failover.

This takes care of reads at least. Writes are best dealt with by having your application retry failed writes, or queue them up.

Regarding your standalone the documented procedures for converting to a replica set are well tested and can be completed very quickly with minimal downtime:

http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/

like image 88
John Petrone Avatar answered Sep 21 '22 23:09

John Petrone


You cannot have no downtime (a new mongod will run on new IP so you need to at least connect to it). But you can minimize downtime by making geographically distributed replica set.

Please Read http://docs.mongodb.org/manual/tutorial/deploy-geographically-distributed-replica-set/

Use the given process but please note:

  1. Do not set priority 0 of instances at New Location so that they become primary when old ones at Old Location step down.
  2. You still need to restart mongod in replica set mode at Old Location.
  3. You need 3 instances including an arbiter at New Location, if you want it to be replica set.
  4. When complete data is in sync with instances at New Location, step down instances at Old Location (one by one). Now everything will go to New Location but the problem is that it is directed through a distant mongod.
  5. So stop mongod at Old Location and start a new one at new Location. Connect your applications to New Location Mongod.

Note: I have not done the same so far. I had planned it once but then I got the problem and it was not of hosting provider. Practically you may get some issues.

like image 27
Nitin G Avatar answered Sep 19 '22 23:09

Nitin G