根据工作环境,自定义一个启动脚本

---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

MY_CNF=/usr/local/mysql/my.cnf #mysql的配置文件

MYSQLD_SAFE=/usr/local/mysql/bin/mysqld_safe

source /usr/local/mysql/scripts/mysql_env.ini

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_SAFE ]; then
echo "mysql安装时启动文件未安装到$MYSQLD_SAFE或无执行权限"
exit 1 #0是执行成功,1是执行不成功
else
if [ $PORTNUM = 1 ]; then
echo "A mysqld process already exists,please stop mysql at first! "
exit 0
else
echo "启动本机mysql端口为:$MYSQL_PORT的服务"
$MYSQLD_SAFE --defaults-file=$MY_CNF > /dev/null &
while [ $INIT_NUM -le 5 ]
do
PORTNUM=`netstat -lnt|grep ${MYSQL_PORT}|wc -l`
echo "mysql启动中....请稍等..."
sleep 5
if [ $PORTNUM = 1 ];
then
echo "mysql ****启动成功****"
exit 0
fi

INIT_NUM=$(($INIT_NUM +1))
done
fi
echo -e "mysql启动失败,请检查错误日志`echo 'cat ' ${ERRORLOG}`"
echo "*****************************************"
exit 0
fi

---3.加入环境变量即可

echo "export PATH=/usr/local/mysql/scripts:\$PATH">>~/.bash_profile

source ~/.bash_profile