当前位置: 首页 > article >正文

Shell篇之编写MySQL启动脚本

Shell篇之编写MySQL启动脚本

1. 脚本内容

vim mysql_ctl.sh
#!/bin/bash

mysql_port=3306
mysql_username="root"
mysql_password="molinker"
mysql_conf="/opt/lanmp/mysql/etc/my.cnf"
mysql_sock="/opt/lanmp/mysql/var/mysql.sock"

function_start_mysql(){
	printf "Starting MySQL ...\n"
	/bin/sh /opt/lanmp/mysql/bin/mysqld_safe --defaults-file=${mysql_conf} 2>&1 > /dev/null &
}

function_status_mysql(){
mysql_stat=0
mysql_tmpa=`ps -ef | grep mysql | grep -v root | awk '{print $2}'`
name="`hostname`.pid"
mysql_tmpb=`cat /opt/lanmp/mysql/var/$name`
if [ `lsof -i:20036 | wc -l` -gt 0 ]; then
  if [ `ps -ef | grep mysql | grep -v grep | wc -l` -gt 0 ]; then
    if [ $mysql_tmpa -eq $mysql_tmpb ]; then
       mysql_stat=1
    fi
  fi
fi

if [ $mysql_stat -eq 1 ]; then
  echo "mysql(pid $mysql_tmpb) is runing!"
elif [ $mysql_stat -eq 0 ]; then
   echo "mysql is not runing!"
fi
}


function_stop_mysql(){
	printf "Stopping MySQL ...\n"
	/opt/lanmp/mysql/bin/mysqladmin --port=${mysql_port} --user=${mysql_username} --password=${mysql_password} --socket=${mysql_sock} shutdown
}

function_restart_mysql(){
	printf "Restarting MySQL ...\n"
	function_stop_mysql
	sleep 1
	function_start_mysql
}

function_kill_mysql(){
	ps -ef | grep "mysqld_safe" | grep -v grep | awk '{print "kill -9 " $2}' | /bin/sh
	ps -ef | grep "mysqld" | grep -v grep | awk '{print "kill -9 " $2}' | /bin/sh
}

if [ "$1" = "start" ]; then
	function_start_mysql
elif [ "$1" = "stop" ]; then
	function_stop_mysql
elif [ "$1" = "status" ]; then
	function_status_mysql
elif [ "$1" = "restart" ]; then
	function_restart_mysql
elif [ "$1" = "kill" ]; then
	function_kill_mysql
else
	printf "Usage: mysql {start|stop|restart|kill|status}\n"
fi

注意事项

mysql_conf:为MySQL对应配置文件路径
mysql_sock:为MySQL对应socket文件

2. 赋权

chmod +x mysql_ctl.sh

3. 命令

./mysql_ctl start
./mysql_ctl status
./mysql_ctl restart

http://www.kler.cn/news/310282.html

相关文章:

  • 9.17日常记录
  • 深度学习-14-深入理解BERT的基本思想和如何训练BERT模型
  • Golang | Leetcode Golang题解之第416题分割等和子集
  • golang学习笔记28——golang中实现多态与面向对象
  • 微信getUserProfile不弹出授权框
  • 编写第一个hadoop3.3.6的mapreduce程序
  • 解决 npm ERR! node-sass 和 gyp ERR! node-gyp 报错问题
  • 第十一章 【后端】商品分类管理微服务(11.4)——spring-boot-devtools
  • play-with-docker使用指南
  • 不想一直走前端,试试产品吧
  • 【AIGC半月报】AIGC大模型启元:2024.09(下)
  • 无人机之控制距离篇
  • CloudFlare问题与CDN问题
  • uni-data-select 使用 localdata 传入数据出现 不回显 | 下拉显示错误的 解决方法
  • 初识ZYNQ——FPGA学习笔记15
  • 观察者模式,回调函数,事件调度
  • K近邻算法_分类鸢尾花数据集
  • 海康威视摄像机和录像机的监控与回放
  • C++/CLI编程知识点小记
  • spug项目实现代码本地启动步骤
  • GEC6818初次连接使用
  • Python快速入门 —— 第六节:模块与包
  • 【VUE3】Vite编译的打包输出dist包大小的小工具
  • React-Hook原理
  • vue的插槽
  • 08 Shell Script条件判断
  • 传输层——UDP协议
  • CentOS 上配置多服务器 SSH 免密登录
  • 【oj刷题】二分查找篇:二分查找算法的原理和应用场景
  • 滤波器的分类