MySQL Forums
Forum List  »  Router & Proxy

Re: mysql-proxy init script for redhat based systems
Posted by: simon elliston ball
Date: August 23, 2007 07:58AM

I've just knocked together a version of the init script which is a bit more of a redhat / mysql cross. It takes into account a my.cnf style configuration file, called /etc/mysql-proxy.cf. It's not pretty, but in case anyone wants it...

#!/bin/bash
# mysql-proxy This shell script takes care of starting and stopping
# mysql-proxy.
#
# chkconfig: - 90 25
# description: mysql-proxy is a lightweight proxy for the mysql database \
# protocol that can monitor, analyze or transform communications
# pidfile: /var/run/mysql-proxy.pid
# config: /etc/mysql-proxy.cf

PATH=/usr/bin:/sbin:/bin:/usr/sbin
export PATH

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

LOGFILE=/var/log/mysql-proxy.log
PIDFILE=/var/run/mysql-proxy.pid
CONFFILE=/etc/mysql-proxy.cf

# check if the squid conf file is present
[ -f /etc/mysql-proxy.cf ] || exit 0

# parse the config
get_mysql_option(){
result=`sed -n "s/^[ \t]*$2[ \t]*=[ \t]*//p" "$1" 2>/dev/null | tail -n 1`
if [ -z "$result" ]; then
# not found, use default
result="$3"
else
# found, still have to deal with quoting and end-of-line comments
dequoted=`echo "$result" | sed "s/^'\([^']*\)'.*$/\1/"`
if [ x"$dequoted" != x"$result" ]; then
result="$dequoted"
else
dequoted=`echo "$result" | sed 's/^"\([^"]*\)".*$/\1/'`
if [ x"$dequoted" != x"$result" ]; then
result="$dequoted"
else
result=`echo "$result" | sed 's/^\([^ \t#]*\).*$/\1/'`
fi
fi
fi
}

# Allow the cf file to override pid file location
get_mysql_option $CONFFILE pid-file $PIDFILE
PIDFILE="$result"

# Set some timeouts
PROXY_PIDFILE_TIMEOUT=60
PROXY_SHUTDOWN_TIMEOUT=60

if [ -f /etc/sysconfig/mysql-proxy ]; then
. /etc/sysconfig/mysql-proxy
fi

# determine the name of the mysql-proxt binary
[ -f /usr/sbin/mysql-proxy ] && PROXY=/usr/sbin/mysql-proxy
[ -f /usr/local/sbin/mysql-proxy ] && PROXY=/usr/local/sbin/mysql-proxy

[ -z "$PROXY" ] && exit 0

prog="$PROXY"

RETVAL=0

start() {

get_mysql_option $CONFFILE backend "127.0.0.1:3306"
backend="$result"
get_mysql_option $CONFFILE backend-readonly ""
backend_readonly="$result"
get_mysql_option $CONFFILE address ""
address="$result"
get_mysql_option $CONFFILE port "4040"
port="$result"
get_mysql_option $CONFFILE admin-address ""
admin_address="$result"
get_mysql_option $CONFFILE admin-port "4041"
admin_port="$result"
get_mysql_option $CONFFILE lua-script ""
script="$result"

# Determine the mysql client lib version to figure out if we need the bugfix line
# TODO

# build the arguments
ARGS_BACKEND="--proxy-backend-addresses=`echo $backend | sed 's/,/ --proxy-backend-addresses=/g'`"
if [ "x$backend_readonly" != "x" ]; then
ARGS_ROBACKEND="--proxy-read-only-backend-addresses=`echo $backend_readonly | sed 's/,/ --proxy-read-only-backend-addresses=/g'`"
fi
if [ "x$script" != "x" ]; then
ARGS_SCRIPT="--proxy-lua-script=$script"
fi

ARGS="--proxy-address=$address:$port --admin-address=$admin_address:$admin_port $ARGS_BACKEND $ARGS_ROBACKEND $ARGS_SCRIPT"


echo -n $"Starting $prog:"

$PROXY --daemon --pid-file=$PIDFILE $ARGS >> $LOGFILE 2>&1
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mysql-proxy
[ $RETVAL -eq 0 ] && echo_success
[ $RETVAL -ne 0 ] && echo_failure
echo
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
kill `cat $PIDFILE`
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
rm -f $PIDFILE
rm -f /var/lock/subsys/mysql-proxy
echo_success
echo
else
echo_failure
echo
fi
return $RETVAL
}

reload() {
restart
}

restart() {
stop
sleep 3
start
}

condrestart() {
[ -e /var/lock/subsys/mysql-proxy ] && restart || :
}

rhstatus() {
status $PROXY
}

probe() {
return 0
}

case "$1" in
start)
start
;;

stop)
stop
;;

reload)
reload
;;

restart)
restart
;;

condrestart)
condrestart
;;

status)
rhstatus
;;

probe)
exit 0
;;

*)
echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
exit 1
esac

exit $?

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: mysql-proxy init script for redhat based systems
6607
August 23, 2007 07:58AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.