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.

430 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. chmod 755 $mysql_unix_port_dir
  192. fi
  193. # Use the mysqld-max binary by default if the user doesn't specify a binary
  194. if test -z "$MYSQLD"
  195. then
  196. if test -x $ledir/mysqld-max
  197. then
  198. MYSQLD=mysqld-max
  199. else
  200. MYSQLD=mysqld
  201. fi
  202. fi
  203. if test ! -x $ledir/$MYSQLD
  204. then
  205. echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  206. echo "Please do a cd to the mysql installation directory and restart"
  207. echo "this script from there as follows:"
  208. echo "./bin/mysqld_safe".
  209. echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
  210. echo "information"
  211. exit 1
  212. fi
  213. if test -z "$pid_file"
  214. then
  215. pid_file=$DATADIR/`@HOSTNAME@`.pid
  216. else
  217. case "$pid_file" in
  218. /* ) ;;
  219. * ) pid_file="$DATADIR/$pid_file" ;;
  220. esac
  221. fi
  222. test -z "$err_log" && err_log=$DATADIR/`@HOSTNAME@`.err
  223. if test -n "$mysql_unix_port"
  224. then
  225. args="--socket=$mysql_unix_port $args"
  226. fi
  227. if test -n "$mysql_tcp_port"
  228. then
  229. args="--port=$mysql_tcp_port $args"
  230. fi
  231. if test $niceness -eq 0
  232. then
  233. NOHUP_NICENESS="nohup"
  234. else
  235. NOHUP_NICENESS="nohup nice -$niceness"
  236. fi
  237. # Using nice with no args to get the niceness level is GNU-specific.
  238. # This check could be extended for other operating systems (e.g.,
  239. # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
  240. # But, it also seems that GNU nohup is the only one which messes
  241. # with the priority, so this is okay.
  242. if nohup nice > /dev/null 2>&1
  243. then
  244. normal_niceness=`nice`
  245. nohup_niceness=`nohup nice`
  246. numeric_nice_values=1
  247. for val in $normal_niceness $nohup_niceness
  248. do
  249. case "$val" in
  250. -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
  251. [0-9] | [0-9][0-9] | [0-9][0-9][0-9] )
  252. ;;
  253. * )
  254. numeric_nice_values=0 ;;
  255. esac
  256. done
  257. if test $numeric_nice_values -eq 1
  258. then
  259. nice_value_diff=`expr $nohup_niceness - $normal_niceness`
  260. if test $? -eq 0 && test $nice_value_diff -gt 0 && \
  261. nice --$nice_value_diff echo testing > /dev/null 2>&1
  262. then
  263. # nohup increases the priority (bad), and we are permitted
  264. # to lower the priority with respect to the value the user
  265. # might have been given
  266. niceness=`expr $niceness - $nice_value_diff`
  267. NOHUP_NICENESS="nice -$niceness nohup"
  268. fi
  269. fi
  270. else
  271. if nohup echo testing > /dev/null 2>&1
  272. then
  273. :
  274. else
  275. # nohup doesn't work on this system
  276. NOHUP_NICENESS=""
  277. fi
  278. fi
  279. USER_OPTION=""
  280. if test -w / -o "$USER" = "root"
  281. then
  282. if test "$user" != "root" -o $SET_USER = 1
  283. then
  284. USER_OPTION="--user=$user"
  285. fi
  286. # If we are root, change the err log to the right user.
  287. touch $err_log; chown $user $err_log
  288. if test -n "$open_files"
  289. then
  290. ulimit -n $open_files
  291. args="--open-files-limit=$open_files $args"
  292. fi
  293. fi
  294. # Try to set the core file size (even if we aren't root) because many systems
  295. # don't specify a hard limit on core file size.
  296. if test -n "$core_file_size"
  297. then
  298. ulimit -c $core_file_size
  299. fi
  300. #
  301. # If there exists an old pid file, check if the daemon is already running
  302. # Note: The switches to 'ps' may depend on your operating system
  303. if test -f $pid_file
  304. then
  305. PID=`cat $pid_file`
  306. if @CHECK_PID@
  307. then
  308. if @FIND_PROC@
  309. then # The pid contains a mysqld process
  310. echo "A mysqld process already exists"
  311. echo "A mysqld process already exists at " `date` >> $err_log
  312. exit 1
  313. fi
  314. fi
  315. rm -f $pid_file
  316. if test -f $pid_file
  317. then
  318. echo "Fatal error: Can't remove the pid file: $pid_file"
  319. echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  320. echo "Please remove it manually and start $0 again"
  321. echo "mysqld daemon not started"
  322. exit 1
  323. fi
  324. fi
  325. #
  326. # Uncomment the following lines if you want all tables to be automatically
  327. # checked and repaired during startup. You should add sensible key_buffer
  328. # and sort_buffer values to my.cnf to improve check performance or require
  329. # less disk space.
  330. # Alternatively, you can start mysqld with the "myisam-recover" option. See
  331. # the manual for details.
  332. #
  333. # echo "Checking tables in $DATADIR"
  334. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
  335. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
  336. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  337. # Does this work on all systems?
  338. #if type ulimit | grep "shell builtin" > /dev/null
  339. #then
  340. # ulimit -n 256 > /dev/null 2>&1 # Fix for BSD and FreeBSD systems
  341. #fi
  342. echo "`date +'%y%m%d %H:%M:%S mysqld started'`" >> $err_log
  343. while true
  344. do
  345. rm -f $safe_mysql_unix_port $pid_file # Some extra safety
  346. if test -z "$args"
  347. then
  348. $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file @MYSQLD_DEFAULT_SWITCHES@ >> $err_log 2>&1
  349. else
  350. 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"
  351. fi
  352. if test ! -f $pid_file # This is removed if normal shutdown
  353. then
  354. echo "STOPPING server from pid file $pid_file"
  355. break
  356. fi
  357. if @TARGET_LINUX@ && test $KILL_MYSQLD -eq 1
  358. then
  359. # Test if one process was hanging.
  360. # This is only a fix for Linux (running as base 3 mysqld processes)
  361. # but should work for the rest of the servers.
  362. # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  363. # kill -9 is used or the process won't react on the kill.
  364. numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
  365. echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  366. I=1
  367. while test "$I" -le "$numofproces"
  368. do
  369. PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'`
  370. for T in $PROC
  371. do
  372. break
  373. done
  374. # echo "TEST $I - $T **"
  375. if kill -9 $T
  376. then
  377. echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  378. else
  379. break
  380. fi
  381. I=`expr $I + 1`
  382. done
  383. fi
  384. echo "`date +'%y%m%d %H:%M:%S'` mysqld restarted" | tee -a $err_log
  385. done
  386. echo "`date +'%y%m%d %H:%M:%S'` mysqld ended" | tee -a $err_log
  387. echo "" | tee -a $err_log