Database replication with MongoDB is easy to setup. Replication duplicates
all of the data from a master to one or more slave instances and allows for
safety and quick recovery in case of a problem with your master database.
Here is an example of how quick and easy it is to test out replication in
MongoDB. Create a couple of directories for holding your mongo databases.
Start by running an instance of the "master" database.
Start a new terminal window and continue by running an instance of a "slave"
database. This example is running on the same machine as master which is great
for testing, but wouldn't be a good setup if you were really trying to
implement replication in a production environment since you would still have a
single-point-of-failure in the single server case.
And start another terminal window to use as the client
Now kill the master instance in your terminal with Control+C. This simulates
the the master server dying. Lastly connect to the slave instance with a mongo
client by specifying the port.
As you can see, the db.person.find()
returns all of the values
that were saved in the master list as well which shows that replication is
working. One of the other interesting facts is that you can start a slave
instance even after the mongod master is already running and has data and all
of the data will be replicated over to the slave instance as well. This all
works without ever shutting down your mongod master instance. This allows you
to add replication after the fact with no downtime. For more on MongoDB check
out these books:
* MongoDB: The Definitive Guide
* The Definitive Guide to MongoDB: The NoSQL Database for Cloud and Desktop Computing
* MongoDB for Web Development (Developer's Library)