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.

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