今天需要在服务器上启动多个MySQL实例

新加的MySQL实例不能用mysqld_multi 管理,所以就自定了个mysql_3307服务,用service管理



可以看看service的代码,都是些简单的shell脚本

service的原理就是去/etc/init.d下找对应的服务脚本,然后调用脚本,service的第二个参数是传给调用的服务作为第一个参数

因此可以自已在/etc/init.d下写一个脚本,脚本中定义相应的start,stop,status等或者也可以自己加些别的动作,在增加个执行权限就可以了



以下是我的mysql_3307定义脚本:

start(){
n=$(netstat -ntlp |grep 3307|grep -v "grep 3307"|wc -l)
if [ $n -ne 0 ];then
  echo "server is already running!"
  exit 0
fi
mysqld_multi --defaults-extra-file=/etc/my_3307.cnf --user=root start 3307
if [ $? -eq 0 ];then
 echo "service mysql_3307 start successful "
else
 echo "service mysql_3307 start fail"
fi
}

stop(){
n=$(netstat -ntlp |grep 3307|grep -v "grep 3307"|wc -l)
if [ $n -eq 0 ];then
  echo "server is not running!"
fi
mysqld_multi --defaults-extra-file=/etc/my_3307.cnf --user=root stop 3307
if [ $? -eq 0 ];then
 echo "service mysql_3307 stop successful "
else
 echo "service mysql_3307 stop fail"
fi
}

status(){
ps -ef |grep 3307|grep -v "grep 3307"
}

case $1 in
start)
 start
;;
stop)
 stop
;;
status)
 status
;;
*)
echo 'service accept arguments start|stop|status'
esac