Tag Archive for 'unit'

How to use a RAM / memory disk for MySQL in Rails

I wanted to use a RAM disk for my MySQL data files when running my tests for a Rails project. I succeeded, but it only cut 2 seconds off of my ~30 second test suite for my functional tests, and didn't cut any time off of my unit tests. But nevertheless, here is how I achieved this, in case it is useful to others. All of this is on OS X, using MySQL 5 from Macports.

What we are doing is creating a RAM disk, and then creating a seperate MySQL instance that stores its data on that RAM disk.

BASH:
  1. # Create the RAM disk
  2. hdid -nomount ram://52428800
  3. newfs_hfs /dev/disk1
  4. mkdir /tmp/ramdisk1
  5. mount -t hfs /dev/disk1 /tmp/ramdisk1
  6.  
  7. # Initialize the MySQL environment
  8. mysql_install_db5 --datadir=/tmp/ramdisk1
  9.  
  10. # Start the MySQL server
  11. /opt/local/libexec/mysqld --basedir=/opt/local --datadir=/tmp/ramdisk1  --pid-file=/tmp/mysql_memory_localhost.pid --port=10000 --socket=/tmp/mysql_memory.sock
  12.  
  13. # Create your test database
  14. mysqladmin5 --socket=/tmp/mysql_memory.sock -uroot create projectname_test

Now in your database.yml file, add socket: /tmp/mysql_memory.sock to the test database configuration.

(thanks to this tutorial for how-to create a RAM disk in OS X)




Close
Powered by ShareThis