Mysql on ramdisk via osx homebrew

Having mysql running on a ramdisk can be a much faster way to unit test a database intensive application.

Supposing you’ve installed mysql or percosa server via homebrew (cause the paths it uses) you can directly run this script to create a virtual disk and run a mysql instance on it.
It will create a simple configuration file stored in /usr/local/etc/my_ramdisk.cnf which will be used by the additional mysql instance.

By default the server will run on port 3307 listening on 0.0.0.0.

Here is the attached script:

#!/usr/bin/env ruby
shutdown = false
cnf_path = '/usr/local/etc/my_ramdisk.cnf'
ramdisk_path = '/Volumes/RAMDisk'
loop { case ARGV[0]
when 'shutdown' then ARGV.shift; shutdown = true
else break
end; }
if shutdown
puts 'Shutting down mysql on ramdisk'
if system('mysqladmin -u root -h 127.0.0.1 -P3307 shutdown')
puts 'Shutdown initated, waiting 5 seconds and removing ramdisk'
sleep 5
puts 'Unmounting ramdisk'
if system('hdiutil detach ' + ramdisk_path)
puts 'Unmounting done'
else
puts 'Unmounting failed, waiting 5 more seconds and retrying'
if system('hdiutil detach ' + ramdisk_path)
puts 'Unmounting done'
else
puts 'Unmounting failed, please try to unmount manually'
abort
end
end
else
puts 'Shutdown failed'
end
exit
end
if File.directory?(ramdisk_path)
puts 'Ramdisk already exist, please use "./mysql_ramdisk shutdown" to clean up things'
abort
end
puts 'Detecting mysql vendor'
if File.directory?('/usr/local/opt/percona-server')
mysql_basedir = '/usr/local/opt/percona-server'
elsif File.directory?('/usr/local/opt/mysql')
mysql_basedir = '/usr/local/opt/mysql'
else
puts 'Cannot find a valid mysql basedir'
abort
end
puts 'Writing mysql configuration file ' + cnf_path
begin
file = File.open(cnf_path, "w")
file.write("[mysqld]
port = 3307
socket = /tmp/mysql-ramdisk.sock
datadir = #{ramdisk_path}")
rescue IOError => e
puts 'Error writing file!'
abort
ensure
file.close unless file == nil
end
puts 'Creating ramdisk at ' + ramdisk_path
if system('diskutil erasevolume HFS+ RAMDisk `hdiutil attach -nomount ram://1048576` > /dev/null')
puts 'Creation succesfull'
else
puts 'Creation failed'
end
puts 'Initialiting default mysql database into ramdisk'
if system('mysql_install_db --basedir=' + mysql_basedir + ' --datadir=' + ramdisk_path + ' > /dev/null')
puts 'Initialization done'
else
puts 'Initialization failed'
abort
end
puts 'Starting mysql instance on port 3307 using ramdisk as data folder storage'
spawn('mysqld_safe --defaults-file=' + cnf_path + ' > /dev/null')

Just run it via ./mysql_ramdisk.rb and shutdown using ./mysql_ramdisk shutdown ;)