Skip to content

Instantly share code, notes, and snippets.

@bobmaerten
Last active August 29, 2015 14:06
Show Gist options
  • Save bobmaerten/0b8c35932615f3e5006b to your computer and use it in GitHub Desktop.
Save bobmaerten/0b8c35932615f3e5006b to your computer and use it in GitHub Desktop.
Mysql On Demand
#!/usr/bin/env bash
usage() {
echo "Usage: `basename $0` {start|stop|status|connect}"
exit 1
}
if [ -z "$1" ]; then
usage
fi
NICKNAME=${2:-default}
ROOT_PASSWD='root'
case "$1" in
start)
docker ps -a | grep -q mysql-${NICKNAME} || $(ID=$(docker run --name mysql-${NICKNAME} -e MYSQL_ROOT_PASSWORD=${ROOT_PASSWD} -d mysql))
IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql-${NICKNAME}) && echo "mysql-${NICKNAME}: started."
;;
status)
docker ps -a | grep -q mysql-${NICKNAME} && echo "mysql-${NICKNAME}: running." || echo "mysql-${NICKNAME}: not running."
usage
;;
stop)
docker ps -a | grep -q mysql-${NICKNAME} && (ID=$(docker stop mysql-${NICKNAME}) && ID=$(docker rm mysql-${NICKNAME}) && echo "mysql-${NICKNAME}: stopped.") || (echo "mysql-${NICKNAME}: not running.")
;;
connect)
docker ps -a | grep -q mysql-${NICKNAME} && (docker run -it --link mysql-${NICKNAME}:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"') || echo "mysql-${NICKNAME}: not running."
;;
*)
usage
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment