如何从主机执行mysqldump命令到mysql docker容器 [英] How to execute mysqldump command from the host machine to a mysql docker container
问题描述
我想为在Docker容器中运行的数据库创建mysql转储.但是,我不想进入容器并执行命令,而是从主机上执行该命令.有没有办法做到这一点.我尝试了一些尝试,但是命令可能不对.
I want to create mysql dumps for a database which is running in docker container. However I do not want to get into the container and execute the command but do it from the host machine. Is there a way to do it. I tried few things but probably I am wrong with the commands.
docker exec -d mysql sh mysqldump -uroot -pSomePassword DBName>/dumps/MyNewDump.sql
docker exec -d mysql sh $(mysqldump -uroot -pSomePassword DBName>/dumps/MyNewDump.sql)
docker exec -d mysql mysqldump -uroot -pSomePassword DBName>/dumps/MyNewDump.sql
dumps
目录已绑定到主机.
这些命令似乎不是正确的方法,也可能根本不是正确的方法.这些总是以错误结尾:
These commands are seems not the right way to do it or probably not the right way to do it at all. These always ends up with an error:
bash:/dumps/MyNewDump.sql:没有这样的文件或目录
bash: /dumps/MyNewDump.sql: No such file or directory
但是,如果我只运行 mysqldump -uroot -pSomePassword DBName>/dumps/MyNewDump.sql
可以在容器内正常工作.
But if I just run mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql
inside the container it works fine.
推荐答案
这对我有效(只需替换容器ID):
This worked on my end(just replace the container ID):
docker exec 1d3595c0ce87 sh -c 'mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
这篇关于如何从主机执行mysqldump命令到mysql docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!