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

前端 未结 9 1820
春和景丽
春和景丽 2021-01-30 06:31

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 run

相关标签:
9条回答
  • 2021-01-30 07:27

    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;"
    
    0 讨论(0)
  • 2021-01-30 07:28
    docker exec -i some_mysql_container mysql -uroot -ppassword  <<< "select database();"
    
    0 讨论(0)
  • 2021-01-30 07:30

    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;"

    0 讨论(0)
提交回复
热议问题