How to execute mysqldump command from the host machine to a mysql docker container

杀马特。学长 韩版系。学妹 提交于 2021-02-08 07:00:17

问题


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

the dumps directory is already bind to the host machine.

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: No such file or directory

But if I just run mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql inside the container it works fine.


回答1:


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.



回答2:


mysqldump takes the usual MySQL CLI options to connect to a server running somewhere else. That means you can run it directly from the host, without needing docker exec (administrator) permissions.

mysqldump -h127.0.0.1 -uroot -pSomePassword DBName > MyNewDump.sql

In contrast to the docker exec forms, this creates the dump file on the host, but that's probably what you want.



来源:https://stackoverflow.com/questions/63740465/how-to-execute-mysqldump-command-from-the-host-machine-to-a-mysql-docker-contain

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