根據工作環境,自定義一個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
#[email protected]
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