How to execute MySQL command from the host to container running MySQL server?

﹥>﹥吖頭↗ 提交于 2021-02-05 12:46:15

问题


I have followed the instruction in 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 like to run some commands.

Which is the best way to connect to the container and execute this command from command line?

Thanks.


回答1:


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

docker exec -it mysql bash -l

(Where mysql is the name you gave the container)

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




回答2:


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



回答3:


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

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

    $ docker exec -t -i container_mysql_name /bin/bash

    -i is the shortcut for --interactive option. This options is used for keep STDIN open even if not attached

    -t is the shortcut for --tty option, used to allocate a pseudo-TTY

  2. I run MySQL client from bash MySQL container:

    $ mysql -uroot -proot

    -u is shortcut for --user=name option, used to define user for login if not current user.

    -p is 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!




回答4:


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

Instead I used -e.

Example:

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




回答5:


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

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

docker exec -it container_mysql_name mysql -u username -p



回答6:


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 -h and -P) and supporting -e:

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

MARGS=()
MPORT="3306"

while test $# != 0; do
  if [[ $1 == -h ]]; then MHOST=$2; shift;
  elif [[ $1 == -h* ]]; then MHOST=${1#"-h"};
  elif [[ $1 == -e ]]; then MEXEC=$2; shift;
  elif [[ $1 == -e* ]]; then MEXEC=${1#"-e"};
  elif [[ $1 == --execute=* ]]; then MEXEC=${1#"--execute="};
  elif [[ $1 == -P ]]; then MPORT=$2; shift;
  elif [[ $1 == -P* ]]; then MPORT=${1#"-P"};
  else MARGS="$MARGS $1"
  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]*).*/\2/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



回答7:


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

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



回答8:


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.

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.

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

#!/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.

richard




回答9:


First bash into your mysql container

docker exec -it mysql-container-name bash

Then within the container connect to mysql to run your commands

mysql -u root -p

Then you are able to run mysql queries.



来源:https://stackoverflow.com/questions/28389458/how-to-execute-mysql-command-from-the-host-to-container-running-mysql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!