entrypoint.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. set -e
  3. if [ ! -z "$DOCKER_START_DELAY" ] ; then
  4. sleep $DOCKER_START_DELAY
  5. fi
  6. # if command starts with an option, prepend mysqld
  7. if [ "${1:0:1}" = '-' ]; then
  8. set -- mysqld "$@"
  9. fi
  10. if [ "$1" = 'mysqld' ]; then
  11. # Get config
  12. DATADIR="$("$@" --verbose --help --log-bin-index=/tmp/tmp.index 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')"
  13. if [ ! -d "$DATADIR/mysql" ]; then
  14. if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
  15. echo >&2 'error: database is uninitialized and password option is not specified '
  16. echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
  17. exit 1
  18. fi
  19. # If the password variable is a filename we use the contents of the file
  20. if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
  21. MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
  22. fi
  23. mkdir -p "$DATADIR"
  24. chown -R mysql:mysql "$DATADIR"
  25. echo 'Initializing database'
  26. mysqld --initialize-insecure=on --user=mysql --datadir="$DATADIR"
  27. echo 'Database initialized'
  28. mysqld --user=mysql --datadir="$DATADIR" --skip-networking &
  29. pid="$!"
  30. mysql=( mysql --protocol=socket -uroot )
  31. for i in {30..0}; do
  32. if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
  33. break
  34. fi
  35. echo 'MySQL init process in progress...'
  36. sleep 1
  37. done
  38. if [ "$i" = 0 ]; then
  39. echo >&2 'MySQL init process failed.'
  40. exit 1
  41. fi
  42. mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[@]}" mysql
  43. if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
  44. MYSQL_ROOT_PASSWORD="$(pwmake 128)"
  45. echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
  46. fi
  47. "${mysql[@]}" <<-EOSQL
  48. -- What's done in this file shouldn't be replicated
  49. -- or products like mysql-fabric won't work
  50. SET @@SESSION.SQL_LOG_BIN=0;
  51. DELETE FROM mysql.user where user != 'mysql.sys';
  52. CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
  53. GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
  54. DROP DATABASE IF EXISTS test ;
  55. FLUSH PRIVILEGES ;
  56. EOSQL
  57. if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
  58. mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
  59. fi
  60. if [ "$MYSQL_DATABASE" ]; then
  61. echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
  62. mysql+=( "$MYSQL_DATABASE" )
  63. fi
  64. if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
  65. echo "CREATE USER '"$MYSQL_USER"'@'%' IDENTIFIED BY '"$MYSQL_PASSWORD"' ;" | "${mysql[@]}"
  66. if [ "$MYSQL_DATABASE" ]; then
  67. echo "GRANT ALL ON \`"$MYSQL_DATABASE"\`.* TO '"$MYSQL_USER"'@'%' ;" | "${mysql[@]}"
  68. fi
  69. echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
  70. fi
  71. echo
  72. for f in /docker-entrypoint-initdb.d/*; do
  73. case "$f" in
  74. *.sh) echo "$0: running $f"; . "$f" ;;
  75. *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f" && echo ;;
  76. *) echo "$0: ignoring $f" ;;
  77. esac
  78. echo
  79. done
  80. if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
  81. "${mysql[@]}" <<-EOSQL
  82. ALTER USER 'root'@'%' PASSWORD EXPIRE;
  83. EOSQL
  84. fi
  85. if ! kill -s TERM "$pid" || ! wait "$pid"; then
  86. echo >&2 'MySQL init process failed.'
  87. exit 1
  88. fi
  89. echo
  90. echo 'MySQL init process done. Ready for start up.'
  91. echo
  92. fi
  93. chown -R mysql:mysql "$DATADIR"
  94. fi
  95. exec "$@"