首页 > 数据库 > MySQL > 正文

检查mysql是否成功启动的方法(bat+bash)

2024-07-24 12:44:39
字体:
来源:转载
供稿:网友

很多朋友更喜欢windows下面的可以参考下面的代码

第一种方法:也比较普遍的方法,包括rsync、serv_u等都可以,通过查找进程中的exe文件,例如mysql就是mysqld.exe

@echo off for /f "usebackq" %%i in (`"tasklist|find /c "mysqld.exe""`) do ( set chkstat=%%i ) if %chkstat% == 0 ( net start mysqlecho ================ >> %date:~0,10%.log echo %date% %time% check mysql service stop >> %date:~0,10%.log echo restart mysql service! >> %date:~0,10%.log echo ================ >> %date:~0,10%.log )

第二种方法:

@echo offfor /f "skip=3 tokens=4" %%i in ('sc query mysql') do set "zt=%%i" &goto :next:nextif /i "%zt%"=="RUNNING" (echo 已经发现该服务在运行,正在已经关闭服务net stop mysql) else (echo 该服务现在处理停止状态,正在现在开启服务net start mysql)exitpause

linux系统中通过shell命令实现

检测 MySQL 是否宕掉,如果宕掉则启动

如果你的 MySQL 经常宕机,可以用这个脚本来实现宕机后自动启动,把它加到 crontab 里执行即可。
详细介绍请看 http://www.codeproject.com/Articles/988967/Mysql-Uptime-Check-Script

#!/bin/bash result=`/usr/bin/mysqladmin ping`expected='mysqld is alive' if [[ "$result" != "$expected" ]]thenecho "It's dead - restart mysql" # email subjectSUBJECT="[MYSQL ERROR] - Attempting to restart service" # Email To ?EMAIL="info@endyourif.com" # Email text/messageEMAILMESSAGE="/tmp/emailmessage.txt"echo "$result was received"> $EMAILMESSAGEecho "when we were expected $expected" >>$EMAILMESSAGE# send an email using /bin/mailmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE sudo /etc/init.d/mysql restartfi

方法二、

#!/bin/bash#mysql checkPORT="0"PORT=`netstat -lnt | grep 3306 | wc -l `echo $PORTif [ $PORT -eq 1 ] thenecho "mysql is running"elseecho "mysql is not running"echo "progrome reeady to start mysql "sudo service mysql start./check_mysql.shfi

您可能感兴趣的文章:

Ajax PHP JavaScript MySQL实现简易无刷新在线聊天室MySql5.6使用validate password 插件加强密码强度的安装及使用方法mysql(master/slave)主从复制原理及配置图文详解Node.js中JavaScript操作MySQL的常用方法整理用Python的Flask框架结合MySQL写一个内存监控程序mysql ERROR 1044 (42000): Access denied for user ''''@''localhost'' to databasemysql Access denied for user ‘root’@’localhost’ (using password: YES)解决方法mysql 字段as详解及实例代码
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表