Skip to content

Instantly share code, notes, and snippets.

@keithresar
Last active January 4, 2017 18:45
Show Gist options
  • Save keithresar/43d258213dbe2c3c588b2095618a69cf to your computer and use it in GitHub Desktop.
Save keithresar/43d258213dbe2c3c588b2095618a69cf to your computer and use it in GitHub Desktop.
Launch client to access MySQL service inside OpenShift project
#!/usr/bin/env bash
#
# Assumes the following environment variables are available in the project:
#
# MYSQL_USER, MYSQL_USER, MYSQL_DATABASE
#
# This script will discover the environment variables, open a port-forwarding request for
# MySQL, then will open the client using the project credentials.
#
# Change to the name of the mysql service in your project
DB_SVC_NAME=mysql
if [ $$ -gt 65535 ]; then
port=`printf "%.0f" $(echo "scale=2;$$/2" | bc)`
else
port=$$
fi
# Forward port
mysql_pod=`oc get pods -l name=${DB_SVC_NAME} |grep Running|grep -v deploy| awk '{print $1}'`
oc port-forward ${mysql_pod} $port:3306 >/dev/null 2>&1 &
port_fw_pid=$!
sleep 2
# Expose project environment variables
export `oc env dc/${DB_SVC_NAME} --list |grep -v '^#'`
# Open mysql connection in foreground
MYSQL_PWD=${MYSQL_PASSWORD} mysql -h 127.0.0.1 -P ${port} --prompt="$(oc status | head -1 | awk '{print $3}')> " -u "${MYSQL_USER}" "${MYSQL_DATABASE}" $*
# Close forwarding connection
kill ${port_fw_pid}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment