I have been writing Django tests on a project with a fair amount of raw mysql code, which precludes the use of sqlite for any advanced MySQL queries. I found that simply bootstrapping the test environment was taking about 22 seconds, meaning tests weren't going to get run very often. Googling revealed a few solutions for Linux but none for OS X.

Configuration

  • Mac OS X Snow Leopard (10.6.3)
  • MySQL 5.1.42 64-bit, installed at /usr/local/mysql

Create the ramdisk

To create the ramdisk you need to specify the size in 512K blocks. Use this formula to calculate the number of blocks to use in the ram argument below.

blocks = Size Of Ramdisk In MB * 1048576 / 512

For example, a 100MB ramdisk would be 204800 blocks. Running the following command will create, mount and format the volume using HFS+

diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://<blocks>`

Install new DB

/usr/local/mysql/scripts/mysql_install_db \
    --user=mysql \
    --basedir=/usr/local/mysql \
    --datadir=/Volumes/ramdisk

More info on mysql_install_db can be found in the docs.

Start MySQL

/usr/local/mysql/bin/mysqld \
    --basedir=/usr/local/mysql \
    --datadir=/Volumes/ramdisk \
    --user=mysql \
    --log-error=/Volumes/ramdisk/mysql.ramdisk.err \
    --pid-file=/Volumes/ramdisk/mysql.ramdisk.pid \
    --port=3308 \
    --socket=/tmp/mysql_ram.sock

Test connection

/usr/local/mysql/bin/mysql \
    -u root \
    --socket=/tmp/mysql_ram.sock \
    -e "SHOW DATABASES;"

[Update 2010-10-07]

Michaux Kelley has pushed a python script to github to do this all for you - check it out.

Related tags: mysql, osx