Moving MySQL databases to ramdisk in Ubuntu linux

Posted on Category:UNIX/Linux in general

This post describes steps required to tranfer MySQL database files to RAM by using ramdisk on Linux. If you ever had to work with symfony and frequently rebuilding database schema, this could come in handy…
In this example I’m using Ubuntu 10 64bit LTS distribution.
First, mount a directory in ramdisk:
[php]
$ mount -t tmpfs -o size=400M tmpfs /tmp/ramdisk/
[/php]
This should create a 400 MB ramdisk in your /tmp/ramdisk directory.
Then move MySQL database files to newly created ramdisk. In my configuration, MySQL files are located in /var/lib/mysql.
[php]
$ mv /var/lib/mysql /tmp/ramdisk/mysql
[/php]
Then link back directory in ramdisk to original mysql files location by symbolic link:
[php]
$ ln -s /tmp/ramdisk/mysql /var/lib/mysql
[/php]
If you were copying files as a root, you may have inadvertently changed file’s permissions. All files should be readable by mysql user, so make sure that they have the right owner:
[php]
chown mysql:mysql mysql
[/php]
In case your MySQL service no longer recognizes database files (shows empty database), try to restart mysql service (you have to be sudo or root):
[php]
$ sudo /etc/init.d/mysql restart
[/php]
… and that should be it. Your MySQL database is now running on your RAM, and you should notice considerable improvement in performance.
Note that you should probably back up your mysql database files, as they will dissapear from ramdisk when you shutdown or reboot your computer.