Guest User

Untitled

a guest
Apr 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. [root@da mysql]# cat /etc/rc.d/init.d/mysqld
  2. #!/bin/bash
  3. #
  4. # mysqld This shell script takes care of starting and stopping
  5. # the MySQL subsystem (mysqld).
  6. #
  7. # chkconfig: - 64 36
  8. # description: MySQL database server.
  9. # processname: mysqld
  10. # config: /etc/my.cnf
  11. # pidfile: /var/run/mysqld/mysqld.pid
  12.  
  13. # Source function library.
  14. . /etc/rc.d/init.d/functions
  15.  
  16. # Source networking configuration.
  17. . /etc/sysconfig/network
  18.  
  19.  
  20. prog="MySQL"
  21.  
  22. # extract value of a MySQL option from /etc/my.cnf
  23. # Usage: get_mysql_option FILE VARNAME DEFAULT
  24. # result is returned in $result
  25. # Ugly as this is, it knows nothing of option file sections ...
  26. get_mysql_option(){
  27. result=`sed -n "s/^[ \t]*$2[ \t]*=[ \t]*//p" "$1" 2>/dev/null | tail -n 1`
  28. if [ -z "$result" ]; then
  29. # not found, use default
  30. result="$3"
  31. else
  32. # found, still have to deal with quoting and end-of-line comments
  33. dequoted=`echo "$result" | sed "s/^'\([^']*\)'.*$/\1/"`
  34. if [ x"$dequoted" != x"$result" ]; then
  35. result="$dequoted"
  36. else
  37. dequoted=`echo "$result" | sed 's/^"\([^"]*\)".*$/\1/'`
  38. if [ x"$dequoted" != x"$result" ]; then
  39. result="$dequoted"
  40. else
  41. result=`echo "$result" | sed 's/^\([^ \t#]*\).*$/\1/'`
  42. fi
  43. fi
  44. fi
  45. }
  46.  
  47. get_mysql_option /etc/my.cnf datadir "/var/lib/mysql"
  48. datadir="$result"
  49. get_mysql_option /etc/my.cnf socket "$datadir/mysql.sock"
  50. socketfile="$result"
  51. get_mysql_option /etc/my.cnf err-log "/var/log/mysqld.log"
  52. errlogfile="$result"
  53. get_mysql_option /etc/my.cnf pid-file "/var/run/mysqld/mysqld.pid"
  54. mypidfile="$result"
  55.  
  56. start(){
  57. touch "$errlogfile"
  58. chown mysql:mysql "$errlogfile"
  59. chmod 0640 "$errlogfile"
  60. [ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
  61. if [ ! -d "$datadir/mysql" ] ; then
  62. action $"Initializing MySQL database: " /usr/bin/mysql_install_db
  63. ret=$?
  64. chown -R mysql:mysql "$datadir"
  65. if [ $ret -ne 0 ] ; then
  66. return $ret
  67. fi
  68. fi
  69. chown -R mysql:mysql "$datadir"
  70. chmod 0755 "$datadir"
  71. # The reason for explicitly specifying --pid-file is that there may
  72. # be no such entry in my.cnf, and the default behavior will be to not
  73. # create it at all...
  74. /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf --pid-file="$mypidfile" >/dev/null 2>&1 &
  75. ret=$?
  76. # Spin for a maximum of N seconds waiting for the server to come up.
  77. # Rather than assuming we know a valid username, accept an "access
  78. # denied" response as meaning the server is functioning.
  79. if [ $ret -eq 0 ]; then
  80. STARTTIMEOUT=30
  81. while [ $STARTTIMEOUT -gt 0 ]; do
  82. RESPONSE=`/usr/bin/mysqladmin -uUNKNOWN_MYSQL_USER ping 2>&1` && break
  83. echo "$RESPONSE" | grep -q "Access denied for user" && break
  84. sleep 1
  85. let STARTTIMEOUT=${STARTTIMEOUT}-1
  86. done
  87. if [ $STARTTIMEOUT -eq 0 ]; then
  88. echo "Timeout error occurred trying to start MySQL Daemon."
  89. action $"Starting $prog: " /bin/false
  90. else
  91. action $"Starting $prog: " /bin/true
  92. fi
  93. else
  94. action $"Starting $prog: " /bin/false
  95. fi
  96. [ $ret -eq 0 ] && touch /var/lock/subsys/mysqld
  97. return $ret
  98. }
  99.  
  100. stop(){
  101. MYSQLPID=`cat "$mypidfile" 2>/dev/null `
  102. if [ -n "$MYSQLPID" ]; then
  103. /bin/kill "$MYSQLPID" >/dev/null 2>&1
  104. ret=$?
  105. if [ $ret -eq 0 ]; then
  106. STOPTIMEOUT=60
  107. while [ $STOPTIMEOUT -gt 0 ]; do
  108. /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break
  109. sleep 1
  110. let STOPTIMEOUT=${STOPTIMEOUT}-1
  111. done
  112. if [ $STOPTIMEOUT -eq 0 ]; then
  113. echo "Timeout error occurred trying to stop MySQL Daemon."
  114. ret=1
  115. action $"Stopping $prog: " /bin/false
  116. else
  117. rm -f /var/lock/subsys/mysqld
  118. rm -f "$socketfile"
  119. action $"Stopping $prog: " /bin/true
  120. fi
  121. else
  122. action $"Stopping $prog: " /bin/false
  123. fi
  124. else
  125. ret=1
  126. action $"Stopping $prog: " /bin/false
  127. fi
  128. return $ret
  129. }
  130.  
  131. restart(){
  132. stop
  133. start
  134. }
  135.  
  136. condrestart(){
  137. [ -e /var/lock/subsys/mysqld ] && restart || :
  138. }
  139.  
  140. # See how we were called.
  141. case "$1" in
  142. start)
  143. start
  144. ;;
  145. stop)
  146. stop
  147. ;;
  148. status)
  149. status mysqld
  150. ;;
  151. restart)
  152. restart
  153. ;;
  154. condrestart)
  155. condrestart
  156. ;;
  157. *)
  158. echo $"Usage: $0 {start|stop|status|condrestart|restart}"
  159. exit 1
  160. esac
  161.  
  162. exit $?
Add Comment
Please, Sign In to add comment