Settings Up MongoDB Replica Sets On EC2 with Ubuntu Natty 11.04
The documentation @ 10 gen shows some pretty basic examples  of how to set up MongoDB replica sets locally, but I needed to set up a real one on EC2.
You can do so with the following steps:
1. Create a new security group (or modify your current security group) and open up TCP on port 27017.
2. Launch two AMIs (backed by EBS) running 64-bit Ubuntu Natty 11.04 in your favorite AZ: In this example, I used the following AMI:
3. Assign an elastic IP address to each of the two running instances, in this example lets just say I have the following
Both app-server and db-server are going to be running an instance mongodb (primary and secondary in the replica set)
4) Once they are up and running, create the appropriate directories on the EBS so if your instance goes down, you still have your data. In my case, I'm going persist mongo on
Execute the following command sequence:
If you do not want to do a 777 you could also do
And install mongodb from 10gen:
5. Update your /etc/hosts files. This is handy so you do not have to keep referencing the ip addresses. Here is what I added to my /etc/hosts files on BOTH machines:
188.8.131.52 db01 db01.example.com
184.108.40.206 db02 db02.example.com
6. Reboot both machines
7. Start mongodb on both machines. You can either do this by editing the appropriate settings in /etc/mongdb.conf, but in this example, im just going to give you the command lines because it is easier:
On the app-server (220.127.116.11) [db01]
On the db -server (18.104.22.168) [db02]
Now we have two instances of mongodb running on two difference machines on port 27017. I manually set the oplogSize because the default can be ridiculous depending on the size of your /mnt. You should make sure you can connect to both instance from both computers via
mongo --host db01 --port 27017
mongo --host db02 --port 27017
If this fails on either machine, your /etc/hosts file is incorrect or your EC2 security groups are not setup correctly.
8.If you get to this point, you should be ready to initiate your replicate set. Log into your app-server and get to the mongo prompt and enter the following:
9. You should see the following response from the initiate command:
10. Wait a minute, and your replica set is ready to go. You will notice the mongo prompts change to
So a final test you can perform is log into the primary and save a doc:
Now go over to the secondary:
This command should return 1.
That is it, and if you have any problems, check the log files
/mnt/logs/mongodb/mongodb.log, because they can be pretty informative!
You should follow me @josephmisiti