在Docker容器上执行SQL脚本 [英] Executing SQL scripts on docker container
问题描述
我有一个运行mysql的docker容器,我想将一个.sql文件推送到该容器,然后让mysql在其中执行命令.第一步非常简单:
I have a docker container running mysql and I want to push a .sql file to the container and then have mysql execute the commands in it. The first step is fairly straightforward:
docker cp ./dummy.sql <container_id>:/
我试图从那里从命令行运行mysql,并将其指向刚推送到容器的文件.
From there I am trying to run mysql from the command line and point it to the file that I just pushed to the container.
docker exec <container_id> mysql -u root -ppassword < /dummy.sql
此命令似乎试图在本地而不是在容器上将/sample.sql用作stdin.我还尝试在容器ID之后的所有内容周围加上引号,这似乎也不起作用.
This command appears to be trying to use /sample.sql as stdin locally rather than on the container. I also tried wrapping quotes around everything after the container ID which also seems to not work.
我还尝试将包含命令的.sh文件推送到docker容器,然后执行该命令,但这并不理想,而且无法正常工作.有什么建议吗?
I also tried pushing a .sh file with the command in it to the docker container and then just executing that, but that is less ideal and also not working. Any advice?
推荐答案
如果要在命令中进行管道重定向,请将其作为字符串传递给/bin/sh
:
If you are going to do pipe redirections in your command, pass it as a string to /bin/sh
:
docker exec <container_id> /bin/sh -c 'mysql -u root -ppassword </dummy.sql'
这篇关于在Docker容器上执行SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!