[sql]mysql启停脚本

时间:2021-11-21 01:21:17

写了个较为完善的mysql多实例的启动停止脚本.

[root@lanny 3307]# cat mysql
#!/bin/sh
[ $# != 1 ]&&{
echo "USAGE:/data/3307/mysql {start|stop}"
exit 0
} if [ "$1" == "start" ];then
if [ ! -f /data/3307/mysqld.pid ];then
/application/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 >/dev/null &
sleep 5
echo "Starting MySQL..."
else
echo "MySQL is running already!"
fi
elif [ "$1" == "stop" ];then
if [ -f /data/3307/mysqld.pid ];then
/application/mysql/bin/mysqladmin -S /data/3307/mysql.sock -uroot -p123456 shutdown 2>&1 >/dev/null &
echo "Stoping MySQL..."
else
echo "MySQL is Stopped already!"
fi
fi

注意点:

1.mysql放在/data/3307/目录下,且有执行权限

2,脚本里命令用全路径,譬如

   /application/mysql/bin/mysqladmin

3,判断一个文件存在与否

存在:  [ -f /data/3307/mysqld.pid ];

不存在:   [ ! -f /data/3307/mysqld.pid ];

4,字符串比较几点注意:4.1 双引号 4.2等号两边空格,且==  4.3[  ]两边均空格

[ "$1" == "stop" ]