Last active
August 10, 2018 04:48
-
-
Save CMCDragonkai/9679c9e22f1d4bf4685f4dce7ba60893 to your computer and use it in GitHub Desktop.
MySQL in Current Working Directory #mysql
This file contains 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 bash | |
# for when you want to start mysql in a current working directory | |
# unlike postgresql, many of the mysql commands do not use environment variables | |
# which requires you to use the aliases for shell commands | |
# just remember that aliases are not inherited by subprocesses | |
# externally set environment variables | |
# any variable that is unset will be defaulted | |
# if the variable is set and an empty string, it will be used literally | |
unset MYSQL_UNIX_PORT | |
unset MYSQL_PWD | |
unset MYSQL_HOST | |
unset MYSQL_TCP_PORT | |
# script | |
MYSQL_DATA="$(pwd)/.mydata" | |
mkdir --parents $MYSQL_DATA | |
mysqld --initialize-insecure --datadir=$MYSQL_DATA | |
export MYSQL_HOME="$(pwd)" | |
export MYSQL_UNIX_PORT="${MYSQL_UNIX_PORT-$MYSQL_DATA/mysql.sock}" | |
alias mysqladmin="mysqladmin \ | |
--socket="$MYSQL_UNIX_PORT" \ | |
--host="$MYSQL_HOST" \ | |
--port="$MYSQL_TCP_PORT"" | |
alias mysql="mysql \ | |
--socket="$MYSQL_UNIX_PORT" \ | |
--host="$MYSQL_HOST" \ | |
--port="$MYSQL_TCP_PORT"" | |
alias mysqldump="mysqldump \ | |
--socket="$MYSQL_UNIX_PORT" \ | |
--host="$MYSQL_HOST" \ | |
--port="$MYSQL_TCP_PORT"" | |
if ! { mysqladmin --protocol=socket status || | |
mysqladmin --protocol=tcp status; }; | |
then | |
if [ -z "${MYSQL_HOST+x}" -o -z "${MYSQL_TCP_PORT+x}" ]; then | |
mysqld \ | |
--datadir="$MYSQL_DATA" \ | |
--socket="$MYSQL_UNIX_PORT" \ | |
--skip-networking \ | |
& | |
else | |
mysqld \ | |
--datadir="$MYSQL_DATA" \ | |
--socket="$MYSQL_UNIX_PORT" \ | |
--bind-address="$MYSQL_HOST" \ | |
--port="$MYSQL_TCP_PORT" \ | |
& | |
fi | |
fi | |
# the above script may have to be updated if using `--socket= --host= --port=` becomes an error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment