该脚本只针对多实例的MySQL,并且有一定的限制,是基于http://coosh.blog.51cto.com/6334375/1735271 这篇安装结果。
[root@vmtest ~]# cd /disk2/mysql_multi_instances/3306
[root@vmtest 3306]# vi mysql_server.sh
#!/bin/bash
#2016-01-15 version 1
#Author Coosh
#This script is used in multi thread environment to start|stop|restart the mysqld deamon. One thing should be noticed, we don‘t use "kill" to shutdown the mysqld process, but "mysqladmin shutdown".
#This script should be place in the same directory with the socket file and my.cnf file
export ACTION=$1
export SOCKFILE=./mysql.sock
export CNFFILE=./my.cnf
function start()
{
[ -r $CNFFILE -a -s $CNFFILE ] && mysqld_safe --defaults-file=$CNFFILE &>/dev/null &
if [ $?==0 ]; then
echo ‘Mysql started!‘
else
echo ‘Mysql start failed...‘
fi
}
function stop()
{
if [ -S $SOCKFILE ]; then
echo -e ‘MySql is running.\nPlease enter root password to shut it down‘;
mysqladmin -uroot -p -S ./mysql.sock shutdown ;
if [ $? -ne 0 ]; then
echo ‘Wrong Password! Please redo the command‘;
exit 64 ;
else
echo ‘MySql graceful shutdown!‘
fi
else
echo "MySql isn‘t running.."
fi
}
case $ACTION in
start)
start ;
;;
stop)
stop ;
;;
restart)
stop ;
start ;
;;
*)
echo "Usage $0 start|stop|restart"
;;
esac运行结果
优雅关闭
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",13363,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
[root@vmtest 3306]# ./mysql_server.sh stop
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))启动
[root@vmtest 3306]# ./mysql_server.sh start
Mysql started!
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",14834,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
[root@vmtest 3306]# ./mysql_server.sh restart
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
Mysql started!
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",15585,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))重启(注意上面的3306的pid是15585)
[root@vmtest 3306]# ./mysql_server.sh restart
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
Mysql started!
[root@vmtest 3306]# !ss
ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",16337,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))如果输错了密码,程序会推出
[root@vmtest 3306]# ./mysql_server.sh restart MySql is running. Please enter root password to shut it down Enter password: mysqladmin: connect to server at ‘localhost‘ failed error: ‘Access denied for user ‘root‘@‘localhost‘ (using password: YES)‘ Wrong Password! Please redo the command
原文:http://coosh.blog.51cto.com/6334375/1735536