Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. #!/bin/bash
  2. # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. set -e
  17.  
  18. echo "[Entrypoint] MySQL Docker Image 5.7.20-1.1.2"
  19. # Fetch value from server config
  20. # We use mysqld --verbose --help instead of my_print_defaults because the
  21. # latter only show values present in config files, and not server defaults
  22. _get_config() {
  23. local conf="$1"; shift
  24. "$@" --verbose --help 2>/dev/null | grep "^$conf" | awk '$1 == "'"$conf"'" { print $2; exit }'
  25. }
  26.  
  27. # If command starts with an option, prepend mysqld
  28. # This allows users to add command-line options without
  29. # needing to specify the "mysqld" command
  30. if [ "${1:0:1}" = '-' ]; then
  31. set -- mysqld "$@"
  32. fi
  33.  
  34. if [ "$1" = 'mysqld' ]; then
  35. # Test that the server can start. We redirect stdout to /dev/null so
  36. # only the error messages are left.
  37. result=0
  38. output=$("$@" --verbose --help 2>&1 > /dev/null) || result=$?
  39. if [ ! "$result" = "0" ]; then
  40. echo >&2 '[Entrypoint] ERROR: Unable to start MySQL. Please check your configuration.'
  41. echo >&2 "[Entrypoint] $output"
  42. exit 1
  43. fi
  44.  
  45. # Get config
  46. DATADIR="$(_get_config 'datadir' "$@")"
  47. SOCKET="$(_get_config 'socket' "$@")"
  48.  
  49. if [ -n "$MYSQL_LOG_CONSOLE" ] || [ -n "" ]; then
  50. # Don't touch bind-mounted config files
  51. if ! cat /proc/1/mounts | grep "etc/my.cnf"; then
  52. sed -i 's/^log-error=/#&/' /etc/my.cnf
  53. fi
  54. fi
  55.  
  56. if [ ! -d "$DATADIR/mysql" ]; then
  57. # If the password variable is a filename we use the contents of the file. We
  58. # read this first to make sure that a proper error is generated for empty files.
  59. if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
  60. MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
  61. if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
  62. echo >&2 '[Entrypoint] Empty MYSQL_ROOT_PASSWORD file specified.'
  63. exit 1
  64. fi
  65. fi
  66. if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
  67. echo >&2 '[Entrypoint] No password option specified for new database.'
  68. echo >&2 '[Entrypoint] A random onetime password will be generated.'
  69. MYSQL_RANDOM_ROOT_PASSWORD=true
  70. MYSQL_ONETIME_PASSWORD=true
  71. fi
  72. mkdir -p "$DATADIR"
  73. chown -R mysql:mysql "$DATADIR"
  74.  
  75. echo '[Entrypoint] Initializing database'
  76. "$@" --initialize-insecure
  77. echo '[Entrypoint] Database initialized'
  78.  
  79. "$@" --daemonize --skip-networking --socket="$SOCKET"
  80.  
  81. # To avoid using password on commandline, put it in a temporary file.
  82. # The file is only populated when and if the root password is set.
  83. PASSFILE=$(mktemp -u /var/lib/mysql-files/XXXXXXXXXX)
  84. install /dev/null -m0600 -omysql -gmysql "$PASSFILE"
  85. # Define the client command used throughout the script
  86. # "SET @@SESSION.SQL_LOG_BIN=0;" is required for products like group replication to work properly
  87. mysql=( mysql --defaults-extra-file="$PASSFILE" --protocol=socket -uroot -hlocalhost --socket="$SOCKET" --init-command="SET @@SESSION.SQL_LOG_BIN=0;")
  88.  
  89. if [ ! -z "" ];
  90. then
  91. for i in {30..0}; do
  92. if mysqladmin --socket="$SOCKET" ping &>/dev/null; then
  93. break
  94. fi
  95. echo '[Entrypoint] Waiting for server...'
  96. sleep 1
  97. done
  98. if [ "$i" = 0 ]; then
  99. echo >&2 '[Entrypoint] Timeout during MySQL init.'
  100. exit 1
  101. fi
  102. fi
  103.  
  104. mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[@]}" mysql
  105.  
  106. if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
  107. MYSQL_ROOT_PASSWORD="$(pwmake 128)"
  108. echo "[Entrypoint] GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
  109. fi
  110. if [ -z "$MYSQL_ROOT_HOST" ]; then
  111. ROOTCREATE="ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';"
  112. else
  113. ROOTCREATE="ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; \
  114. CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; \
  115. GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ; \
  116. GRANT PROXY ON ''@'' TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;"
  117. fi
  118. "${mysql[@]}" <<-EOSQL
  119. DELETE FROM mysql.user WHERE user NOT IN ('mysql.session', 'mysql.sys', 'root') OR host NOT IN ('localhost');
  120. CREATE USER 'healthchecker'@'localhost' IDENTIFIED BY 'healthcheckpass';
  121. ${ROOTCREATE}
  122. FLUSH PRIVILEGES ;
  123. EOSQL
  124. if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
  125. # Put the password into the temporary config file
  126. cat >"$PASSFILE" <<EOF
  127. [client]
  128. password="${MYSQL_ROOT_PASSWORD}"
  129. EOF
  130. #mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
  131. fi
  132.  
  133. if [ "$MYSQL_DATABASE" ]; then
  134. echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
  135. mysql+=( "$MYSQL_DATABASE" )
  136. fi
  137.  
  138. if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
  139. echo "CREATE USER '"$MYSQL_USER"'@'%' IDENTIFIED BY '"$MYSQL_PASSWORD"' ;" | "${mysql[@]}"
  140.  
  141. if [ "$MYSQL_DATABASE" ]; then
  142. echo "GRANT ALL ON \`"$MYSQL_DATABASE"\`.* TO '"$MYSQL_USER"'@'%' ;" | "${mysql[@]}"
  143. fi
  144.  
  145. echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
  146. elif [ "$MYSQL_USER" -a ! "$MYSQL_PASSWORD" -o ! "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
  147. echo '[Entrypoint] Not creating mysql user. MYSQL_USER and MYSQL_PASSWORD must be specified to create a mysql user.'
  148. fi
  149. echo
  150. for f in /docker-entrypoint-initdb.d/*; do
  151. case "$f" in
  152. *.sh) echo "[Entrypoint] running $f"; . "$f" ;;
  153. *.sql) echo "[Entrypoint] running $f"; "${mysql[@]}" < "$f" && echo ;;
  154. *) echo "[Entrypoint] ignoring $f" ;;
  155. esac
  156. echo
  157. done
  158.  
  159. # When using a local socket, mysqladmin shutdown will only complete when the server is actually down
  160. mysqladmin --defaults-extra-file="$PASSFILE" shutdown -uroot --socket="$SOCKET"
  161. rm -f "$PASSFILE"
  162. unset PASSFILE
  163. echo "[Entrypoint] Server shut down"
  164.  
  165. # This needs to be done outside the normal init, since mysqladmin shutdown will not work after
  166. if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
  167. if [ -z "yes" ]; then
  168. echo "[Entrypoint] User expiration is only supported in MySQL 5.6+"
  169. else
  170. echo "[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used."
  171. SQL=$(mktemp -u /var/lib/mysql-files/XXXXXXXXXX)
  172. install /dev/null -m0600 -omysql -gmysql "$SQL"
  173. if [ ! -z "$MYSQL_ROOT_HOST" ]; then
  174. cat << EOF > "$SQL"
  175. ALTER USER 'root'@'${MYSQL_ROOT_HOST}' PASSWORD EXPIRE;
  176. ALTER USER 'root'@'localhost' PASSWORD EXPIRE;
  177. EOF
  178. else
  179. cat << EOF > "$SQL"
  180. ALTER USER 'root'@'localhost' PASSWORD EXPIRE;
  181. EOF
  182. fi
  183. set -- "$@" --init-file="$SQL"
  184. unset SQL
  185. fi
  186. fi
  187.  
  188. echo
  189. echo '[Entrypoint] MySQL init process done. Ready for start up.'
  190. echo
  191. fi
  192.  
  193. # Used by healthcheck to make sure it doesn't mistakenly report container
  194. # healthy during startup
  195. # Put the password into the temporary config file
  196. touch /healthcheck.cnf
  197. cat >"/healthcheck.cnf" <<EOF
  198. [client]
  199. user=healthchecker
  200. socket=${SOCKET}
  201. password=healthcheckpass
  202. EOF
  203. touch /mysql-init-complete
  204. chown -R mysql:mysql "$DATADIR"
  205. echo "[Entrypoint] Starting MySQL 5.7.20-1.1.2"
  206. fi
  207.  
  208. exec "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement