Setup MongoDB Replica Set

Before embarking on setting the replica set make sure three machines are set as mentioned in previous MongoDB setup blog post.

Once everything is set make sure all three machines can communicate with each other. Mostly there will be a either a firewall issue that could be limiting the communication or bind_ip property in mongod.conf that might have been left un-commented. I had no success with setting bind_ip to specific ip. So had to comment it out. When commented, MongoDB listens to all the interfaces.

Also would recommend setting the /etc/hosts file to map s1, s2, s3 with specific ip addresses.

The process to check if all the machines are connected and communicating (s1, s2, s3)
Connect to s1 and then type the following command to confirm the connection.
Note: Port is not required if the set ports are default. You can also refer official MongoDB site on Replica Setup

ssh username@s1
mongo --host s2 --port 27017
mongo --host s3 --port 27017

Repeat the same test on s2 and s3. In total, 6 results should show successful connection. On Mac, rlimits size is limited and normally MongoDB complains about it but if you notice we have already set the parameter in mongodb.plist file to 1024. If its not set then mongo will display the warning as below. Also, make sure oplogSize is set high enough (Default on Mac is 183MB). Higher value ensures secondary can copy without becoming stale. This value is set mongod.conf file.

Sat Aug 24 11:25:01.871 [initandlisten] MongoDB starting : pid=332 port=27017 dbpath=/data/db-collection-location/ 64-bit host=address
Sat Aug 24 11:25:01.871 [initandlisten] 
Sat Aug 24 11:25:01.871 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Sat Aug 24 11:25:01.871 [initandlisten] db version v2.4.6

Now you are almost ready to have Mongo Replica working with two secondaries and one primary.

Do the following on the primary of your choice.
Connect to mongo on the primary you like. Note: This is still not the primary.

On shell type: mongo
Then type. Note: This takes some time. Have patience. At this point I would advice you
take a small break. [ ‘>’ is mongo prompt]

>rs.initiate()

On screen you will see something like

{
 "info2" : "no configuration explicitly specified -- making one",
 "me" : "s1:27017",
 "info" : "Config now saved locally. Should come online in about a minute.",
 "ok" : 1
 }
tarzan:STARTUPxx>

[‘>’ Mongo prompt again]. This means Mongo is ready for replica. This machine is not primary yet. In a second it will be assigned as primary. Mongo decides that.

Then type command rs.conf()

> rs.conf();
 {
     "_id" : "tarzan",
     "version" : 1,
     "members" : [
         {
             "_id" : 0,
             "host" : "s1:27017"
         }
     ]
 }

At this point you are ready to add the secondary host. If you have set hosts name in /etc/hosts file then just pass the host name or else pass ip address. Preferred way is to set the host name for convenience.

tarzan:PRIMARY> rs.add("s2")
tarzan:PRIMARY> rs.add("s3")

You will see a message for both saying

 { "ok" : 1 }

Now run rs.conf(); & run rs.status() command to see the list of host mapped and listed as secondary. Run the same command on host s2 and s3 to see almost the same result.

Posted in MongoDB | Tagged , , | Leave a comment

Steps to setup MongoDB on Mac OS X

Tested on: MongoDB version 2.4.6 and mac 10.8.X

Installing standalone MongoDB on any machine.

STEP I

  • Download mongo from official site
  • Unzip it. Unzip using mac tool or on your favourite shell
tar xzf mongodb-osx-x86_64-2.4.6.tgz
  • Rename the unzipped folder to mongodb
sudo mv ~/Downloads/mongodb /usr/local
  • Create folder to hold data. Mongo stores data to default location ‘/data/db’. Don’t forget to set the permission. Prefer creating a user that has access to MongoDB and can start and stop MongoDB. Use this user to set as username for ‘/data/db’
sudo mkdir -p /data/db 
sudo chown yourusername /data/db
  • Create folder for logging MongoDB messages. This is defined later in ‘mongod.conf’
sudo mkdir -p /var/log/mongodb
  • Create/update .bash_profile
touch ~/.bash_profile
vim ~/.bash_profile
  • Type the following the bash_profile
export MONGO_PATH=/usr/local/mongodb 
export PATH=$PATH:$MONGO_PATH/bin
  • Restart shell and type to confirm if mongo is working
$ mongo -version
MongoDB shell version: 2.4.6

STEP II

  • For auto start MongoDB
sudo touch /Library/LaunchDaemons/mongodb.plist
sudo vim /Library/LaunchDaemons/mongodb.plist

Add the following in the above file

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>mongodb</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/mongodb/bin/mongod</string>
            <string>run</string>
            <string>--config</string>
            <string>/etc/mongod.conf</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>/usr/local/mongodb</string>
        <key>StandardErrorPath</key>
        <string>/var/log/mongodb/error.log</string>
        <key>StandardOutPath</key>
        <string>/var/log/mongodb/output.log</string>
        <key>HardResourceLimits</key>
        <dict>
           <key>NumberOfFiles</key>
           <integer>1024</integer>
        </dict>
        <key>SoftResourceLimits</key>
        <dict>
            <key>NumberOfFiles</key>
            <integer>1024</integer>
        </dict>
    </dict>
 </plist>
  • Create mongod.conf file
sudo touch /etc/mongod.conf
sudo vim /etc/mongod.conf
  • Save the file with following contents in mongod.conf
# Store data at default /data/db
dbpath = /data/db
# Append logs to /var/log/mongodb/mongo.log
logpath = /var/log/mongodb/mongo.log
logappend = true
# Run as conventional database 
fork = true
# 25GB oplog size;
oplogSize=25600
# Only accept local connections. Note: This did not work, hence commented. Currently listens to all the interfaces.
#bind_ip = 127.0.0.1,X.X.X.X,X.X.X.X
# Choose a name for your replica set; make sure its consistent
replSet = tarzan
# Diagnostic Configurations
# http://docs.mongodb.org/manual/administration/configuration/
# Profiles if query is slower than 50ms; default is 100ms
slowms = 50
# Profile the query; sets the level to profile
profile = 3
# Verbose for logging; currently turned off; switch on when there are real issues
#verbose = true
#Level 3 logs all read and write operations
diaglog = 3
# Forces all request to be validated. Don`t use in production as this can cause performance issue. Use only when in untrusted environment.
#objcheck = true
# Profile cpu
cpu = true
 
  • Now reload/load the above by typing
sudo launchctl unload /Library/LaunchDaemons/mongodb.plist
sudo launchctl load /Library/LaunchDaemons/mongodb.plist
  • Test again if mongo is running
ps -ef | grep mongo
Posted in MongoDB | Tagged , | Leave a comment