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.

146 lines
2.8 KiB

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