Как зайти в MySQL Server в Docker container?

375
27 июня 2022, 03:10

Собрал образ на основании вот этого Dockerfile:https://github.com/mysql/mysql-docker/tree/mysql-server/8.0

docker build . --tag mysql-image

запустил сбилденный mysql image

 docker run -it mysql-image /bin/bash

запустился. Попытался войти в mysql:

mysql -u root -p

запросил пароль - ввел "root" (другого не знаю) - выдал ошибку:

ERROR 2002 (HY000): Can't connect to local MySql serve through socket '/var/lib/mysql/mysql.sock'

Подскажите как зайти в mysql контейнер? Сам только начал изучать Docker. Сам Dockerfile:

FROM oraclelinux:7-slim
ARG MYSQL_SERVER_PACKAGE=mysql-community-server-minimal-8.0.22
ARG MYSQL_SHELL_PACKAGE=mysql-shell-8.0.22
# Install server
RUN yum install -y https://repo.mysql.com/mysql-community-minimal-release-el7.rpm \
      https://repo.mysql.com/mysql-community-release-el7.rpm \
  && yum-config-manager --enable mysql80-server-minimal \
  && yum install -y \
      $MYSQL_SERVER_PACKAGE \
      $MYSQL_SHELL_PACKAGE \
      libpwquality \
  && yum clean all \
  && mkdir /docker-entrypoint-initdb.d
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /healthcheck.sh
ENTRYPOINT ["/entrypoint.sh"]
HEALTHCHECK CMD /healthcheck.sh
EXPOSE 3306 33060 33061
CMD ["mysqld"]

Файл docker-entrypoint.sh:

set -e
echo "[Entrypoint] MySQL Docker Image 8.0.22-1.1.18"
# Fetch value from server config
# We use mysqld --verbose --help instead of my_print_defaults because the
# latter only show values present in config files, and not server defaults
_get_config() {
    local conf="$1"; shift
    "$@" --verbose --help 2>/dev/null | grep "^$conf" | awk '$1 == "'"$conf"'" { print $2; exit }'
}
# If command starts with an option, prepend mysqld
# This allows users to add command-line options without
# needing to specify the "mysqld" command
if [ "${1:0:1}" = '-' ]; then
    set -- mysqld "$@"
fi
if [ "$1" = 'mysqld' ]; then
    # Test that the server can start. We redirect stdout to /dev/null so
    # only the error messages are left.
    result=0
    output=$("$@" --validate-config) || result=$?
    if [ ! "$result" = "0" ]; then
        echo >&2 '[Entrypoint] ERROR: Unable to start MySQL. Please check your configuration.'
        echo >&2 "[Entrypoint] $output"
        exit 1
    fi
    # Get config
    DATADIR="$(_get_config 'datadir' "$@")"
    SOCKET="$(_get_config 'socket' "$@")"
    if [ -n "$MYSQL_LOG_CONSOLE" ] || [ -n "console" ]; then
        # Don't touch bind-mounted config files
        if ! cat /proc/1/mounts | grep "etc/my.cnf"; then
            sed -i 's/^log-error=/#&/' /etc/my.cnf
        fi
    fi
    if [ ! -d "$DATADIR/mysql" ]; then
        # If the password variable is a filename we use the contents of the file. We
        # read this first to make sure that a proper error is generated for empty files.
        if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
            MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
            if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
                echo >&2 '[Entrypoint] Empty MYSQL_ROOT_PASSWORD file specified.'
                exit 1
            fi
        fi
        if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
            echo >&2 '[Entrypoint] No password option specified for new database.'
            echo >&2 '[Entrypoint]   A random onetime password will be generated.'
            MYSQL_RANDOM_ROOT_PASSWORD=true
            MYSQL_ONETIME_PASSWORD=true
        fi
        mkdir -p "$DATADIR"
        chown -R mysql:mysql "$DATADIR"
        echo '[Entrypoint] Initializing database'
        "$@" --initialize-insecure
        echo '[Entrypoint] Database initialized'
        "$@" --daemonize --skip-networking --socket="$SOCKET"
        # To avoid using password on commandline, put it in a temporary file.
        # The file is only populated when and if the root password is set.
        PASSFILE=$(mktemp -u /var/lib/mysql-files/XXXXXXXXXX)
        install /dev/null -m0600 -omysql -gmysql "$PASSFILE"
        # Define the client command used throughout the script
        # "SET @@SESSION.SQL_LOG_BIN=0;" is required for products like group replication to work properly
        mysql=( mysql --defaults-extra-file="$PASSFILE" --protocol=socket -uroot -hlocalhost --socket="$SOCKET" --init-command="SET @@SESSION.SQL_LOG_BIN=0;")
        if [ ! -z "" ];
        then
            for i in {30..0}; do
                if mysqladmin --socket="$SOCKET" ping &>/dev/null; then
                    break
                fi
                echo '[Entrypoint] Waiting for server...'
                sleep 1
            done
            if [ "$i" = 0 ]; then
                echo >&2 '[Entrypoint] Timeout during MySQL init.'
                exit 1
            fi
        fi
        mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[@]}" mysql
        
        if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
            MYSQL_ROOT_PASSWORD="$(pwmake 128)"
            echo "[Entrypoint] GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
        fi
        if [ -z "$MYSQL_ROOT_HOST" ]; then
            ROOTCREATE="ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';"
        else
            ROOTCREATE="ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; \
            CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; \
            GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ; \
            GRANT PROXY ON ''@'' TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;"
        fi
        "${mysql[@]}" <<-EOSQL
            DELETE FROM mysql.user WHERE user NOT IN ('mysql.infoschema', 'mysql.session', 'mysql.sys', 'root') OR host NOT IN ('localhost');
            CREATE USER 'healthchecker'@'localhost' IDENTIFIED BY 'healthcheckpass';
            ${ROOTCREATE}
            FLUSH PRIVILEGES ;
        EOSQL
        if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
            # Put the password into the temporary config file
            cat >"$PASSFILE" <<EOF
[client]
password="${MYSQL_ROOT_PASSWORD}"
EOF
            #mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
        fi
        if [ "$MYSQL_DATABASE" ]; then
            echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
            mysql+=( "$MYSQL_DATABASE" )
        fi
        if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
            echo "CREATE USER '"$MYSQL_USER"'@'%' IDENTIFIED BY '"$MYSQL_PASSWORD"' ;" | "${mysql[@]}"
            if [ "$MYSQL_DATABASE" ]; then
                echo "GRANT ALL ON \`"$MYSQL_DATABASE"\`.* TO '"$MYSQL_USER"'@'%' ;" | "${mysql[@]}"
            fi
        elif [ "$MYSQL_USER" -a ! "$MYSQL_PASSWORD" -o ! "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
            echo '[Entrypoint] Not creating mysql user. MYSQL_USER and MYSQL_PASSWORD must be specified to create a mysql user.'
        fi
        echo
        for f in /docker-entrypoint-initdb.d/*; do
            case "$f" in
                *.sh)  echo "[Entrypoint] running $f"; . "$f" ;;
                *.sql) echo "[Entrypoint] running $f"; "${mysql[@]}" < "$f" && echo ;;
                *)     echo "[Entrypoint] ignoring $f" ;;
            esac
            echo
        done
        # When using a local socket, mysqladmin shutdown will only complete when the server is actually down
        mysqladmin --defaults-extra-file="$PASSFILE" shutdown -uroot --socket="$SOCKET"
        rm -f "$PASSFILE"
        unset PASSFILE
        echo "[Entrypoint] Server shut down"
        # This needs to be done outside the normal init, since mysqladmin shutdown will not work after
        if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
            if [ -z "yes" ]; then
                echo "[Entrypoint] User expiration is only supported in MySQL 5.6+"
            else
                echo "[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used."
                SQL=$(mktemp -u /var/lib/mysql-files/XXXXXXXXXX)
                install /dev/null -m0600 -omysql -gmysql "$SQL"
                if [ ! -z "$MYSQL_ROOT_HOST" ]; then
                    cat << EOF > "$SQL"
ALTER USER 'root'@'${MYSQL_ROOT_HOST}' PASSWORD EXPIRE;
ALTER USER 'root'@'localhost' PASSWORD EXPIRE;
EOF
                else
                    cat << EOF > "$SQL"
ALTER USER 'root'@'localhost' PASSWORD EXPIRE;
EOF
                fi
                set -- "$@" --init-file="$SQL"
                unset SQL
            fi
        fi
        echo
        echo '[Entrypoint] MySQL init process done. Ready for start up.'
        echo
    fi
    # Used by healthcheck to make sure it doesn't mistakenly report container
    # healthy during startup
    # Put the password into the temporary config file
    touch /healthcheck.cnf
    cat >"/healthcheck.cnf" <<EOF
[client]
user=healthchecker
socket=${SOCKET}
password=healthcheckpass
EOF
    touch /mysql-init-complete
    chown -R mysql:mysql "$DATADIR"
    echo "[Entrypoint] Starting MySQL 8.0.22-1.1.18"
fi
env MYSQLD_PARENT_PID=$$ "$@"

Файл healthcheck.sh:

if [ -f /mysql-init-complete ]; # The entrypoint script touches this file
then # Ping server to see if it is ready
  mysqladmin --defaults-extra-file=/healthcheck.cnf ping
else # Initialization still in progress
  exit 1
fi
Answer 1

У вас mysqld не запускается, передавая bash в команду запуска docker run -it mysql-image /bin/bash, вы тем самым говорите докеру не выполнять mysqld, а открыть bash. Попробуйте сначала запустить контейнер (без баша), посмотреть логи, если он работает, то тогда пытаться коннектиться. Также вместо того, чтобы собирать самим образ можно воспользоваться уже готовыми.

READ ALSO
Перегрузка операторов ввода/вывода при наследовании

Перегрузка операторов ввода/вывода при наследовании

Есть базовый класс Computer и производный класс Laptop:

257
Удаление элементов массива указателей на базовый класс

Удаление элементов массива указателей на базовый класс

У меня базовый класс с двумя виртуальными методами и два класса-наследникаВ функции main есть массив указателей на базовый класс, который может...

220
Расширение массива в cpp

Расширение массива в cpp

Возможно ли реализовать реализовать именно расширение памяти? Или хотя бы при помощи манипуляций с указателями избежать лишнего копирования...

296