Skip to content

Instantly share code, notes, and snippets.

@arnehormann
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnehormann/57943d7f67a82f966724 to your computer and use it in GitHub Desktop.
Save arnehormann/57943d7f67a82f966724 to your computer and use it in GitHub Desktop.
Startup MySQL
#!/bin/bash
set -e -u -o pipefail
PWD=`pwd`
WHOAMI=`whoami`
BASEDIR=${MYSQL_SERVER_BASEDIR?missing MYSQL_SERVER_BASEDIR}
# default directories are subdirectories of where this script was run from
DATADIR=${MYSQL_SERVER_DATADIR:-${PWD}/data}
LOGDIR=${MYSQL_SERVER_DATADIR:-${PWD}/log}
RUNDIR=${MYSQL_SERVER_RUNDIR:-${PWD}/run}
TMPDIR=${MYSQL_SERVER_RUNDIR:-${PWD}/tmp}
SLAVELOADDIR=${MYSQL_SERVER_RUNDIR:-${PWD}/loaddata}
SOCKET=${MYSQL_SERVER_FILE_SOCK:-${RUNDIR}/mysql.sock}
PID=${MYSQL_SERVER_FILE_PID:-${RUNDIR}/mysql.pid}
LOGOUT=${MYSQL_SERVER_FILE_LOG:-${LOGDIR}/out.log}
LOGERR=${MYSQL_SERVER_FILE_ERR:-${LOGDIR}/out.err}
USER=${MYSQL_SERVER_USER:-${WHOAMI}}
# TODO: check if dirs / files exist and belong to the user, else...
# setup new ones - and init the db base tables!
mkdir -p "$DATADIR" "$LOGDIR" "$RUNDIR" "$TMPDIR" "$SLAVELOADDIR"
chown "$USER" "$DATADIR" "$LOGDIR" "$RUNDIR" "$TMPDIR" "$SLAVELOADDIR"
# direct output to stderr
[ -f "$LOGOUT" ] || ln -s /dev/stdout "${LOGOUT}"
[ -f "$LOGERR" ] || ln -s /dev/stderr "${LOGERR}"
su -m "$USER" -c mysqld --defaults-file=<(cat <<-MYSQLD_CONFIG
[mysqld]
basedir = ${BASEDIR}
datadir = ${DATADIR}
socket = ${SOCKET}
pid_file = ${PID}
tmpdir = ${TMPDIR}
slave_load_tmpdir = ${SLAVELOADDIR}
log_error = ${LOGERR}
general_log_file = ${LOGOUT}
MYSQLD_CONFIG
)
@arnehormann
Copy link
Author

totally untested partial conversion of a holy mess to a 12 factor app

uses [http://www.tldp.org/LDP/abs/html/parameter-substitution.html](BASH parameter substition), HEREDOC and redirection to generate a temporary config file on the fly from environment variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment