MySQL 从主机执行mysql命令到运行mysql服务器的容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28389458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:19:41  来源:igfitidea点击:

Execute mysql command from the host to container running mysql server

mysqldockerlinux-containers

提问by Mazzy

I have followed the instruction https://registry.hub.docker.com/_/mysql/to pull an image and running a container in which it runs a mysql server. The container is running in the background and I would run some commands. Which is the best way to connect to the container and execute this command from command line?

我已按照https://registry.hub.docker.com/_/mysql/ 的说明拉取映像并运行一个容器,该容器在其中运行 mysql 服务器。容器在后台运行,我会运行一些命令。哪个是连接到容器并从命令行执行此命令的最佳方式?

回答by Abdullah Jibaly

You can connect to your mysql container and run your commands using:

您可以连接到您的 mysql 容器并使用以下命令运行您的命令:

docker exec -it mysql bash -l

(Where mysqlis the name you gave the container)

mysql你给容器起的名字在哪里)

Keep in mind that anything you do will not persist to the next time your run a container from the same image.

请记住,您所做的任何事情都不会持续到下次从同一映像运行容器时。

回答by Laurent Picquet

docker exec -i some_mysql_container mysql -uroot -ppassword  <<< "select database();"

回答by Nolwennig

To connect to the MySQL database using MySQL command line client.

使用 MySQL 命令行客户端连接到 MySQL 数据库。

  1. I connect to the bash into the running MySQL container:

    $ docker exec -t -icontainer_mysql_name/bin/bash

    -iis the shortcut for --interactiveoption. This options is used for keep STDIN open even if not attached

    -tis the shortcut for --ttyoption, used to allocate a pseudo-TTY

  2. I run MySQL client from bash MySQL container:

    $ mysql -uroot -proot

    -uis shortcut for --user=nameoption, used to define user for login if not current user.

    -pis shortcut for -password[=name]option, used to define password to use when connecting to server. If password is not given it's asked from the tty.

  3. Disco!

  1. 我将 bash 连接到正在运行的 MySQL 容器中:

    $ docker exec -t -icontainer_mysql_name/bin/bash

    -i--interactive选项的快捷方式。此选项用于保持 STDIN 打开,即使未附加

    -t--tty选项的快捷方式,用于分配一个伪TTY

  2. 我从 bash MySQL 容器运行 MySQL 客户端:

    $ mysql -uroot -proot

    -u--user=name选项的快捷方式,用于定义用户登录,如果不是当前用户。

    -p-password[=name]选项的快捷方式,用于定义连接服务器时使用的密码。如果没有给出密码,它会从 tty 中询问。

  3. 迪斯科

回答by Andreas Volkmann

In my case the <<<solution did not work.

在我的情况下,<<<解决方案不起作用。

Instead I used -e.

相反,我使用了-e.

Example:

例子:

docker exec ${CONTAINER_NAME} mysql -u ${USER_NAME} -p${PASSWORD} -e "drop schema test; create schema test;"

docker exec ${CONTAINER_NAME} mysql -u ${USER_NAME} -p${PASSWORD} -e "drop schema test; create schema test;"

回答by Jerry Chong

For @Abdullah Jibalysolution, after tested in MySQL 5.7, it would only entered into bash terminal prompt, whereby you still need to enter mysql command second time.

对于@Abdullah Jibaly解决方案,在MySQL 5.7 中测试后,它只会进入 bash 终端提示符,您仍然需要第二次输入 mysql 命令。

In order to directly enter into MySQL command line client after run MySQL container with one line of command, just run the following:

为了在一行命令运行MySQL容器后直接进入MySQL命令行客户端,只需运行以下命令:

docker exec -it container_mysql_name mysql -u username -p

回答by Gustaf Naeser

I use the following to create a command that will sort out at least a couple of cases with databases outside or inside the container (with -hand -P) and supporting -e:

我使用以下命令来创建一个命令,该命令将至少整理出几个包含容器外部或内部数据库(带有-h-P)并支持的情况-e

cat > ~/bin/mysql <<'EOF'
#/bin/bash

MARGS=()
MPORT="3306"

while test $# != 0; do
  if [[  == -h ]]; then MHOST=; shift;
  elif [[  == -h* ]]; then MHOST=${1#"-h"};
  elif [[  == -e ]]; then MEXEC=; shift;
  elif [[  == -e* ]]; then MEXEC=${1#"-e"};
  elif [[  == --execute=* ]]; then MEXEC=${1#"--execute="};
  elif [[  == -P ]]; then MPORT=; shift;
  elif [[  == -P* ]]; then MPORT=${1#"-P"};
  else MARGS="$MARGS "
  fi

  shift;
done

if [ -z  "${MHOST+x}" ]; then
   MHOST=localhost
fi

if [ $(docker inspect --format '{{ .State.Status }}' mysql) == "running" ]; then
 if [ ! -z "${MHOST+x}" ]; then
    if [ "$MHOST" == "localhost" -o "$MHOST" == "127.0.0.1" ]; then
      CPORT=$(docker port mysql 3306/tcp)
      if [ ${CPORT#"0.0.0.0:"} == $MPORT ]; then
        #echo "aiming for container port ($MPORT -> $CPORT)";
        MHOST=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql);
      else
        MHOST=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*//p' | head -1);
      fi
    fi
  fi
fi

if [ -z "$MEXEC" ]; then
   docker run --link mysql:mysql -i --rm mysql mysql "-h" $MHOST "-P" $MPORT $MARGS
else
   docker run --link mysql:mysql -i --rm mysql mysql "-h" $MHOST "-P" $MPORT $MARGS <<< $MEXEC
fi
EOF
chmod +x ~/bin/mysql

回答by ivor

i didn't find any of these solutions to be effective for my use case: needing to store the returned data from the SQL to a bash variable.

我没有发现这些解决方案中的任何一个对我的用例有效:需要将从 SQL 返回的数据存储到 bash 变量。

i ended up with the following syntax when making the call from inside a bash script running on the host computer (outside the docker mysql server), basically use 'echo' to forward the SQL statement to stdin on the docker exec command.

当从主机上运行的 bash 脚本内部(在 docker mysql 服务器外部)进行调用时,我最终得到了以下语法,基本上使用“echo”将 SQL 语句转发到 docker exec 命令上的 stdin。

modify the following to specify the mysql container name and proper mysql user and password for your use case:

修改以下内容,为您的用例指定 mysql 容器名称和正确的 mysql 用户和密码:

#!/bin/bash
mysqlCMD="docker exec -i _mysql-container-name_ mysql -uroot -proot "
sqlCMD="select count(*) from DBnames where name = 'sampleDB'"
count=`echo $sqlCMD | $mysqlCMD | grep -v count`

# count variable now contains the result of the SQL statement

for whatever reason, when i used the -e option, and then provided that string within the back-quotes, the interpreter modified the quotation marks resulting in SQL syntax failure.

无论出于何种原因,当我使用 -e 选项,然后在反引号中提供该字符串时,解释器修改了引号,导致 SQL 语法失败。

richard

理查德

回答by William Pispico

Its possible with docker run, start a new container just to execute your mysql statement. This approach helped me to workaround the access deniedproblem when you try to run a statement with docker exec using localhost to connect to mysql

它可能与docker run,启动一个新容器只是为了执行你的 mysql 语句。当您尝试使用 docker exec 使用 localhost 连接到 mysql 时,这种方法帮助我解决了访问被拒绝的问题

$ docker run -it --rm mysql mysql -h172.17.0.2 -uroot -pmy-secret-pw -e "show databases;"