mysql 命令 kill_Mysql单机多实例服务启停shell脚本

ffabb127a852350b5531b6e0be374852.png

在中小型公司,很多情况,一台性能比较好的服务器,一般都会安装多个mysql数据库实例,当公司还没有数据库运维平台时,数据库管理人员启停mysql库时,就比较痛苦,下面就分享一个Mysql单机多实例服务启停shell脚本,其内容如下所示

mysql.sh#!/bin/sh[ -f /etc/init.d/functions ] && . /etc/init.d/functionsMysql_User=rootMysql_Password=rootMysql_Port=$1Mysql_CNF=/data/mysql/conf/${Mysql_Port}/my.cnfMysql_Sock=/data/mysql/run/${Mysql_Port}/mysql.sockCmd_Path=/data/mysql-5.7.26/binfun_usage(){        echo "USAGE $0:{start|stop|restart}"                exit 1}fun_start(){if [ -e $Mysql_Sock ];then         action "MySQL is running." /bin/false   else         /bin/sh ${Cmd_Path}/mysqld_safe --defaults-file=$Mysql_CNF  2>&1 >/dev/null & #Mysql start        sleep 10        netstat -lntup |grep ${Mysql_Port}  >/dev/null  #acheck mysql process   [ $? -eq 0 ]&&action "Mysql start successfully." /bin/true || action "Mysql startup failure." /bin/falsefi}fun_stop(){if [ ! -e $Mysql_Sock  ];then         action "MyySQL is not run." /bin/false    exit 1 else        ${Cmd_Path}/mysqladmin -u${Mysql_User} -p${Mysql_Password} -S ${Mysql_Sock} shutdown        netstat -lntup |grep ${Mysql_Port} >/dev/null   [ $? -ne 0 ]&& action "Mysql stop is successfully." /bin/true || action "Mysql stop is failure." /bin/false  fi}fun_restart(){        fun_stop         sleep 2        fun_start        netstat -lntup |grep ${Mysql_Port}  >/dev/null         [ $? -eq 0 ]&& action "Mysql restart is successfully." /bin/true || action "Mysql restart is failure." /bin/falseexit 1}case $2 instart)        fun_start        ;;stop)        fun_stop        ;;restart)        fun_restart        ;;*)          fun_usage        ;;esac
588c9903b6274670f561d6a9f2c11f93.png

脚本使用也非常简单,便捷

./mysql.sh 3306 start./mysql.sh 3306 stop./mysql.sh 3306 restart

细心的同学可能已经发现了,在这个脚本里,停数据库服务时,需要mysql数据库的root密码,为什么有的方式停数据库服务不需要root密码呢,例如

/etc/init.d/mysql stop

其实,这里的/etc/init.d/mysql也是一个shell脚本,他的来源于mysql安装路径下的mysql-5.7.26/support-files/mysql.server文件,查看mysql.server文件内容,就可以找到mysql停服务的一段逻辑

'stop')    # Stop daemon. We use a signal here to avoid having to know the    # root password.    if test -s "$mysqld_pid_file_path"    then      # signal mysqld_safe that it needs to stop      touch "$mysqld_pid_file_path.shutdown"      mysqld_pid=`cat "$mysqld_pid_file_path"`      if (kill -0 $mysqld_pid 2>/dev/null)      then        echo $echo_n "Shutting down MySQL"        kill $mysqld_pid        # mysqld should remove the pid file when it exits, so wait for it.        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?      else        log_failure_msg "MySQL server process #$mysqld_pid is not running!"        rm "$mysqld_pid_file_path"      fi      # Delete lock for RedHat / SuSE      if test -f "$lock_file_path"      then        rm -f "$lock_file_path"      fi      exit $return_value    else      log_failure_msg "MySQL server PID file could not be found!"    fi    ;;
f937bab0a38d6333bb8815ff82102d2a.png

逻辑很简单,先创建一个/data/mysql/run/3306/mysql.pid.shutdown文件,给mysql_safe发一个信号,说mysql要停服务了,然后用kill -0验证mysql的进程是否存在,如果存在,就kill掉mysql进程,最后删除lock文件,这里是用操作系统的kill命令直接杀掉进程来停服务的,所以不用mysql的root密码,当然如果你为了密码安全,也可以将脚本停服务的逻辑,修改成这样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值