根据工作环境,自定义一个mysql关闭脚本
---1.保存为/usr/local/mysql/scripts/mysql_env.ini
#set mysql init variables
MYSQL_USER=system #mysql的用户名
MYSQL_PASS=‘password‘ #mysql的登录用户密码
#check input parameter or no
if [ $# -ne 1 ] ; then #判断输入的参数个数
echo "mysql port is 3306"
MYSQL_PORT=3306
else
MYSQL_PORT=$1 #$1是传入的第1个位置参数
echo "mysql port is $1"
fi
---2.关闭mysql
#!/bin/sh
#ocpyang@126.com
source /usr/local/mysql/scripts/mysql_env.ini
MY_CNF=/usr/local/mysql/my.cnf #mysql的配置文件
MYSQLD_ADMIN=/usr/local/mysql/bin/mysqladmin
MYSQL_SOCKET=/usr/local/mysql/mysql.sock
MYSQL_CMD="${MYSQLD_ADMIN} -u${MYSQL_USER} -p${MYSQL_PASS} -S ${MYSQL_SOCKET}"
ERRORLOG=`grep -i "^log-error" $MY_CNF |cut -d = -f 2`
INIT_NUM=1
PORTNUM=`netstat -lnt|grep ${MYSQL_PORT}|wc -l`
if [ ! -x $MYSQLD_ADMIN ]; then
echo "mysql安装时启动文件未安装到$MYSQLD_ADMIN或无执行权限"
exit 0
elif [ $PORTNUM = 0 ] ;then
echo "mysql处于关闭状态,无须关闭!"
exit 0
else
echo "关闭mysql服务"
${MYSQL_CMD} shutdown
while [ $INIT_NUM -le 5 ]
do
PORTNUM=`netstat -lnt|grep ${MYSQL_PORT}|wc -l`
echo "mysql关闭中....请稍等..."
sleep 5
if [ $PORTNUM = 0 ];
then
echo "mysql ****关闭成功****"
exit 0
fi
INIT_NUM=$(($INIT_NUM +1))
done
echo -e "mysql关闭失败,请检查错误日志`echo ‘cat ‘ ${ERRORLOG}`"
echo "*****************************************"
exit 0
fi
---3.加入环境变量即可
echo "export PATH=/usr/local/mysql/scripts:\$PATH">>~/.bash_profile
source ~/.bash_profile