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.

151 lines
3.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010-2018 OpenWrt.org
  3. # shellcheck disable=SC2034
  4. START=95
  5. # shellcheck disable=SC2034
  6. STOP=10
  7. NAME=mysqld
  8. LOGGER="/usr/bin/logger -p user.err -s -t $NAME --"
  9. [ -x "$LOGGER" ] || LOGGER="echo"
  10. MYSQLADMIN="/usr/bin/mysqladmin"
  11. MYSQLD="/usr/bin/$NAME"
  12. MYSQLDSAFE="/usr/bin/mysqld_safe"
  13. # mysqladmin likes to read /root/.my.cnf which could cause issues.
  14. export HOME="/etc/mysql"
  15. # Safeguard (relative paths, core dumps...)
  16. cd /
  17. mysqld_get_param() {
  18. $MYSQLD --print-defaults \
  19. | tr " " "\n" \
  20. | grep -- "--$1" \
  21. | tail -n 1 \
  22. | cut -d= -f2
  23. }
  24. # Checks if a server is running and accessible.
  25. #
  26. # check_alive insists on a pingable server
  27. # check_dead also fails if there is a lost mysqld in the process list
  28. #
  29. # Usage: boolean mysqld_status [check_alive|check_dead]
  30. mysqld_status() {
  31. if $MYSQLADMIN ping >/dev/null 2>&1; then
  32. ping_alive=1
  33. else
  34. ping_alive=0
  35. fi
  36. ps_alive=0
  37. pidfile="$(mysqld_get_param pid-file)"
  38. if [ -f "$pidfile" ] && kill -0 "$(cat "$pidfile")" >/dev/null 2>&1; then
  39. ps_alive=1
  40. fi
  41. if { [ "$1" = check_alive ] && [ $ping_alive = 1 ]; } || \
  42. { [ "$1" = check_dead ] && [ $ping_alive = 0 ] \
  43. && [ $ps_alive = 0 ]; }
  44. then
  45. return 0 # EXIT_SUCCESS
  46. else
  47. return 1 # EXIT_FAILURE
  48. fi
  49. }
  50. start() {
  51. conf=/etc/mysql/my.cnf
  52. logdir=/var/log/mysql
  53. rundir=/var/run/mysqld
  54. hint="please fix your server configuration in /etc/mysql/"
  55. for i in "$MYSQLD" "$MYSQLADMIN" "$MYSQLDSAFE"; do
  56. if [ ! -x "$i" ]; then
  57. $LOGGER "$i is missing"
  58. exit 1
  59. fi
  60. done
  61. if [ ! -r "$conf" ]; then
  62. $LOGGER "$conf cannot be read"
  63. exit 1
  64. fi
  65. config_load "$NAME"
  66. config_get_bool enabled general enabled 0
  67. # shellcheck disable=SC2154
  68. if [ "$enabled" -eq 0 ]; then
  69. $LOGGER "service not enabled in /etc/config/$NAME"
  70. exit 1
  71. fi
  72. config_get options general options
  73. datadir="$(mysqld_get_param datadir)"
  74. tmpdir="$(mysqld_get_param tmpdir)"
  75. if [ -z "$datadir" ]; then
  76. $LOGGER "datadir is not set"
  77. $LOGGER "$hint"
  78. exit 1
  79. fi
  80. if [ -z "$tmpdir" ]; then
  81. $LOGGER "tmpdir is not set"
  82. $LOGGER "$hint"
  83. exit 1
  84. fi
  85. if [ ! -f "$datadir/mysql/tables_priv.MAD" ]; then
  86. args="--force"
  87. basedir="$(mysqld_get_param basedir)"
  88. [ -n "$basedir" ] && args="$args --basedir=$basedir"
  89. $LOGGER "Cannot detect privileges table. You might need to run"
  90. $LOGGER "'mysql_install_db \"$args\"'"
  91. $LOGGER "to initialize the system tables."
  92. exit 1
  93. fi
  94. # Start daemon
  95. if mysqld_status check_alive; then
  96. $LOGGER "server is already running"
  97. else
  98. for i in "$logdir" "$rundir"; do
  99. opts="-m 0750"
  100. if ! [ -e "$i" ]; then
  101. # $rundir needs to be accessible for
  102. # clients
  103. if [ "$i" = "$rundir" ]; then
  104. opts=
  105. fi
  106. # shellcheck disable=SC2086
  107. mkdir -p $opts "$i"
  108. [ -d "$i" ] && chown mariadb:mariadb "$i"
  109. fi
  110. done
  111. # shellcheck disable=SC2154,SC2086
  112. "$MYSQLDSAFE" $options >/dev/null 2>&1 &
  113. fi
  114. }
  115. stop() {
  116. if ! mysqld_status check_dead; then
  117. "$MYSQLADMIN" shutdown
  118. fi
  119. }
  120. reload() {
  121. if mysqld_status check_alive; then
  122. "$MYSQLADMIN" reload
  123. else
  124. $LOGGER "server is not running"
  125. fi
  126. }