Skip to content

Instantly share code, notes, and snippets.

@thomet
Last active January 4, 2016 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomet/8576948 to your computer and use it in GitHub Desktop.
Save thomet/8576948 to your computer and use it in GitHub Desktop.
Creates a mysql database in a ramdisk with socket support (/tmp/mysql_ram.socket)
DEVICE=`hdiutil attach -nomount ram://2048000` # 2GB Ramdisk
diskutil erasevolume HFS+ "ramdisk" $DEVICE # mounted automaticaly in /Volumes/ramdisk
BASE_DIR=/usr/local/opt/mysql # MySQL home installed via Homebrew
DATA_DIR=/Volumes/ramdisk # Ramdisk home
SOCKET=/tmp/mysql_ram.sock # SOcket for the new mysql server instance
PID_FILE=/tmp/mysql_ram.pid # Pid file for the mysql server
# Create mysql db in ramdisk dir
mysql_install_db --basedir="$BASE_DIR" --datadir="$DATA_DIR"
# Start mysql server (only with socket support)
(mysqld --basedir="$BASE_DIR" --datadir="$DATA_DIR" --plugin-dir="$BASE_DIR/lib/plugin" --pid-file="$PID_FILE" --socket=$SOCKET --skip-networking) &
# Wait for the mysql server
while [ ! -e $SOCKET ] ; do
sleep 1
done
# Setup the new db server
mysql --socket=$SOCKET -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'fuji'@'localhost' IDENTIFIED BY 'yama' WITH GRANT OPTION;"
mysql --socket=$SOCKET -uroot -e "CREATE DATABASE my_database;"
# Catch ctrl-c
trap ctrl_c INT
# Shutdown server and umount ramdisk
function ctrl_c() {
kill -TERM $(cat $PID_FILE)
wait
umount $DATA_DIR
hdiutil detach $DEVICE
}
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment