Created
July 19, 2013 13:03
-
-
Save alex88/6038973 to your computer and use it in GitHub Desktop.
Creates a ramdisk on osx and runs a mysql instance using pre-installed homebrew percona or mysql server Use directly to create, with 'shutdown' argument to tear off the mysql and the disk To connect use -h 127.0.0.1 -P3307
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment