Settings Up MongoDB Replica Sets On EC2 with Ubuntu Natty 11.04

The documentation @ 10 gen shows some pretty basic examples [1] 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:

ami-ead30b83

3. Assign an elastic IP address to each of the two running instances, in this example lets just say I have the following

app-server (1.2.3.4)

db-server (5.6.7.8)

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

https://gist.github.com/2856241

Execute the following command sequence:

https://gist.github.com/2856393

If you do not want to do a 777 you could also do

https://gist.github.com/2856394

And install mongodb from 10gen:

https://gist.github.com/2856397

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:

1.2.3.4 db01 db01.example.com

5.6.7.8 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 (1.2.3.4) [db01]

https://gist.github.com/2856398

On the db -server (5.6.7.8) [db02]

https://gist.github.com/2856399

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.[2] 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:

https://gist.github.com/2856400

9. You should see the following response from the initiate command:

https://gist.github.com/2856403

10. Wait a minute, and your replica set is ready to go. You will notice the mongo prompts change to

https://gist.github.com/2856406

So a final test you can perform is log into the primary and save a doc:

https://gist.github.com/2856408

Now go over to the secondary:

https://gist.github.com/2856409

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!

[1] http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial

[2] http://www.mongodb.org/display/DOCS/Replication+Oplog+Length

You should follow me @josephmisiti