Shell編程---mysql單實例啓動停止腳本

企業案例:開發mysql單實例啓動停止腳本

分析:

  1. 首先確保已經將數據庫安裝成功
  2. 找到mysql數據庫啓動的實質命令 mysqld_safe --user=mysql &
  3. 找到mysql數據庫停止的實質命令 mysqladmin -u"root" -p"password" shutdown
  4. 進行腳本開發進行腳本開發

數據庫安裝

數據庫安裝請參照:https://blog.csdn.net/yuki5233/article/details/81736439

找到相關命令

[root@myhost ~]# which mysqld_safe
/usr/local/mysql/bin/mysqld_safe    
#/usr/local/mysql/bin/mysqld_safe --user=mysql &

[root@myhost ~]# which mysqladmin
/usr/local/mysql/bin/mysqladmin
#/usr/local/mysql/bin/mysqladmin -u"root" -p"password" shutdown

開發腳本

[root@myhost ~]# cat /home/script/mysql_01.sh
#!/bin/sh
#加載/etc/profile 配置文件,引入環境變量等
source /etc/profile
#加載 /etc/init.d/functions配置文件,引入linux函數庫
source /etc/init.d/functions

#define variables (定義相關變量)
mysql_path="/usr/local/mysql"
mysql_user="root"
mysql_passwd="root123456"
mysql_port="3306"

############################################################################################

function mysql_usage(){
	echo "$0 {start|stop|restart|status}"
	exit 1
}

function mysql_start(){
	echo "Starting mysql......"
	${mysql_path}/bin/mysqld_safe --user=mysql & >/dev/null 2>&1 &
	if [ $? -eq 0 ];
		then
			sleep 3
			action "start mysql"   /bin/true
		else
			sleep 3
			action "start mysql"   /bin/false
	fi
}


function mysql_stop(){
	echo "Stopping mysql......"
	${mysql_path}/bin/mysqladmin -u ${mysql_user} -p ${mysql_passwd} shutdown
	if [ $? -eq 0 ];
		then
			sleep 3
			action "stop mysql"  /bin/true
		else
			sleep 3
			action "stop mysql" /bin/false
	fi

}

function mysql_status {
	
	port_line=`netstat -lntup|grep ${mysql_port}|wc -l`
	if [ ${port_line} -ge 1 ];
		then
			echo "mysql is running."
		else
			echo "mysql is not running."
	fi
}


############################################################################################

if [ "$1" == "start" ];then
	mysql_start
elif [ "$1" == "stop" ];then
	mysql_stop
elif [ "$1" == "status" ];then
	mysql_status
elif [ "$1" == "restart" ];then
	#重啓mysql即先停止mysql,再啓動mysql (包含兩個步驟)
	mysql_stop
	mysql_start
else
	mysql_usage
	exit 1
fi
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章