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.

sudo vim /etc/rc.local

Add the following two lines to this file on the lines preceding the “exit 0” so it looks like this:

mkdir /run/shm/mysqltemp
chmod 777 /run/shm/mysqltemp
exit 0

Now reboot your system. When your system comes back up, navigate to /run/shm and make sure there is a directory there called mysqltemp.

cd /run/shm
ls -l

Finally, backup and then modify our MySQL my.cnf file.

sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.backup1
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):

tmpdir   = /run/shm/mysqltemp

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…

sudo vim /etc/apparmor.d/usr.sbin.mysqld

Add the following three lines to the bottom of the file, before the closing “}”

  /run/shm/* rw,
  /run/shm/mysqltemp/ rw,
  /run/shm/mysqltemp/* rw,

Save that file and close it, then tell app armor to reload all profiles…

sudo /etc/init.d/apparmor reload

Finally, restart MySQL

service mysql restart

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.


1 of 1

8 comments on: Create a RAM Disk in Linux & Use It to Speed Up MySQL…

  1. Pingback: Cómo crear sobre memoria RAM un disco temporal en linux » Blog OpenAlfa

  2. mitch

    You may want to mount the ram drive you create ? LOL I think this doc needs some updating..

    • nbeam


      Yes, if you create a separate/new RAM disk you would need to mount it and if you want it automounted it would need to go in your FSTAB. However, per the article, I suggest the use the /run/shm directory already present in Ubuntu which is a perfectly workable, already auto-mounted, RAM drive.

  3. Lee

    Is this necessary? I thought MySQL already have https://dev.mysql.com/doc/refman/5.7/en/innodb-buffer-pool.html ?

    • nbeam

      Very nice! – I wasn’t aware of the buffer-pool at all. It looks like with some tuning you can increase your buffer pool and also break it up into multiple files to reduce contention (according to the link). So my question is, how do MySQL usage of the buffer pool compare to usage of the TempDir? Perhaps once the buffer pool fills up it starts paging stuff to the tempdir? In which case just increasing the buffer pool size would make more sense than doing all of the above. I want to research this out. I also know that some databases or at least portions of databases can be run on MyISAM. The default is InnoDB I think but I know I have run into situations in the past where the recommendation was for certain tables to be set to MyISAM. If that is the case, does MyISAM have similar functionality and/or does it benefit from what is laid out in the article? I am not not a database guru so I am asking honest questions 🙂 – Thanks!

      • kocoten1992

        It does has http://dev.mysql.com/doc/refman/5.7/en/myisam-key-cache.html, it’ll work as you think (if not, submit a bug ticket to them :P).

        I’ll be honest too, I’ve never tuning MySQL once in life, just an incident to know that functionality exist, can’t answer harder question than that, maybe the docs or the book High Performance MySQL 3rd would help.

        If it didn’t, then set up test environment and test your hypothesis (guess who is the guru now).

    • nbeam

      I started doing a bit of digging about trying to find out what moving the Temp folder storage location does for you vs. increasing InnoDB buffer pool. I came up with nothing. Essentially I found articles advocating for doing both. It’s curious, I would love to setup a test environment and fiddle around with it some day. Some day being the operative clause though 🙁 – My time is really tight right now unfortunately.

Leave a Reply