user630702
user630702

Reputation: 3097

Docker MySQL - You must set environment variables but it is set already

I've set the ENV variables but still container throws error that I should set them.

This is the image I'm trying to use: mysql-56-centos7

Any suggestion on how to fix this?

Dockerfile:

FROM centos/mysql-56-centos7


ENV MYSQL_USER=root \
    MYSQL_PASSWORD=test \
    MYSQL_DATABASE=test \
    MYSQL_DATA_DIR=/var/lib/mysql \
    MYSQL_RUN_DIR=/run/mysqld \
    MYSQL_LOG_DIR=/var/log/mysql

EXPOSE 3306/tcp

CMD ["run-mysqld"]

Following is the error:

[root@centos1 test01]# docker container logs cf0245546d47
=> sourcing 20-validate-variables.sh ...
You must either specify the following environment variables:
  MYSQL_USER (regex: '^[a-zA-Z0-9_]+$')
  MYSQL_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$')
  MYSQL_DATABASE (regex: '^[a-zA-Z0-9_]+$')
Or the following environment variable:
  MYSQL_ROOT_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$')
Or both.
Optional Settings:
  MYSQL_LOWER_CASE_TABLE_NAMES (default: 0)
  MYSQL_LOG_QUERIES_ENABLED (default: 0)
  MYSQL_MAX_CONNECTIONS (default: 151)
  MYSQL_FT_MIN_WORD_LEN (default: 4)
  MYSQL_FT_MAX_WORD_LEN (default: 20)
  MYSQL_AIO (default: 1)
  MYSQL_KEY_BUFFER_SIZE (default: 32M or 10% of available memory)
  MYSQL_MAX_ALLOWED_PACKET (default: 200M)
  MYSQL_TABLE_OPEN_CACHE (default: 400)
  MYSQL_SORT_BUFFER_SIZE (default: 256K)
  MYSQL_READ_BUFFER_SIZE (default: 8M or 5% of available memory)
  MYSQL_INNODB_BUFFER_POOL_SIZE (default: 32M or 50% of available memory)
  MYSQL_INNODB_LOG_FILE_SIZE (default: 8M or 15% of available memory)
  MYSQL_INNODB_LOG_BUFFER_SIZE (default: 8M or 15% of available memory)

Upvotes: 3

Views: 4713

Answers (1)

atline
atline

Reputation: 31564

You could change MYSQL_USER=root to MYSQL_USER=user to fix the problem.

I'm not sure the root cause, but looks there is really some bug there for this image, see next /usr/share/container-scripts/mysql/pre-init/20-validate-variables.sh in the image, it looks it will have special handling for root account, but it will finally fail if you set root account.

If you want to use root, then just MYSQL_ROOT_PASSWORD need to be used, from its logic, then you need to delete MYSQL_USER, MYSQL_PASSWORD & MYSQL_DATABASE.

function usage() {
  [ $# == 1 ] && echo "error: $1"
  echo "You must either specify the following environment variables:"
  echo "  MYSQL_USER (regex: '$mysql_identifier_regex')"
  echo "  MYSQL_PASSWORD (regex: '$mysql_password_regex')"
  echo "  MYSQL_DATABASE (regex: '$mysql_identifier_regex')"
  echo "Or the following environment variable:"
  echo "  MYSQL_ROOT_PASSWORD (regex: '$mysql_password_regex')"
  echo "Or both."
  echo "Optional Settings:"
  echo "  MYSQL_LOWER_CASE_TABLE_NAMES (default: 0)"
  echo "  MYSQL_LOG_QUERIES_ENABLED (default: 0)"
  echo "  MYSQL_MAX_CONNECTIONS (default: 151)"
  echo "  MYSQL_FT_MIN_WORD_LEN (default: 4)"
  echo "  MYSQL_FT_MAX_WORD_LEN (default: 20)"
  echo "  MYSQL_AIO (default: 1)"
  echo "  MYSQL_KEY_BUFFER_SIZE (default: 32M or 10% of available memory)"
  echo "  MYSQL_MAX_ALLOWED_PACKET (default: 200M)"
  echo "  MYSQL_TABLE_OPEN_CACHE (default: 400)"
  echo "  MYSQL_SORT_BUFFER_SIZE (default: 256K)"
  echo "  MYSQL_READ_BUFFER_SIZE (default: 8M or 5% of available memory)"
  echo "  MYSQL_INNODB_BUFFER_POOL_SIZE (default: 32M or 50% of available memory)"
  echo "  MYSQL_INNODB_LOG_FILE_SIZE (default: 8M or 15% of available memory)"
  echo "  MYSQL_INNODB_LOG_BUFFER_SIZE (default: 8M or 15% of available memory)"
  echo
  echo "For more information, see https://github.com/sclorg/mysql-container"
  exit 1
}

function validate_variables() {
  # Check basic sanity of specified variables
  if [[ -v MYSQL_USER && -v MYSQL_PASSWORD ]]; then
    [[ "$MYSQL_USER"     =~ $mysql_identifier_regex ]] || usage "Invalid MySQL username"
    if [[ "$MYSQL_VERSION" < "5.7" ]] ; then
      [ ${#MYSQL_USER} -le 16 ] || usage "MySQL username too long (maximum 16 characters)"
    else
      [ ${#MYSQL_USER} -le 32 ] || usage "MySQL username too long (maximum 32 characters)"
    fi
    [[ "$MYSQL_PASSWORD" =~ $mysql_password_regex   ]] || usage "Invalid password"
    user_specified=1
  fi

  if [ -v MYSQL_ROOT_PASSWORD ]; then
    [[ "$MYSQL_ROOT_PASSWORD" =~ $mysql_password_regex ]] || usage "Invalid root password"
    root_specified=1
  fi

  # If MYSQL_USER == "root", we have a special case
  if [[ "${user_specified:-0}" == "1" && "$MYSQL_USER" == "root" ]]; then
    if [[ "${root_specified:-0}" == "1" ]]; then
      usage "When setting MYSQL_USER to 'root' you can only set either MYSQL_PASSWORD or MYSQL_ROOT_PASSWORD"
    fi
    # We will now behave as if MYSQL_USER was not specified
    export MYSQL_ROOT_PASSWORD="$MYSQL_PASSWORD"
    export -n MYSQL_USER
    export -n MYSQL_PASSWORD
    user_specified=0
    root_specified=1
  fi

  # Either combination of user/pass/db or root password is ok
  if [[ "${user_specified:-0}" == "0" && "${root_specified:-0}" == "0" ]]; then
    usage
  fi

  # If the root user is not specified, database name is required
  if [[ "${root_specified:-0}" == "0" ]]; then
    [ -v MYSQL_DATABASE ] || usage "You need to specify database name or root password"
  fi

  if [ -v MYSQL_DATABASE ]; then
    [[ "$MYSQL_DATABASE" =~ $mysql_identifier_regex ]] || usage "Invalid database name"
    [ ${#MYSQL_DATABASE} -le 64 ] || usage "Database name too long (maximum 64 characters)"
  fi

  # Specifically check of incomplete specification
  if [[ -v MYSQL_USER || -v MYSQL_PASSWORD || -v MYSQL_DATABASE ]] && \
     [[ "${user_specified:-0}" == "0" ]]; then
    usage
  fi
}

if ! [ -v MYSQL_RUNNING_AS_SLAVE ] ; then
  validate_variables
fi

Upvotes: 1

Related Questions