企业案例:开发mysql多实例启动脚本:
实现:mysql多实例的start/stop/restart,并且由chkconfig管理
mysql多实例路径为:
[root@jason ~]# ls -ld /data/3306/
drwxr-xr-x 3 mysql mysql 4096 Oct 9 13:28 /data/3306/
1)已知mysql多实例启动命令为:
mysql_safe --default-file=/data/3306/my.cnf &
2)停止命令为:
mysqladmin -uroot -poldboy123 -S /data/3306/mysql.sock shutdown
注:其实启动脚本可以更简化,当然作者本人展示的是以谨慎的方式来编写。
写脚本思路:
定义一系列变量
fun_start(){
如果mysql.sock存在那么就提示mysql正在运行
否则 启动mysql进程,并且通过netstat命令查看进程号,通过返回值判断是否等于0,如果等于0,那么就提示启动成功,如果不等于0就提示失败
}
fun_stop(){
如果mysql.sock不存在那么就提示mysql未运行,然后退出
否则 关闭mysql进程,并且通过netstat命令查看进程号,通过返回值判断是否不等于0,如果不等于0,那么就提示停止成功,如果等于0就提示失败
}
fun_restart(){
调用fun_stop函数
休息2s
然后再调用fun_start函数
并且通过netstat命令查看进程号,通过返回值判断是否等于0,如果等于0,那么就提示启动成功,如果不等于0就提示失败
}
准备使用chkconfig启动和关闭顺序为13和20,查看是否被占用:
`[root@jason etc]# ls /etc/rc* |egrep "13|20"` #<==结果为空,那么可以使用13和20
MySQL多实例启动脚本展示:
[root@jason ~]# cat /data/3306/mysql
#!/bin/sh
#chkconfig:2345 13 20
#This is mysql start|stop|restart scripts.
#-------------------------------------
#Author:jason
#QQ:760966297
#mile:jasonminghao@163.com
#-------------------------------------
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
Mysql_User=root
Mysql_Password=oldboy123
Mysql_Port=3306
Mysql_Path=/data/${Mysql_Port}
Mysql_Sock=/data/${Mysql_Port}/mysql.sock
Cmd_Path=/application/mysql/bin
fun_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_Path/my.cnf 2>&1 >/dev/null & #Mysql start
sleep 2
netstat -lntup |grep 3306 >/dev/null #acheck mysql process
[ $? -eq 0 ]&&action "Mysql start successfully." /bin/true || action "Mysql startup failure." /bin/false
fi
}
fun_stop(){
if [ ! -e $Mysql_Sock ];then
action "MyySQL is not run." /bin/false
exit 2
else
${Cmd_Path}/mysqladmin -u${Mysql_User} -p${Mysql_Password} -S ${Mysql_Sock} shutdown
netstat -lntup |grep 3306 >/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 3306 >/dev/null
[ $? -eq 0 ]&& action "Mysql restart is successfully." /bin/true || action "Mysql restart is failure." /bin/false
exit 103
}
case $1 in
start)
fun_start
;;
stop)
fun_stop
;;
restart)
fun_restart
;;
*)
fun_usage
;;
esac
脚本测试:
[root@jason ~]# /data/3306/mysql start
Mysql start successfully. [ OK ]
[root@jason ~]# /data/3306/mysql stop
Mysql stop is successfully. [ OK ]
[root@jason ~]# /data/3306/mysql restart #<==这里因为是在代码中定义了进程不存在就会提示
MyySQL is not run. [FAILED]
[root@jason ~]# /data/3306/mysql start
Mysql start successfully. [ OK ]
[root@jason ~]# /data/3306/mysql restart
Mysql stop is successfully. [ OK ]
Mysql start successfully. [ OK ]
Mysql restart is successfully. [ OK ]
将mysql启动脚本加入开机自启动:
[root@jason ~]# cp /data/3306/mysql /etc/init.d/ #<==将mysql启动文件拷贝到/etc/init.d
[root@jason ~]# ls /etc/init.d/mysql
/etc/init.d/mysql
[root@jason ~]# chmod +x /etc/init.d/mysql #<==为mysql文件增加执行权限
[root@jason ~]# chkconfig --add mysql #<==添加到chkconfig管理
[root@jason ~]# chkconfig --list mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
转载于:https://blog.51cto.com/12643266/2298643