Skip to content

Instantly share code, notes, and snippets.

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 JackDanger/768034169df505a00ac19b7aa1e92de1 to your computer and use it in GitHub Desktop.
Save JackDanger/768034169df505a00ac19b7aa1e92de1 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script works on OS X with Homebrew Given a specific MySQL version this
# script will ensure that version is installed, nothing incompatible is
# blocking it from working, and the mysqld process responds to queries.
#
mysql_version=$1
if [[ -z "${mysql_version}" ]]; then
echo "USAGE: ${0} MYSQL_VERSION_THAT_HOMEBREW_RECOGNIZES"
echo "example: ${0} mysql@5.5"
exit 1
fi
set -uo pipefail
main() {
set +u
local mysql_version=$1
set -u
# Remove all mysql version
brew services list | egrep 'mysql|percona' | awk '{print $1}' | while read version; do
brew services stop ${version}
brew uninstall --force ${version}
done
# If MySQL 8 is somehow still listening on port 3306, shut it down
if echo '' | nc localhost 3306 | grep -q caching_sha2; then
killall -9 mysqld
fi
brew reinstall --force ${mysql_version}
brew link --force --overwrite ${mysql_version}
brew services restart ${mysql_version}
# Wait until mysqld is available, or 30 seconds
wait_time=0
until nc -z localhost 3306 || [ $wait_time -eq 30 ]; do
sleep 1
done
sleep 1 # once mysqld is listening, give it a moment to serve queries
# If myqsld found a data dir from a newer MySQL version, move this directory
# and reinstall
log_err_msg="Unknown/unsupported storage engine: InnoDB"
if grep -q "${log_err_msg}" /usr/local/var/mysql/*.err; then
dir=/usr/local/var/mysql
bak=${dir}.bak
echo "MySQL data dir doesn't work with ${mysql_version}. Moving to '${bak}'"
rm -rf ${bak}
mv ${dir} ${bak}
mkdir ${dir}
# Run this whole thing one more time.
return main ${mysql_version}
fi
# Ensure MySQL is actually running and definitely has the right version
if mysql -u root -e 'select CONCAT("mysql@", @@version)' | grep -q ${mysql_version}; then
echo "MySQL installation failed! This is unexpected."
echo "Paste the following output into Slack for help:"
set -x
brew services list
ls -lAG /usr/local/var/mysql*
ps aux | grep mysq[l]
set +x
fi
}
main $mysql_version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment