Wednesday, April 7, 2010

[Level 2] Script for mysql.

Because I have multi MySQL environments, so I have to assign different parameter (especially the "--socket" option) in different environment,
and it make me crazy.
So I create a folder "/shell" for store my scripts and create a alias "mysql" to the script in /shell.
My sample as the following:

.bashrc:
# cat ~/.bashrc | tail -3
export MYSQL_HOME=/usr/local/mysql/5.1
export PATH=$PATH:$MYSQL_HOME/bin
alias mysql="/shell/mysql"

Script /shell/mysql:
 #!/bin/bash -vx

echo "call $0..."
sMySQLCmdList="/usr/mysql/bin/mysql /usr/local/mysql/bin/mysql"
#sMySQLCmd=""
for sItem in $sMySQLCmdList
do
  if [ -f $sItem ]
  then
    sMySQLCmd=$sItem
    break
  fi
done
if pgrep mysqld
then
  mysql_socket=`ps -ef | grep mysqld | grep -v monitor | grep socket | sed -e 's/.*--socket=//' |     cut -d' ' -f1`
 
  if [ `echo $@ | grep -c -- -h` -eq 1 ]
  then
    $sMySQLCmd $@
  elif [ ! -z $mysql_socket ]
  then
    $sMySQLCmd --socket=$mysql_socket $@
  else
    $sMySQLCmd $@
  fi
else
  echo "No Running MySQL Server, Exit mysql" && exit 1
fi
 


Wish this helps.

regards,
Stanley Huang