Small script to run a instance of mysql in my home directory using the binary provided by the OS. Maybe some day I will make it more usable but for the time being it suits my needs.
#!/bin/bash
MYSQL_HOME=$HOME
MYSQL_USER=$USER
MYSQL_PORT=13306
ACTION=$1
if [ $ACTION == 'init' ]; then
echo "initalising new mysql installation at $MYSQL_HOME/var/lib/mysql"
rm -rf $MYSQL_HOME/var/lib/mysql
mkdir -p $MYSQL_HOME/var/lib/mysql $MYSQL_HOME/mysql/data $HOME/var/lib/mysql/tmp
mysql_install_db --user=$MYSQL_USER --datadir=$MYSQL_HOME/var/lib/mysql/data/
cat < $HOME/.my.cnf
[mysqld]
user=$MYSQL_USER
datadir=$MYSQL_HOME/var/lib/mysql/data
log-error=mysqld.log
pid-file=mysqld.pid
socket=$MYSQL_HOME/var/lib/mysql/tmp/mysql.sock
port=$MYSQL_PORT
[client]
user=$MYSQL_USER
socket=$MYSQL_HOME/var/lib/mysql/tmp/mysql.sock
port=$MYSQL_PORT
[safe_mysqld]
log-error=mysqld.log
pid-file=mysqld.pid
port=$MYSQL_PORT
EOT
echo " Done"
elif [ $ACTION == 'start' ];then
echo -n "Starting mysqld as $MYSQL_USER on $MYSQL_PORT"
nohup mysqld_safe &>/dev/null &
echo " Done"
elif [ $ACTION == 'stop' ];then
PID=$(cat $MYSQL_HOME/var/lib/mysql/data/mysqld.pid)
echo -n "Stopping mysql instance [$PID]"
kill `cat $MYSQL_HOME/var/lib/mysql/data/mysqld.pid`
echo " Done"
else
echo "Unknown command '$1'"
fi
UPDATED SCRIPT:
I decided to update the script and make it less hacky
#!/bin/bash
MYSQL_HOME=$HOME
MYSQL_USER=$USER
MYSQL_PORT=13306
function init {
echo "initalising new mysql installation at $MYSQL_HOME/var/lib/mysql"
rm -rf $MYSQL_HOME/var/lib/mysql
mkdir -p $MYSQL_HOME/var/lib/mysql $MYSQL_HOME/mysql/data $HOME/var/lib/mysql/tmp
mysql_install_db --user=$MYSQL_USER --datadir=$MYSQL_HOME/var/lib/mysql/data/
cat < $HOME/.my.cnf
[mysqld]
user=$MYSQL_USER
datadir=$MYSQL_HOME/var/lib/mysql/data
log-error=mysqld.log
pid-file=mysqld.pid
socket=$MYSQL_HOME/var/lib/mysql/tmp/mysql.sock
port=$MYSQL_PORT
innodb_file_per_table
[client]
user=$MYSQL_USER
socket=$MYSQL_HOME/var/lib/mysql/tmp/mysql.sock
port=$MYSQL_PORT
[safe_mysqld]
log-error=mysqld.log
pid-file=mysqld.pid
port=$MYSQL_PORT
EOT
echo " Done"
}
function start {
echo -n "Starting mysqld as $MYSQL_USER on $MYSQL_PORT"
nohup mysqld_safe &>/dev/null &
echo " Done"
}
function stop {
if [ -e $MYSQL_HOME/var/lib/mysql/data/mysqld.pid ]; then
PID=$(cat $MYSQL_HOME/var/lib/mysql/data/mysqld.pid)
echo -n "Stopping mysql instance [$PID]"
kill $PID
echo " Done"
else
echo "mysqld.pid file doesnt exist"
fi
}
function usage {
echo "Unknown command '$1'"
}
function status {
if [ -e $MYSQL_HOME/var/lib/mysql/data/mysqld.pid ]; then
PID=$(cat $MYSQL_HOME/var/lib/mysql/data/mysqld.pid)
ps -p $PID &>/dev/null
RTN=$?
else
RTN=-1
fi
if [ $RTN -eq 0 ]; then
echo "Mysql Running, pid:$PID"
elif [ $RTN -eq 1 ]; then
echo "mysql stopped but pidfile exists"
elif [ $RTN -eq -1 ]; then
echo "Stopped"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
init)
init
;;
*)
echo $"Usage: $0 {start|stop|status|restart|init}"
exit 1
esac