Tag Archives: mongodb

MongoDB on EBS with RAID10, LVM, XFS

I keep forgetting these commands since those are used once in a blue moon, so
writing this post to refer them when needed.

As mentioned in MongoDB – Amazon EC2 Docs [1], we will create a RAID10 array.
RAID10 provides both striping (speed) and mirroring (redundancy).[2]

– Create RAID10 array

4 Disks of 8 GB Each – 16 Gigs available for use

$ mdadm --verbose --create /dev/md0 --level=10 --chunk=256 --raid-devices=4 /dev/sdf{1..4}
$ blockdev --setra 512 /dev/md0 # [3]

I used to set “readahead” to “65536” [4] , but then I got following warning with MongoDB 2.4.x

Readahead for /path/to/mongo/partition is set to 2048KB, we suggest setting it to 256KB.

Refer [3] for more details.

$ mdadm --detail --scan >> /etc/mdadm/mdadm.conf

16 Gigs is not a huge space, You will have to increase this space as your DB grows. For that we need LVM [5] .

– Create a physical volume
$ pvcreate /dev/md0

– Create a volume group
$ vgcreate VOL_GRP_NAME /dev/md0

– Check available Free Physical Extents (Free PE), use that number in next command
$ vgdisplay VOL_GRP_NAME

$ lvcreate --name LOGICAL_VOL_NAME --extents +100%FREE VOL_GRP_NAME

– Create file system
$ mkfs.xfs -f /dev/VOL_GRP_NAME/LOGICAL_VOL_NAME

– Mount newly created partition
$ mount -t xfs -o noatime,noexec,nodiratime /dev/VOL_GRP_NAME/LOGICAL_VOL_NAME /MOUNT_POINT/

– Put an /etc/fstab entry so the partition will mount automatically if you reboot.
$ echo "/dev/VOL_GRP_NAME/LOGICAL_VOL_GRP /MOUNT_POINT xfs noatime,noexec,nodiratime 0 0" >> /etc/fstab

– When you want to increase the size of an array, create another RAID10 array /dev/md1

– Extend Volume Group
$ vgextend VOL_GRP_NAME /dev/md1

– Extend Logical Volume
$ lvextend -l +100%FREE /dev/VOL_GRP_NAME/LOGICAL_VOL_NAME

– Finally extend file system on it
$ xfs_growfs /MOUNT_POINT

(You don’t have to un-mount partition or reboot system since XFS supports live expansion) [6]

[1] http://www.mongodb.org/display/DOCS/Amazon+EC2
[2] https://en.wikipedia.org/wiki/RAID_10#RAID_1_.2B_0
[3] http://www.kchodorow.com/blog/2012/04/05/thursday-4-blockdev/
[4] http://www.gabrielweinberg.com/blog/2011/05/raid0-ephemeral-storage-on-aws-ec2.html
[5] http://unthought.net/Software-RAID.HOWTO/Software-RAID.HOWTO-11.html
[6] http://tharindu.info/2011/05/22/grow-online-xfs-on-lvm/



MongoDB – Replica Set

Here’s a good tutorial on Replica-Set on MongoDB website http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial

In this tutorial, in “Replication” section tutorial asks to create a document on PRIMARY server.

db.messages.save({name: "ReplSet Tutorial"});

After creating a document on PRIMARY if you hit


in MongoDB shell the result is displayed. But if you switch to SECONDARY and execute same command to check if replication is working, you get following error.

uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }

The reason is stated here : http://www.mongodb.org/display/DOCS/Querying#Querying-slaveOk

So all you need to do is hit this command


That’s it. Now “db.messages.find()” should work on SECONDARY as well.