What is a Ram Disk you ask? Simply put, you carve out a piece of your system’s RAM and use it as a normal file system. But you probably have some more questions…
Why would I want to do this?
Simply put, RAM is very fast. Faster than most (any?) SSD drive. So if you have an application that would benefit from being able to access data very quickly, a RAM disk makes a lot of sense.
That’s amazing… why don’t I just load everything into a RAM disk all the time?
Two primary reasons… First… RAM is expensive and therefore quite scarce compared with conventional hard drive space. So your system probably has a fairly limited amount to work with. Second, and perhaps more critical, RAM is erased whenever power is lost. So if you do a system reboot, or lose power, everything stored on your RAM disk, even the RAM disk itself, is completely lost.
Oi’ that’s bad… when exactly would I want to use a RAM disk then?
Some applications, like MySQL for example, write “temporary” files to disk. These are usually pretty small files that are built on the fly for normal operations. Think of them like a scratch-pad you would use when doing a long equation in Algebra class. These files are built on the fly to store some temporary data (perhaps a specific view of the database as requested by a website page). Often they are used for “cache” as well, i.e. rather than taking the CPU through the task of building as special table from existing tables again and again, it caches the special table so it can serve it the next time it is requested.
Ultimately, if your system gets rebooted, you really don’t care if you lose this kind of data. Furthermore, having this kind of data written to and pulled from a RAM disk, could really improve application performance depending on your use case.
In this article, I am going to talk about building a RAM disk on the fly, and telling MySQL to use it. Furthermore, we are going to create a few scripts to make sure our RAM disk is created at every boot so that MySQL can consistently take advantage of it.
Being both a Windows System Admin and a Linux guy, I am often astounded at how simple some things are in one operating system vs. the other. A RAM Disk is one such “thing” that is very easy to do in Linux but requires jumping through hoops in Windows.
Let’s go ahead and dive in to building our RAM Disk in Linux… STOP!
Doing research in the middle of writing an article can sometimes be rather helpful… I came across this tidbit:
If you are running Ubuntu, then you already have perfectly usable, mounted, ramdisk location that can store files up to half the size of your total installed ram. This should be plenty for MySQL so I am not going to go into more detail on how to manually create a RAMdisk in linux as this isn’t necessary on any modern version of Ubuntu (which is my OS of choice…)
Run the following command and you can see how much storage space you have mounted at /run/shm
You should see a folder mounted at /run/shm that is approximately half the size of your installed ram. We are going to put a command in our /etc/rc.local file to create a sub-directory here every time ubuntu boots. We are going to tell MySQL to use this folder as its temporary file directory. Once that is done, we should get a performance boost for any applications relying on our MySQL install… in my case, Drupal.
Add the following two lines to this file on the lines preceding the “exit 0” so it looks like this:
chmod 777 /run/shm/mysqltemp
Now reboot your system. When your system comes back up, navigate to /run/shm and make sure there is a directory there called mysqltemp.
Finally, backup and then modify our MySQL my.cnf file.
sudo vim /etc/mysql/my.cnf
Add the following line in the [mysqld] section of the file (this parameter may already exist, if it does, just update the file path):
Now… if you try to restart MySQL at this point it will fail. I was a bit puzzled by this at first. I knew it was because i had changed directories. Luckily MySQL logs quite well so I checked the /var/log/mysql/error.log file. It showed that MySQL couldn’t write to the new directory. Odd…
I checked write permissions on the directory and noted that group and everyone didn’t have write permissions. I 777’d the directory and tried to start it again but it failed with same error.
Please note that I already updated the code/instructions above so that your directory already has full write permissions for group and everyone…
A bit more digging and I came across the reason why… A security feature in ubuntu called “App Armor” was to blame. Some apps have a file that App Armor loads. This file is like a second set of write permissions for each application. If you don’t spell out the directories that MySQL can write to in this file, then it won’t work. So… lets fix this problem…
Add the following three lines to the bottom of the file, before the closing “}”
Save that file and close it, then tell app armor to reload all profiles…
Finally, restart MySQL
Now enjoy increased MySQL performance! All temporary files that MySQL has to create and tear down in the course of a day will now be written to and read from RAM which should significantly boost performance. I rebooted my server once more just to be safe and all came up and ran as expected. I am running an Ubuntu 13.04 box.