Home mysql How to go to Mysql Server in Docker Container?

How to go to Mysql Server in Docker Container?

Author

Date

Category

Collected an image based on this dockerfile: https: // github .com / mysql / mysql-docker / time / mysql-server / 8.0

docker build. --tag mysql-image

Running a mysql image

docker run -it mysql-image / bin / bash

started.
I tried to enter MySQL:

mysql -u root -p

requested the password – Introduced “Root” (I don’t know the other) – Issued an error:

Error 2002 (HY000): CAN't Connect to Local MySQL Serve Through Socket '/ Var / Lib / MySQL / mysql.sock '

Tell me how to go to MySQL container? He himself just began to study Docker.
DockerFile itself:

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-enel7.rpm \
   https://repo.mysql.com/mysql-community-release-el7.rpm \
 & amp; & amp; Yum-Config-Manager --enable Mysql80-Server-Minimal \
 & amp; & amp; yum install -y \
   $ Mysql_Server_Package \
   $ Mysql_shell_package \
   libpwquality \
 & amp; & amp; Yum Clean All \
 & amp; & amp; mkdir /dockeress-entrypoint-initdb.d.
VOLUME / VAR / LIB / MYSQL
Copy DockeredRypoint.sh / InntryPoint.sh.
Copy HealthCheck.sh /healthcheck.sh.
Entrypoint ["/ientRyPoint.sh"]
HealthCheck cmd /healthcheck.sh.
Expose 3306 33060 33061
Cmd ["mysqld"]

DockeredRyPoint.sh file:

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
# Latter Only Show Values ​​Present In Config Files, and Not Server Defaults
_Get_config () {
  LOCAL CONF = "$ 1"; Shift.
  "$ @" --Verbose --Help 2 & gt; / 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 Worthout
# 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 MESSAZ ARE LEFT.
  Result = 0.
  Output = $ ("$ @" --Validate-Config) || RESULT = $?
  IF [!! "$ result" = "0"]; Then.
    ECHO & GT; & AMP; 2 '[ENTRYPOINT] ERROR: Unable to start MySQL. Please Check Your Configuration. '
    ECHO & GT; & AMP; 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 = / # & amp; /' /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 emror files.
    if [-f "$ mysql_root_password"]; Then.
      Mysql_root_password = "$ (Cat $ mysql_root_password)"
      if [-z "$ mysql_root_password"]; Then.
        Echo & GT; & amp; 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 & GT; & AMP; 2 '[ENTRYPOINT] NO Password Option Specified for New Database.'
      Echo & GT; & Amp; 2 '[EntryPoint] A Random OneTime Password Will Be Generated.'
      MySQL_RANDOM_ROOT_PASSWORD = True.
      Mysql_oneetime_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 & amp; & gt; / dev / null; then
          break
        fi
        echo '[Entrypoint] Waiting for server ...'
        sleep 1
      done
      if ["$ i" = 0]; then
        echo & gt; & amp; 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 [@]}" & lt; & lt; -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 & gt; "$ PASSFILE" & lt; & lt; 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 [@]}" & lt; "$ f" & amp; & amp; 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 & LT; & LT; EOF & GT; "$ Sql"
Alter user 'root' @ '$ {mysql_root_host}' password expire;
Alter User 'root' @ 'Localhost' password expire;
EOF.
        ELSE.
          CAT & LT; & LT; EOF & GT; "$ 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 & GT; "/ HealthCheck.cnf" & lt; & lt; 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 file:

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, Authority 100%

Do you mysqld does not start by passing bash to start docker run -it mysql-image / bash you Speak Doker Do not perform MYSQLD , and open Bash . Try to start the container (without a Basha), see logs if it works, then try to connect. Also, instead of collecting the image itself, you can use it ready.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions