当前位置:Gxlcms > 数据库问题 > MySQL 多实例启动脚本

MySQL 多实例启动脚本

时间:2021-07-01 10:21:17 帮助过:14人阅读

企业案例:开发mysql多实例启动脚本:
mysql多实例路径为:

  1. <code>[root@jason ~]# ls -ld /data/3306/
  2. drwxr-xr-x 3 mysql mysql 4096 Oct 9 13:28 /data/3306/</code>

1)已知mysql多实例启动命令为:

  1. <code>mysql_safe --default-file=/data/3306/my.cnf &</code>

2)停止命令为:

  1. <code>mysqladmin -uroot -poldboy123 -S /data/3306/mysql.sock shutdown
  2. </code>

注:其实启动脚本可以更简化,当然作者本人展示的以谨慎的编写来展示脚本


MySQL多实例启动脚本展示:

  1. <code>
  2. [root@jason ~]# cat /data/3306/mysql
  3. #!/bin/sh
  4. #kconfig:2345 13 15
  5. #This is mysql start|stop|restart scripts.
  6. #-------------------------------------
  7. #Author:jason
  8. #QQ:760966297
  9. #mile:jasonminghao@163.com
  10. #-------------------------------------
  11. [ -f /etc/init.d/functions ]&& . /etc/init.d/functions
  12. Mysql_User=root
  13. Mysql_Password=oldboy123
  14. Mysql_Port=3306
  15. Mysql_Path=/data/${Mysql_Port}
  16. Mysql_Sock=/data/${Mysql_Port}/mysql.sock
  17. Cmd_Path=/application/mysql/bin
  18. fun_usage(){
  19. echo "USAGE $0:{start|stop|restart}"
  20. exit 1
  21. }
  22. fun_start(){
  23. if [ -e $Mysql_Sock ];then
  24. action "MySQL is running." /bin/false
  25. else
  26. /bin/sh ${Cmd_Path}/mysqld_safe --defaults-file=$Mysql_Path/my.cnf 2>&1 >/dev/null & #Mysql start
  27. sleep 2
  28. netstat -lntup |grep 3306 >/dev/null #acheck mysql process
  29. [ $? -eq 0 ]&&action "Mysql start successfully." /bin/true || action "Mysql startup failure." /bin/false
  30. fi
  31. }
  32. fun_stop(){
  33. if [ ! -e $Mysql_Sock ];then
  34. action "MyySQL is not run." /bin/false
  35. exit 2
  36. else
  37. ${Cmd_Path}/mysqladmin -u${Mysql_User} -p${Mysql_Password} -S ${Mysql_Sock} shutdown
  38. netstat -lntup |grep 3306 >/dev/null
  39. [ $? -ne 0 ]&& action "Mysql stop is successfully." /bin/true || action "Mysql stop is failure." /bin/false
  40. fi
  41. }
  42. fun_restart(){
  43. fun_stop
  44. sleep 2
  45. fun_start
  46. netstat -lntup |grep 3306 >/dev/null
  47. [ $? -eq 0 ]&& action "Mysql restart is successfully." /bin/true || action "Mysql restart is failure." /bin/false
  48. exit 103
  49. }
  50. case $1 in
  51. start)
  52. fun_start
  53. ;;
  54. stop)
  55. fun_stop
  56. ;;
  57. restart)
  58. fun_restart
  59. ;;
  60. *)
  61. fun_usage
  62. ;;
  63. esac
  64. </code>

脚本测试:

  1. <code>
  2. [root@jason ~]# /data/3306/mysql start
  3. Mysql start successfully. [ OK ]
  4. [root@jason ~]# /data/3306/mysql stop
  5. Mysql stop is successfully. [ OK ]
  6. [root@jason ~]# /data/3306/mysql restart #<==这里因为是在代码中定义了进程不存在就会提示
  7. MyySQL is not run. [FAILED]
  8. [root@jason ~]# /data/3306/mysql start
  9. Mysql start successfully. [ OK ]
  10. [root@jason ~]# /data/3306/mysql restart
  11. Mysql stop is successfully. [ OK ]
  12. Mysql start successfully. [ OK ]
  13. Mysql restart is successfully. [ OK ]
  14. </code>

MySQL 多实例启动脚本

标签:命令   shu   stop   ase   案例   art   scripts   roc   启动   

人气教程排行