Don’t Let Your Standalone MongoDB Server Stand Alone
July 22, 2013 | Updated: May 22, 2015
Lots of folks start with standalone servers when they first try MongoDB. If you are running standalone, it’s especially critical that you back it up because you don’t have the fault tolerance and redundancy of a multi-node replica set.
The MongoDB Management Service (MMS) is a convenient and effective way to backup MongoDB but it does not support standalone servers. This post will explain how to get around that problem.
MMS Backup requires that you be running a replica set - but what is not widely known is that you can run a one node replica set, thereby producing the oplog that MMS Backup requires to work, while still running only a single mongod server.
Here is how you do it.
- Shut down the standalone mongod instance. The safe way do this is by calling db.shutdownServer() from the mongo shell.
- Restart the instance. Use the --replSet option to specify the name of the new replica set.
- Connect to the mongod instance.
- Use rs.initiate() to initiate the new replica set
Here is a concrete example. If you your database is running on the standard ports and uses the standard location for the data directory (/data/db) then you might issue the following command to start the server:
# mongod --replSet rs0
You can name the replica set however you like. Using “rs0” was just an example. After you do this, initiate the replica set:
# mongo # rs.initiate()
The replica set is now operational. For detailed instructions, visit the docs.
Now that your single server is a one-node replica set, you can configure backup through MMS Backup.
1) Get Monitoring Running
Using MMS Monitoring is prerequisite of using MMS Backup because the backup service relies on the configuration information gathered by the monitoring service to figure out what to backup. Monitoring is free.
If you are not already monitoring your server using MMS Monitoring, go to mms.mongodb.com and signup for a free account. You will then need to download the monitoring agent and run it on a machine that can see your server.
The monitoring agent is written in Python so you will need to have Python installed. There are instructions that come with the monitoring agent to tell you how to start it.
2) Register for MMS Backup
Visit the Backup tab within MMS to register for the service. This includes setting up two-factor authentication for restores, entering your credit card for billing and accepting the terms of service.
3) Install the Backup Agent
The next step is installing the backup agent on your deployment. This step is easy - just download the appropriate agent for your operating system from the Settings section of mms.mongodb.com and unzip or untar the file in the preferred directory. In the backup-agent directory, issue the following command to start the agent (unix variant):
The backup agent is a compiled executable and hence does not require Python to work (unlike the monitoring agent).
4) Enable your Replica Set for Backup in the MMS UI
Now that you've got replica sets going and the backup agent installed, the final step is to go the backups page on the MMS UI and enable your one-node replica set for backup.
5) Rest Easy - Your Standalone Server is No Longer Standing Alone - It’s Backed Up!
An initial sync will begin, and soon your database server will be backed up to MongoDB’s datacenters.
For more information about monitoring or backing up your MongoDB deployment, or to get started now, visit mms.mongodb.com.