You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
13 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing mysqld_safe
  12. KILL_MYSQLD=1;
  13. MYSQLD=
  14. trap '' 1 2 3 15 # we shouldn't let anyone kill us
  15. umask 007
  16. defaults=
  17. case "$1" in
  18. --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  19. defaults="$1"; shift
  20. ;;
  21. esac
  22. usage () {
  23. cat <<EOF
  24. Usage: $0 [OPTIONS]
  25. --no-defaults Don't read the system defaults file
  26. --defaults-file=FILE Use the specified defaults file
  27. --defaults-extra-file=FILE Also use defaults from the specified file
  28. --ledir=DIRECTORY Look for mysqld in the specified directory
  29. --log-error=FILE Log errors to the specified log file
  30. --open-files-limit=LIMIT Limit the number of open files
  31. --core-file-size=LIMIT Limit core files to the specified size
  32. --timezone=TZ Set the system timezone
  33. --mysqld=FILE Use the specified file as mysqld
  34. --mysqld-version=VERSION Use "mysqld-VERSION" as mysqld
  35. --nice=NICE Set the scheduling priority of mysqld
  36. --skip-kill-mysqld Don't try to kill stray mysqld processes
  37. All other options are passed to the mysqld program.
  38. EOF
  39. exit 1
  40. }
  41. parse_arguments() {
  42. # We only need to pass arguments through to the server if we don't
  43. # handle them here. So, we collect unrecognized options (passed on
  44. # the command line) into the args variable.
  45. pick_args=
  46. if test "$1" = PICK-ARGS-FROM-ARGV
  47. then
  48. pick_args=1
  49. shift
  50. fi
  51. for arg do
  52. case "$arg" in
  53. --skip-kill-mysqld*)
  54. KILL_MYSQLD=0;
  55. ;;
  56. # these get passed explicitly to mysqld
  57. --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
  58. --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
  59. --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
  60. --user=*) user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
  61. # these two might have been set in a [mysqld_safe] section of my.cnf
  62. # they are added to mysqld command line to override settings from my.cnf
  63. --socket=*) mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  64. --port=*) mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
  65. # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
  66. --ledir=*) ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
  67. --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
  68. --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
  69. --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
  70. --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
  71. --mysqld=*) MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
  72. --mysqld-version=*)
  73. tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
  74. if test -n "$tmp"
  75. then
  76. MYSQLD="mysqld-$tmp"
  77. else
  78. MYSQLD="mysqld"
  79. fi
  80. ;;
  81. --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
  82. --help)
  83. usage
  84. ;;
  85. *)
  86. if test -n "$pick_args"
  87. then
  88. # This sed command makes sure that any special chars are quoted,
  89. # so the arg gets passed exactly to the server.
  90. args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  91. fi
  92. ;;
  93. esac
  94. done
  95. }
  96. #
  97. # First, try to find BASEDIR and ledir (where mysqld is)
  98. #
  99. MY_PWD=`pwd`
  100. # Check for the directories we would expect from a binary release install
  101. if test -f ./share/mysql/english/errmsg.sys -a -x ./bin/mysqld
  102. then
  103. MY_BASEDIR_VERSION=$MY_PWD # Where bin, share and data are
  104. ledir=$MY_BASEDIR_VERSION/bin # Where mysqld is
  105. # Check for the directories we would expect from a source install
  106. elif test -f ./share/mysql/english/errmsg.sys -a \
  107. -x ./libexec/mysqld
  108. then
  109. MY_BASEDIR_VERSION=$MY_PWD # Where libexec, share and var are
  110. ledir=$MY_BASEDIR_VERSION/libexec # Where mysqld is
  111. # Since we didn't find anything, used the compiled-in defaults
  112. else
  113. MY_BASEDIR_VERSION=@prefix@
  114. ledir=@libexecdir@
  115. fi
  116. #
  117. # Second, try to find the data directory
  118. #
  119. # Try where the binary installs put it
  120. if test -d $MY_BASEDIR_VERSION/data/mysql
  121. then
  122. DATADIR=$MY_BASEDIR_VERSION/data
  123. if test -z "$defaults" -a -r "$DATADIR/my.cnf"
  124. then
  125. defaults="--defaults-extra-file=$DATADIR/my.cnf"
  126. fi
  127. # Next try where the source installs put it
  128. elif test -d $MY_BASEDIR_VERSION/var/mysql
  129. then
  130. DATADIR=$MY_BASEDIR_VERSION/var
  131. # Or just give up and use our compiled-in default
  132. else
  133. DATADIR=@localstatedir@
  134. fi
  135. if test -z "$MYSQL_HOME"
  136. then
  137. if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
  138. then
  139. echo "WARNING: Found two instances of my.cnf -"
  140. echo "$MY_BASEDIR_VERSION/my.cnf and"
  141. echo "$DATADIR/my.cnf"
  142. echo "IGNORING $DATADIR/my.cnf"
  143. echo
  144. MYSQL_HOME=$MY_BASEDIR_VERSION
  145. elif test -r "$DATADIR/my.cnf"
  146. then
  147. echo "WARNING: Found $DATADIR/my.cnf"
  148. echo "Datadir is deprecated place for my.cnf, please move it to $MY_BASEDIR_VERSION"
  149. echo
  150. MYSQL_HOME=$DATADIR
  151. else
  152. MYSQL_HOME=$MY_BASEDIR_VERSION
  153. fi
  154. fi
  155. export MYSQL_HOME
  156. user=@MYSQLD_USER@
  157. niceness=0
  158. # these rely on $DATADIR by default, so we'll set them later on
  159. pid_file=
  160. err_log=
  161. # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
  162. # and then merge with the command line arguments
  163. if test -x ./bin/my_print_defaults
  164. then
  165. print_defaults="./bin/my_print_defaults"
  166. elif test -x @bindir@/my_print_defaults
  167. then
  168. print_defaults="@bindir@/my_print_defaults"
  169. elif test -x @bindir@/mysql_print_defaults
  170. then
  171. print_defaults="@bindir@/mysql_print_defaults"
  172. else
  173. print_defaults="my_print_defaults"
  174. fi
  175. args=
  176. SET_USER=2
  177. parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
  178. if test $SET_USER -eq 2
  179. then
  180. SET_USER=0
  181. fi
  182. parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
  183. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  184. safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-@MYSQL_UNIX_ADDR@}}
  185. # Make sure that directory for $safe_mysql_unix_port exists
  186. mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
  187. if [ ! -d $mysql_unix_port_dir ]
  188. then
  189. mkdir $mysql_unix_port_dir
  190. chown $user $mysql_unix_port_dir
  191. fi
  192. # Use the mysqld-max binary by default if the user doesn't specify a binary
  193. if test -z "$MYSQLD"
  194. then
  195. if test -x $ledir/mysqld-max
  196. then
  197. MYSQLD=mysqld-max
  198. else
  199. MYSQLD=mysqld
  200. fi
  201. fi
  202. if test ! -x $ledir/$MYSQLD
  203. then
  204. echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  205. echo "Please do a cd to the mysql installation directory and restart"
  206. echo "this script from there as follows:"
  207. echo "./bin/mysqld_safe".
  208. echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
  209. echo "information"
  210. exit 1
  211. fi
  212. if test -z "$pid_file"
  213. then
  214. pid_file=$DATADIR/`@HOSTNAME@`.pid
  215. else
  216. case "$pid_file" in
  217. /* ) ;;
  218. * ) pid_file="$DATADIR/$pid_file" ;;
  219. esac
  220. fi
  221. test -z "$err_log" && err_log=$DATADIR/`@HOSTNAME@`.err
  222. if test -n "$mysql_unix_port"
  223. then
  224. args="--socket=$mysql_unix_port $args"
  225. fi
  226. if test -n "$mysql_tcp_port"
  227. then
  228. args="--port=$mysql_tcp_port $args"
  229. fi
  230. if test $niceness -eq 0
  231. then
  232. NOHUP_NICENESS="nohup"
  233. else
  234. NOHUP_NICENESS="nohup nice -$niceness"
  235. fi
  236. # Using nice with no args to get the niceness level is GNU-specific.
  237. # This check could be extended for other operating systems (e.g.,
  238. # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
  239. # But, it also seems that GNU nohup is the only one which messes
  240. # with the priority, so this is okay.
  241. if nohup nice > /dev/null 2>&1
  242. then
  243. normal_niceness=`nice`
  244. nohup_niceness=`nohup nice`
  245. numeric_nice_values=1
  246. for val in $normal_niceness $nohup_niceness
  247. do
  248. case "$val" in
  249. -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
  250. [0-9] | [0-9][0-9] | [0-9][0-9][0-9] )
  251. ;;
  252. * )
  253. numeric_nice_values=0 ;;
  254. esac
  255. done
  256. if test $numeric_nice_values -eq 1
  257. then
  258. nice_value_diff=`expr $nohup_niceness - $normal_niceness`
  259. if test $? -eq 0 && test $nice_value_diff -gt 0 && \
  260. nice --$nice_value_diff echo testing > /dev/null 2>&1
  261. then
  262. # nohup increases the priority (bad), and we are permitted
  263. # to lower the priority with respect to the value the user
  264. # might have been given
  265. niceness=`expr $niceness - $nice_value_diff`
  266. NOHUP_NICENESS="nice -$niceness nohup"
  267. fi
  268. fi
  269. else
  270. if nohup echo testing > /dev/null 2>&1
  271. then
  272. :
  273. else
  274. # nohup doesn't work on this system
  275. NOHUP_NICENESS=""
  276. fi
  277. fi
  278. USER_OPTION=""
  279. if test -w / -o "$USER" = "root"
  280. then
  281. if test "$user" != "root" -o $SET_USER = 1
  282. then
  283. USER_OPTION="--user=$user"
  284. fi
  285. # If we are root, change the err log to the right user.
  286. touch $err_log; chown $user $err_log
  287. if test -n "$open_files"
  288. then
  289. ulimit -n $open_files
  290. args="--open-files-limit=$open_files $args"
  291. fi
  292. fi
  293. # Try to set the core file size (even if we aren't root) because many systems
  294. # don't specify a hard limit on core file size.
  295. if test -n "$core_file_size"
  296. then
  297. ulimit -c $core_file_size
  298. fi
  299. #
  300. # If there exists an old pid file, check if the daemon is already running
  301. # Note: The switches to 'ps' may depend on your operating system
  302. if test -f $pid_file
  303. then
  304. PID=`cat $pid_file`
  305. if @CHECK_PID@
  306. then
  307. if @FIND_PROC@
  308. then # The pid contains a mysqld process
  309. echo "A mysqld process already exists"
  310. echo "A mysqld process already exists at " `date` >> $err_log
  311. exit 1
  312. fi
  313. fi
  314. rm -f $pid_file
  315. if test -f $pid_file
  316. then
  317. echo "Fatal error: Can't remove the pid file: $pid_file"
  318. echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  319. echo "Please remove it manually and start $0 again"
  320. echo "mysqld daemon not started"
  321. exit 1
  322. fi
  323. fi
  324. #
  325. # Uncomment the following lines if you want all tables to be automatically
  326. # checked and repaired during startup. You should add sensible key_buffer
  327. # and sort_buffer values to my.cnf to improve check performance or require
  328. # less disk space.
  329. # Alternatively, you can start mysqld with the "myisam-recover" option. See
  330. # the manual for details.
  331. #
  332. # echo "Checking tables in $DATADIR"
  333. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
  334. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
  335. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  336. # Does this work on all systems?
  337. #if type ulimit | grep "shell builtin" > /dev/null
  338. #then
  339. # ulimit -n 256 > /dev/null 2>&1 # Fix for BSD and FreeBSD systems
  340. #fi
  341. echo "`date +'%y%m%d %H:%M:%S mysqld started'`" >> $err_log
  342. while true
  343. do
  344. rm -f $safe_mysql_unix_port $pid_file # Some extra safety
  345. if test -z "$args"
  346. then
  347. $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file @MYSQLD_DEFAULT_SWITCHES@ >> $err_log 2>&1
  348. else
  349. eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file @MYSQLD_DEFAULT_SWITCHES@ $args >> $err_log 2>&1"
  350. fi
  351. if test ! -f $pid_file # This is removed if normal shutdown
  352. then
  353. echo "STOPPING server from pid file $pid_file"
  354. break
  355. fi
  356. if @TARGET_LINUX@ && test $KILL_MYSQLD -eq 1
  357. then
  358. # Test if one process was hanging.
  359. # This is only a fix for Linux (running as base 3 mysqld processes)
  360. # but should work for the rest of the servers.
  361. # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  362. # kill -9 is used or the process won't react on the kill.
  363. numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
  364. echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  365. I=1
  366. while test "$I" -le "$numofproces"
  367. do
  368. PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'`
  369. for T in $PROC
  370. do
  371. break
  372. done
  373. # echo "TEST $I - $T **"
  374. if kill -9 $T
  375. then
  376. echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  377. else
  378. break
  379. fi
  380. I=`expr $I + 1`
  381. done
  382. fi
  383. echo "`date +'%y%m%d %H:%M:%S'` mysqld restarted" | tee -a $err_log
  384. done
  385. echo "`date +'%y%m%d %H:%M:%S'` mysqld ended" | tee -a $err_log
  386. echo "" | tee -a $err_log